all repos — caroster @ 977a49f918209f5b36bb88670077f49cb1a5a14f

[Octree] Group carpool to your event https://caroster.io

fix: :bug: Fix authentication error messages
Tim Izzo tim@octree.ch
Wed, 28 Sep 2022 07:11:31 +0000
commit

977a49f918209f5b36bb88670077f49cb1a5a14f

parent

d8b50fcea8125de6998c9781d73e18b9372aff9e

M frontend/containers/SignInForm/index.tsxfrontend/containers/SignInForm/index.tsx

@@ -10,16 +10,18 @@ import FormHelperText from '@material-ui/core/FormHelperText';

import CardActions from '@material-ui/core/CardActions'; import {useTranslation} from 'react-i18next'; import {signIn} from 'next-auth/react'; -import useToastsStore from '../../stores/useToastStore'; import useAddToEvents from '../../hooks/useAddToEvents'; import useRedirectUrlStore from '../../stores/useRedirectUrl'; -const SignIn = () => { +interface Props { + error?: string; +} + +const SignIn = (props: Props) => { + const {error} = props; const {t} = useTranslation(); - const [error, setError] = useState(''); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); - const addToast = useToastsStore(s => s.addToast); const {saveStoredEvents} = useAddToEvents(); const classes = useStyles(); const getRedirectUrl = useRedirectUrlStore(s => s.getRedirectUrl);

@@ -40,29 +42,21 @@ callbackUrl,

}); saveStoredEvents(); // TODO Check it's correctly executed after sign-in } catch (error) { - handleAuthError(error); + console.error(error); } return false; }; - const handleAuthError = error => { - const strapiError = error.message; - console.error({strapiError}); - if (strapiError === 'Invalid identifier or password') { - setError(t('signin.errors')); - addToast(t('signin.errors')); - } else if (strapiError === 'Auth.form.error.confirmed') { - setError(t('signin.unconfirmed')); - addToast(t('signin.unconfirmed')); - } - }; - return ( <form onSubmit={onSubmit}> <CardContent className={classes.content}> <Typography variant="h6">{t('signin.title')}</Typography> - {error && <FormHelperText error={true}>{error}</FormHelperText>} + {error && ( + <FormHelperText error={true}> + {t(`signin.errors.${error}`)} + </FormHelperText> + )} <TextField label={t('signin.email')} fullWidth
M frontend/locales/en.jsonfrontend/locales/en.json

@@ -213,8 +213,8 @@ "signin.email": "Email",

"signin.password": "Password", "signin.login": "$t(menu.login)", "signin.register": "$t(menu.register)", - "signin.errors": "Check your email and password", - "signin.unconfirmed": "Your account has not been confirmed. Please check your emails", + "signin.errors.CredentialsSignin": "Check your email and password", + "signin.errors.EmailNotConfirmed": "Your account has not been confirmed. Please check your emails", "signin.withGoogle": "Use a Google account", "lost_password.title": "Password recovery", "lost_password.reset_title": "Definition of a new password",

@@ -231,4 +231,4 @@ "lost_password.actions.login": "Return to the login screen",

"lost_password.actions.resend": "Send again", "lost_password.actions.register": "Create an account?", "lost_password.actions.save_new_password": "Update" -}+}
M frontend/locales/fr.jsonfrontend/locales/fr.json

@@ -213,8 +213,8 @@ "signin.email": "Email",

"signin.password": "Mot de passe", "signin.login": "$t(menu.login)", "signin.register": "$t(menu.register)", - "signin.errors": "Vérifier votre email et mot de passe", - "signin.unconfirmed": "Votre compte n'a pas été confirmé. Merci de vérifier vos emails", + "signin.errors.CredentialsSignin": "Vérifier votre email et mot de passe", + "signin.errors.EmailNotConfirmed": "Votre compte n'a pas été confirmé. Merci de vérifier vos emails", "signin.withGoogle": "Utiliser un compte Google", "lost_password.title": "Récupération de mot de passe", "lost_password.reset_title": "Définition d'un nouveau mot de passe",

@@ -231,4 +231,4 @@ "lost_password.actions.login": "Retour à l'écran de connexion",

"lost_password.actions.resend": "Envoyer à nouveau", "lost_password.actions.register": "Créer un compte ?", "lost_password.actions.save_new_password": "Mettre à jour" -}+}
M frontend/pages/api/nauth/[...nextauth].jsfrontend/pages/api/nauth/[...nextauth].js

@@ -13,22 +13,20 @@ email: {label: 'Email', type: 'text'},

password: {label: 'Password', type: 'password'}, }, async authorize(credentials, req) { - try { - const response = await fetch(`${STRAPI_URL}/api/auth/local`, { - method: 'POST', - headers: {'Content-Type': 'application/json'}, - body: JSON.stringify({ - identifier: credentials.email, - password: credentials.password, - }), - }); - const data = await response.json(); - const {user, jwt} = data; - return {...user, jwt}; - } catch (error) { - console.error({error}); - return null; - } + const response = await fetch(`${STRAPI_URL}/api/auth/local`, { + method: 'POST', + headers: {'Content-Type': 'application/json'}, + body: JSON.stringify({ + identifier: credentials.email, + password: credentials.password, + }), + }); + const data = await response.json(); + if (data?.error?.message === 'Your account email is not confirmed') + throw new Error('EmailNotConfirmed'); + else if (!data?.jwt) return null; + const {user, jwt} = data; + return {...user, jwt}; }, }), GoogleProvider({

@@ -80,5 +78,6 @@ },

}, pages: { signIn: '/auth/login', + error: '/auth/login', }, });
M frontend/pages/auth/login.tsxfrontend/pages/auth/login.tsx

@@ -10,14 +10,19 @@ import LanguagesIcon from '../../containers/Languages/Icon';

import {getSession} from 'next-auth/react'; import pageUtils from '../../lib/pageUtils'; -const Login = () => { +interface PageProps { + error?: string; +} + +const Login = (props: PageProps) => { const {t} = useTranslation(); return ( <Layout menuTitle={t('signin.title')} displayMenu={false}> <Card> <CardMedia component={Logo} /> - <SignInForm /> + <SignInForm error={props?.error} /> + d'une autre branche <Divider /> <LoginGoogle /> </Card>

@@ -36,7 +41,11 @@ destination: '/',

permanent: false, }, }; - else return pageUtils.getServerSideProps()(context); + else + return pageUtils.getServerSideProps(async ctx => { + const error = ctx.query?.error || null; + return {props: {error}}; + })(context); }; export default Login;