"use strict";
/*
 * @adonisjs/auth
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.Auth = void 0;
/**
 * Auth class exposes the API to obtain guard instances for a given
 * HTTP request.
 */
class Auth {
    constructor(manager, ctx) {
        this.manager = manager;
        this.ctx = ctx;
        /**
         * We keep a per request singleton instances for each instantiated mapping
         */
        this.mappingsCache = new Map();
        /**
         * The default guard is always the one defined inside the config, until
         * manually overwritten by the user
         */
        this.defaultGuard = this.manager.defaultGuard;
    }
    /**
     * Returns an instance of a named or the default mapping
     */
    use(mapping) {
        mapping = mapping || this.defaultGuard;
        if (!this.mappingsCache.has(mapping)) {
            this.ctx.logger.trace('instantiating auth mapping', { name: mapping });
            this.mappingsCache.set(mapping, this.manager.makeMapping(this.ctx, mapping));
        }
        return this.mappingsCache.get(mapping);
    }
    /**
     * Guard name for the default mapping
     */
    get name() {
        return this.use().name;
    }
    /**
     * Reference to the logged in user
     */
    get user() {
        return this.use().user;
    }
    /**
     * Reference to the default guard config
     */
    get config() {
        return this.use().config;
    }
    /**
     * Find if the user has been logged out in the current request
     */
    get isLoggedOut() {
        return this.use().isLoggedOut;
    }
    /**
     * A boolean to know if user is a guest or not. It is
     * always opposite of [[isLoggedIn]]
     */
    get isGuest() {
        return this.use().isGuest;
    }
    /**
     * A boolean to know if user is logged in or not
     */
    get isLoggedIn() {
        return this.use().isLoggedIn;
    }
    /**
     * A boolean to know if user is retrieved by authenticating
     * the current request or not.
     */
    get isAuthenticated() {
        return this.use().isAuthenticated;
    }
    /**
     * Whether or not the authentication has been attempted
     * for the current request
     */
    get authenticationAttempted() {
        return this.use().authenticationAttempted;
    }
    /**
     * Reference to the provider for looking up the user
     */
    get provider() {
        return this.use().provider;
    }
    /**
     * Verify user credentials.
     */
    async verifyCredentials(uid, password) {
        return this.use().verifyCredentials(uid, password);
    }
    /**
     * Attempt to verify user credentials and perform login
     */
    async attempt(uid, password, ...args) {
        return this.use().attempt(uid, password, ...args);
    }
    /**
     * Login a user without any verification
     */
    async login(user, ...args) {
        return this.use().login(user, ...args);
    }
    /**
     * Login a user using their id
     */
    async loginViaId(id, ...args) {
        return this.use().loginViaId(id, ...args);
    }
    /**
     * Attempts to authenticate the user for the current HTTP request. An exception
     * is raised when unable to do so
     */
    async authenticate() {
        return this.use().authenticate();
    }
    /**
     * Attempts to authenticate the user for the current HTTP request and supresses
     * exceptions raised by the [[authenticate]] method and returns a boolean
     */
    async check() {
        return this.use().check();
    }
    /**
     * Logout user
     */
    async logout(...args) {
        return this.use().logout(...args);
    }
    /**
     * Serialize toJSON
     */
    toJSON() {
        return {
            defaultGuard: this.defaultGuard,
            guards: [...this.mappingsCache.keys()].reduce((result, key) => {
                result[key] = this.mappingsCache.get(key).toJSON();
                return result;
            }, {}),
        };
    }
}
exports.Auth = Auth;
