diff --git a/app/Controllers/Http/AuthController.ts b/app/Controllers/Http/AuthController.ts index e5815cdc..d010fbb6 100644 --- a/app/Controllers/Http/AuthController.ts +++ b/app/Controllers/Http/AuthController.ts @@ -21,6 +21,7 @@ export default class AuthController { response.redirect().toRoute('login') } } + async signup({ view }: HttpContextContract) { return view.render('auth/signup') @@ -31,4 +32,10 @@ export default class AuthController { await User.create(playload) return response.redirect().toRoute('home') } + + + async logout({ auth, response }:HttpContextContract) { + await auth.logout() + return response.redirect().back() + } } diff --git a/app/Controllers/Http/CompteController.ts b/app/Controllers/Http/CompteController.ts new file mode 100644 index 00000000..a1a6ab9f --- /dev/null +++ b/app/Controllers/Http/CompteController.ts @@ -0,0 +1,33 @@ +import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' +import ModifPseudoValidator from 'App/Validators/ModifPseudoValidator' +import ModifEmailValidator from 'App/Validators/ModifEmailValidator' + +export default class CompteController { + + async index({ view }: HttpContextContract) { + return view.render('compte') + } + + async modifpseudo({ request, auth, session, response }: HttpContextContract) { + const user = auth.user + + await request.validate(ModifPseudoValidator) + + user!.pseudo = request.input('pseudo') + await user!.save() + session.flash({success: "Username updated successfully"}) + response.redirect().back() + } + + async modifemail({ request, auth, session, response }: HttpContextContract) { + const user = auth.user + + await request.validate(ModifEmailValidator) + + user!.email = request.input('email') + await user!.save() + session.flash({success: "Email updated successfully"}) + response.redirect().back() + } + +} diff --git a/app/Validators/ModifEmailValidator.ts b/app/Validators/ModifEmailValidator.ts new file mode 100644 index 00000000..564a70bd --- /dev/null +++ b/app/Validators/ModifEmailValidator.ts @@ -0,0 +1,46 @@ +import { schema, CustomMessages, rules } from '@ioc:Adonis/Core/Validator' +import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' + +export default class ModifEmailValidator { + constructor(protected ctx: HttpContextContract) {} + + /* + * Define schema to validate the "shape", "type", "formatting" and "integrity" of data. + * + * For example: + * 1. The username must be of data type string. But then also, it should + * not contain special characters or numbers. + * ``` + * schema.string([ rules.alpha() ]) + * ``` + * + * 2. The email must be of data type string, formatted as a valid + * email. But also, not used by any other user. + * ``` + * schema.string([ + * rules.email(), + * rules.unique({ table: 'users', column: 'email' }), + * ]) + * ``` + */ + public schema = schema.create({ + email: schema.string({}, [rules.email(), rules.unique({ table: 'users', column: 'email' }) ]) + }) + + /** + * Custom messages for validation failures. You can make use of dot notation `(.)` + * for targeting nested fields and array expressions `(*)` for targeting all + * children of an array. For example: + * + * { + * 'profile.username.required': 'Username is required', + * 'scores.*.number': 'Define scores as valid numbers' + * } + * + */ + public messages: CustomMessages = { + required: 'The {{ field }} is required to modifie email', + 'email.email': 'You must enter an email in the email field', + 'email.unique': 'Email is already in use' + } +} diff --git a/app/Validators/ModifPseudoValidator.ts b/app/Validators/ModifPseudoValidator.ts new file mode 100644 index 00000000..5249f82f --- /dev/null +++ b/app/Validators/ModifPseudoValidator.ts @@ -0,0 +1,45 @@ +import { schema, CustomMessages, rules } from '@ioc:Adonis/Core/Validator' +import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' + +export default class ModifPseudoValidator { + constructor(protected ctx: HttpContextContract) {} + + /* + * Define schema to validate the "shape", "type", "formatting" and "integrity" of data. + * + * For example: + * 1. The username must be of data type string. But then also, it should + * not contain special characters or numbers. + * ``` + * schema.string([ rules.alpha() ]) + * ``` + * + * 2. The email must be of data type string, formatted as a valid + * email. But also, not used by any other user. + * ``` + * schema.string([ + * rules.email(), + * rules.unique({ table: 'users', column: 'email' }), + * ]) + * ``` + */ + public schema = schema.create({ + pseudo: schema.string({}, [rules.minLength(3)]) + }) + + /** + * Custom messages for validation failures. You can make use of dot notation `(.)` + * for targeting nested fields and array expressions `(*)` for targeting all + * children of an array. For example: + * + * { + * 'profile.username.required': 'Username is required', + * 'scores.*.number': 'Define scores as valid numbers' + * } + * + */ + public messages: CustomMessages = { + required: 'The {{ field }} is required to modifie pseudo', + 'pseudo.minLength': 'The pseudo must be at least 3 characters long' + } +} diff --git a/resources/views/components/form/form.edge b/resources/views/components/form/form.edge index 1f91dda3..5409bd2e 100644 --- a/resources/views/components/form/form.edge +++ b/resources/views/components/form/form.edge @@ -1,4 +1,7 @@
\ No newline at end of file diff --git a/resources/views/components/select_theme.edge b/resources/views/components/select_theme.edge index 5f3ecec2..753b3d9c 100644 --- a/resources/views/components/select_theme.edge +++ b/resources/views/components/select_theme.edge @@ -22,7 +22,7 @@