Last active
December 17, 2021 02:20
-
-
Save tomgobich/5ade223503147d816f8a28db2d3eeac1 to your computer and use it in GitHub Desktop.
AdonisJS Guest Middleware Example
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
| import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' | |
| export default class Guest { | |
| public async handle({ auth, response }: HttpContextContract, next: () => Promise<void>) { | |
| if (auth.user) { | |
| // redirect user where ever you'd like | |
| return response.redirect('/'); | |
| } | |
| await next() | |
| } | |
| } |
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
| Server.middleware.registerNamed({ | |
| auth: () => import('App/Middleware/Auth'), | |
| guest: () => import('App/Middleware/Guest') // <- register middleware | |
| }) |
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
| Route.group(() => { | |
| Route.get('register', 'AuthController.registerShow').as('register.show') | |
| Route.post('register', 'AuthController.register').as('register') | |
| Route.get('login', 'AuthController.loginShow').as('login.show') | |
| Route.post('login', 'AuthController.login').as('login') | |
| Route.get('logout', 'AuthController.logout').as('logout') | |
| }).as('auth').middleware(['guest']) // <- apply middleware to routes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment