SilentAuth.ts 662 B

123456789101112131415161718192021
  1. import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
  2. /**
  3. * Silent auth middleware can be used as a global middleware to silent check
  4. * if the user is logged-in or not.
  5. *
  6. * The request continues as usual, even when the user is not logged-in.
  7. */
  8. export default class SilentAuthMiddleware {
  9. /**
  10. * Handle request
  11. */
  12. public async handle({ auth }: HttpContextContract, next: () => Promise<void>) {
  13. /**
  14. * Check if user is logged-in or not. If yes, then `ctx.auth.user` will be
  15. * set to the instance of the currently logged in user.
  16. */
  17. await auth.check()
  18. await next()
  19. }
  20. }