diff --git a/context/AuthContext.js b/context/AuthContext.js index 628cd5d0..fcff8a2f 100644 --- a/context/AuthContext.js +++ b/context/AuthContext.js @@ -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() @@ -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) @@ -118,7 +120,29 @@ 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 () => { @@ -126,11 +150,22 @@ export function AuthProvider({ children }) { 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) } @@ -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 = '' diff --git a/functions/lib/mailchimp.js b/functions/lib/mailchimp.js index c15ebc0f..57a08541 100644 --- a/functions/lib/mailchimp.js +++ b/functions/lib/mailchimp.js @@ -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:') diff --git a/public/locales/en/translation.json b/public/locales/en/translation.json index b4cb707a..bb5fe68d 100644 --- a/public/locales/en/translation.json +++ b/public/locales/en/translation.json @@ -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": { diff --git a/public/locales/pt-BR/translation.json b/public/locales/pt-BR/translation.json index c40e54c8..a978a5be 100644 --- a/public/locales/pt-BR/translation.json +++ b/public/locales/pt-BR/translation.json @@ -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?",