@adonisjs/auth
Version:
Official authentication provider for Adonis framework
38 lines (37 loc) • 1.1 kB
JavaScript
//#region src/middleware/initialize_auth_middleware.ts
/**
* The "InitializeAuthMiddleware" is used to create a request
* specific authenticator instance for every HTTP request.
*
* This middleware does not protect routes from unauthenticated
* users. Please use the "auth" middleware for that.
*
* @example
* router.use([() => import('#middleware/initialize_auth_middleware')])
*/
var InitializeAuthMiddleware = class {
/**
* Handle the HTTP request by initializing the authenticator
*
* @param ctx - The HTTP context
* @param next - The next function to call in the middleware chain
*
* @example
* // This middleware runs automatically when registered
* // It adds ctx.auth to every HTTP request
*/
async handle(ctx, next) {
/**
* Initialize the authenticator for the current HTTP
* request
*/
ctx.auth = (await ctx.containerResolver.make("auth.manager")).createAuthenticator(ctx);
/**
* Sharing authenticator with templates
*/
if ("view" in ctx) ctx.view.share({ auth: ctx.auth });
return next();
}
};
//#endregion
export { InitializeAuthMiddleware as default };