Skip to content

Commit

Permalink
fix: message when login abort instead of breaking the app
Browse files Browse the repository at this point in the history
  • Loading branch information
nomadbitcoin committed Sep 25, 2024
1 parent 8b1a679 commit 1496e5f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
66 changes: 61 additions & 5 deletions context/AuthContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { auth } from '../firebase/initFirebase.js'
import { getUserFromFirestore, createUserinFirestore, updateUserGithub } from '../lib/user.js'
import { useTranslation } from 'react-i18next'
import { useRouter } from 'next/router'

const AuthContext = createContext()

Expand All @@ -39,6 +40,7 @@ const toastParameters = {
}

export function AuthProvider({ children }) {
const router = useRouter()
const { t } = useTranslation()
const [user, setUser] = useState(null)
const [loading, setLoading] = useState(true)
Expand Down Expand Up @@ -118,19 +120,52 @@ export function AuthProvider({ children }) {
}

const loginGoogle = async () => {
return loginWithProvider(GoogleAuthProvider)
setLoading(true)
try {
const result = await signInWithPopup(auth, new GoogleAuthProvider())
await handleUser(result.user)
const redirectPath = router.query.from || '/courses'
router.push(redirectPath)
toast.success(t('messages.login_success'), {
toastParameters,
})
} catch (error) {
console.error('Google login error:', error)
if (error.code === 'auth/popup-closed-by-user') {
toast.info(t('messages.login_cancelled'), {
toastParameters,
})
} else {
toast.error(t('messages.something_wrong_try_again'), {
toastParameters,
})
}
} finally {
setLoading(false)
}
}

const loginGithub = async () => {
setLoading(true)
try {
const result = await signInWithPopup(auth, new GithubAuthProvider())
await handleGithubLogin(result.user)
} catch (error) {
console.error('GitHub login error:', error)
toast.error(t('messages.something_wrong_try_again'), {
const redirectPath = router.query.from || '/courses'
router.push(redirectPath)
toast.success(t('messages.login_success'), {
toastParameters,
})
} catch (error) {
console.error('GitHub login error:', error)
if (error.code === 'auth/popup-closed-by-user') {
toast.info(t('messages.login_cancelled'), {
toastParameters,
})
} else {
toast.error(t('messages.something_wrong_try_again'), {
toastParameters,
})
}
} finally {
setLoading(false)
}
Expand All @@ -155,7 +190,28 @@ export function AuthProvider({ children }) {

const loginWithProvider = async (Provider) => {
setLoading(true)
await signInWithPopup(auth, new Provider())
try {
const result = await signInWithPopup(auth, new Provider())
await handleUser(result.user)
const redirectPath = router.query.from || '/courses'
router.push(redirectPath)
toast.success(t('messages.login_success'), {
toastParameters,
})
} catch (error) {
console.error('Provider login error:', error)
if (error.code === 'auth/popup-closed-by-user') {
toast.info(t('messages.login_cancelled'), {
toastParameters,
})
} else {
toast.error(t('messages.something_wrong_try_again'), {
toastParameters,
})
}
} finally {
setLoading(false)
}
}
let githubUrl = ''

Expand Down
1 change: 0 additions & 1 deletion functions/lib/mailchimp.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ exports.createUser = async function (user) {
const result = await mailchimp.lists.addListMember('b578d43584', {
email_address: user.email,
status: 'subscribed',
full_name: user.name,
merge_fields: { WALLET: user.wallet_address, NAME: user.name },
})
console.log('User added to Mailchimp successfully:')
Expand Down
3 changes: 2 additions & 1 deletion public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@
"data_updated_success": "Data updated successfully!",
"error_updating_data": "Error updating data",
"language_not_available": "Content not available for the selected language. Using the default language.",
"course_registration_success": "Successfully registered for the course!"
"course_registration_success": "Successfully registered for the course!",
"login_cancelled": "Login cancelled"
},
"twitter-share": "Networking is everything, how about sharing your progress with your friends on Twitter?",
"buttons": {
Expand Down
3 changes: 2 additions & 1 deletion public/locales/pt-BR/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@
"data_updated_success": "Dados atualizados com sucesso!",
"error_updating_data": "Erro ao atualizar dados",
"language_not_available": "Conteúdo não disponível para o idioma selecionado. Usando o idioma padrão.",
"course_registration_success": "Registrado no curso com sucesso!"
"course_registration_success": "Registrado no curso com sucesso!",
"login_cancelled": "Login cancelado"
},
"twitter-share": "Networking é tudo, que tal compartilhar seu progresso com os seus amigos no Twitter?",
"link_github_account": "Deseja vincular o github à sua conta já cadastrada?",
Expand Down

0 comments on commit 1496e5f

Please sign in to comment.