diff --git a/website/app/controllers/authentication_controller.ts b/website/app/controllers/authentication_controller.ts index 894166e..bd1e213 100644 --- a/website/app/controllers/authentication_controller.ts +++ b/website/app/controllers/authentication_controller.ts @@ -2,6 +2,7 @@ import Account from '#models/account' import { socialAccountLoginValidator } from '#validators/account' import User from '#models/user' import type { HttpContext } from '@adonisjs/core/http' +import { createUserValidator, createUserValidatorErrorMessage } from '#validators/authentication' async function getOrCreate(search: Pick) { const account = await Account.firstOrCreate({ @@ -25,25 +26,23 @@ export default class AuthenticationController { response.redirect('/') } catch (error) { session.flash('errorsBag', { oauth: 'Email ou palavra-passe incorretos' }) - response.redirect().back() + return response.redirect().back() } } async register({ request, auth, response, session }: HttpContext) { - const { email, password, confirmPassword } = request.only([ - 'email', - 'password', - 'confirmPassword', - ]) + try { + await request.validateUsing(createUserValidator) + } catch (error) { + session.flash('errorsBag', { oauth: createUserValidatorErrorMessage(error) }) + return response.redirect().toRoute('view.register') + } + + const { email, password } = request.only(['email', 'password']) if (await User.query().where('email', email).first()) { session.flash('errorsBag', { oauth: 'Este e-mail já está em uso' }) - response.redirect().back() - } - - if (confirmPassword !== password) { - session.flash('errorsBag', { oauth: 'Palavras-passes não coincidem' }) - response.redirect().back() + return response.redirect().toRoute('view.register') } try { @@ -60,7 +59,8 @@ export default class AuthenticationController { response.redirect('/') } catch (error) { - console.log(error) + session.flash('errorsBag', { oauth: 'Ocorreu um erro no registo' }) + return response.redirect().toRoute('view.register') } } diff --git a/website/app/validators/authentication.ts b/website/app/validators/authentication.ts new file mode 100644 index 0000000..99a8ee8 --- /dev/null +++ b/website/app/validators/authentication.ts @@ -0,0 +1,22 @@ +import vine from '@vinejs/vine' +import { VineValidationError } from '../../types/validation.js' + +export const createUserValidator = vine.compile( + vine.object({ + email: vine.string().email(), + password: vine.string().minLength(8).confirmed(), + }) +) + +export const createUserValidatorErrorMessage = (error: VineValidationError) => { + const rule = error.messages[0].rule + + switch (rule) { + case 'email': + return 'E-mail inválido' + case 'minLength': + return 'Palavra-passe tem de ter no mínimo 8 caratéres' + case 'confirmed': + return 'Palavras-passe não coincidem' + } +} diff --git a/website/inertia/pages/login.tsx b/website/inertia/pages/login.tsx index a203c7e..cfa4093 100644 --- a/website/inertia/pages/login.tsx +++ b/website/inertia/pages/login.tsx @@ -1,9 +1,8 @@ import { Link } from '@tuyau/inertia/react' -import { Button, buttonVariants } from '~/components/ui/button' +import { Button } from '~/components/ui/button' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '~/components/ui/card' import { Input } from '~/components/ui/input' import { Label } from '~/components/ui/label' -import { Separator } from '~/components/ui/separator' import { useError } from '~/hooks/use_error' import { useForm } from '@inertiajs/react' import { cn } from '~/lib/utils' @@ -27,7 +26,7 @@ export default function Login() {
- + Iniciar Sessão @@ -71,34 +70,34 @@ export default function Login() { -
- -

Ou

- -
-
- - Iniciar Sessão com o Google - {/* */} - - - Iniciar Sessão com o Github - {/* */} - - - Iniciar Sessão com o LinkedIn - {/* */} - -
+ {/*
*/} + {/* */} + {/*

Ou

*/} + {/* */} + {/*
*/} + {/*
*/} + {/* */} + {/* Iniciar Sessão com o Google */} + {/* {/* */} + {/* */} + {/* */} + {/* Iniciar Sessão com o Github */} + {/* {/* */} + {/* */} + {/* */} + {/* Iniciar Sessão com o LinkedIn */} + {/* {/* */} + {/* */} + {/*
*/}
@@ -108,9 +107,9 @@ export default function Login() {
+ {oauthError &&

{oauthError}

} - {oauthError &&

{oauthError}

}
diff --git a/website/inertia/pages/register.tsx b/website/inertia/pages/register.tsx index 4127b7d..3fcbd36 100644 --- a/website/inertia/pages/register.tsx +++ b/website/inertia/pages/register.tsx @@ -1,9 +1,7 @@ -import { Link } from '@tuyau/inertia/react' -import { Button, buttonVariants } from '~/components/ui/button' +import { Button } from '~/components/ui/button' import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '~/components/ui/card' import { Input } from '~/components/ui/input' import { Label } from '~/components/ui/label' -import { Separator } from '~/components/ui/separator' import { useError } from '~/hooks/use_error' import { useForm } from '@inertiajs/react' import { cn } from '~/lib/utils' @@ -28,7 +26,7 @@ export default function Login() {
- + Criar conta @@ -42,7 +40,7 @@ export default function Login() { setData('email', e.target.value)} @@ -80,40 +78,40 @@ export default function Login() { -
- -

Ou

- -
-
- - Iniciar Sessão com o Google - {/* */} - - - Iniciar Sessão com o Github - {/* */} - - - Iniciar Sessão com o LinkedIn - {/* */} - -
+ {/*
*/} + {/* */} + {/*

Ou

*/} + {/* */} + {/*
*/} + {/*
*/} + {/* */} + {/* Iniciar Sessão com o Google */} + {/* {/* */} + {/* */} + {/* */} + {/* Iniciar Sessão com o Github */} + {/* {/* */} + {/* */} + {/* */} + {/* Iniciar Sessão com o LinkedIn */} + {/* {/* */} + {/* */} + {/*
*/}
+ {oauthError &&

{oauthError}

} - {oauthError &&

{oauthError}

}
diff --git a/website/types/validation.ts b/website/types/validation.ts new file mode 100644 index 0000000..d5afab6 --- /dev/null +++ b/website/types/validation.ts @@ -0,0 +1,14 @@ +/** + * Types declared for error formats in the validators using vine + */ + +export type VineValidationError = { + code: string + messages: Array +} + +export type VineValidationErrorMessage = { + rule: string + message: string + field: string +}