Created
June 11, 2020 20:24
-
-
Save Voltra/a33a5cc4143705737c939bca8bedfd5c to your computer and use it in GitHub Desktop.
Adonis code sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "use strict"; | |
| const User = use("App/Models/User"); | |
| class AuthController { | |
| static get defaultRoute(){ | |
| return "tab.search"; | |
| } | |
| static get userRoute(){ | |
| return "user.profile"; | |
| } | |
| static get adminRoute(){ | |
| return "admin.dashboard"; | |
| } | |
| static get adminRole(){ | |
| return "operator"; | |
| } | |
| async redir({ response, auth, session }){ | |
| await session.commit(); | |
| try{ | |
| await auth.check(); | |
| return response.route(AuthController.userRoute); | |
| }catch(e){ | |
| return response.route(AuthController.defaultRoute); | |
| } | |
| } | |
| /** | |
| * @param {Adonis.Http.Context} ctx | |
| * @param {Adonis.Http.Request} ctx.request | |
| * @param {Adonis.Http.Response} ctx.response | |
| * @param {any} ctx.view | |
| */ | |
| async login({ response, view }) { | |
| return view.render("auth.login", { | |
| title: "Connexion", | |
| description: "Page de connexion CMS", | |
| og: { | |
| image: "/assets/img/bg.jpg" | |
| }, | |
| tags: ["taraud", "connexion", "cms"] | |
| }); | |
| } | |
| /** | |
| * @param {Adonis.Http.Context} ctx | |
| * @param {Adonis.Http.Request} ctx.request | |
| * @param {Adonis.Http.Response} ctx.response | |
| * @param {any} ctx.view | |
| * @param {any} ctx.auth | |
| */ | |
| async handleLogin(ctx){ | |
| const { request, response, auth, session } = ctx; | |
| let { email, password, remember } = request.all(); | |
| remember = ["on", "true", "1"].includes(`${remember}`.toLowerCase()); | |
| try{ | |
| await auth.remember(remember).attempt(email, password); | |
| session.flash({success: "Vous êtes bien connecté"}); | |
| await session.commit(); | |
| return this.redir(ctx); | |
| }catch(e){ | |
| await session.withErrors({ | |
| password: "Le mot de passe saisi est invalide" | |
| }).flashAll(); | |
| await session.commit(); | |
| return response.route("auth.login"); | |
| } | |
| } | |
| /** | |
| * @param {Adonis.Http.Context} ctx | |
| * @param {Adonis.Http.Request} ctx.request | |
| * @param {Adonis.Http.Response} ctx.response | |
| * @param {any} ctx.view | |
| * @param {any} ctx.auth | |
| */ | |
| async logout(ctx){ | |
| const { request, response, auth, session } = ctx; | |
| await auth.logout(); | |
| session.flash({success: "Vous avez été correctement déconnecté"}); | |
| return this.redir(ctx); | |
| } | |
| } | |
| module.exports = AuthController; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment