UNPKG

1.53 kBJavaScriptView Raw
1"use strict";
2/*
3 * @adonisjs/auth
4 *
5 * (c) Harminder Virk <virk@adonisjs.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10Object.defineProperty(exports, "__esModule", { value: true });
11/**
12 * Auth provider to register the auth binding
13 */
14class AuthProvider {
15 constructor(application) {
16 this.application = application;
17 }
18 /**
19 * Register auth binding
20 */
21 register() {
22 this.application.container.singleton('Adonis/Addons/Auth', () => {
23 const authConfig = this.application.container
24 .resolveBinding('Adonis/Core/Config')
25 .get('auth', {});
26 const { AuthManager } = require('../src/AuthManager');
27 return new AuthManager(this.application, authConfig);
28 });
29 }
30 /**
31 * Hook into boot to register auth macro
32 */
33 async boot() {
34 this.application.container.withBindings(['Adonis/Core/HttpContext', 'Adonis/Addons/Auth'], (HttpContext, Auth) => {
35 HttpContext.getter('auth', function auth() {
36 return Auth.getAuthForRequest(this);
37 }, true);
38 });
39 this.application.container.withBindings(['Adonis/Core/Server', 'Adonis/Core/View'], (Server) => {
40 Server.hooks.before(async (ctx) => {
41 ctx['view'].share({ auth: ctx.auth });
42 });
43 });
44 }
45}
46exports.default = AuthProvider;
47AuthProvider.needsApplication = true;