UNPKG

@adonisjs/auth

Version:

Offical authentication provider for Adonis framework

99 lines (98 loc) 2.92 kB
"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.InvalidCredentialsException = void 0; const utils_1 = require("@poppinss/utils"); /** * Exception raised when unable to verify user credentials */ class InvalidCredentialsException extends utils_1.Exception { /** * Unable to find user */ static invalidUid(guard) { const error = new this('User not found', 400, 'E_INVALID_AUTH_UID'); error.guard = guard; return error; } /** * Invalid user password */ static invalidPassword(guard) { const error = new this('Password mis-match', 400, 'E_INVALID_AUTH_PASSWORD'); error.guard = guard; return error; } /** * Send response as an array of errors */ respondWithJson(ctx) { ctx.response.status(this.status).send({ errors: [ { message: 'Invalid user credentials', }, ], }); } /** * Flash error message and redirect the user back */ respondWithRedirect(ctx) { if (!ctx.session) { return ctx.response.status(this.status).send('Invalid credentials'); } ctx.session.flashExcept(['_csrf']); ctx.session.flash('auth', { errors: { uid: this.code === 'E_INVALID_AUTH_UID' ? ['Invalid login id'] : null, password: this.code === 'E_INVALID_AUTH_PASSWORD' ? ['Invalid password'] : null, }, }); ctx.response.redirect('back', true); } /** * Send response as an array of errors formatted as per JSONAPI spec */ respondWithJsonAPI(ctx) { ctx.response.status(this.status).send({ errors: [ { code: this.code, title: 'Invalid user credentials', source: null, }, ], }); } /** * Self handle exception and attempt to make the best response based * upon the type of request */ async handle(_, ctx) { if (ctx.request.ajax()) { this.respondWithJson(ctx); return; } switch (ctx.request.accepts(['html', 'application/vnd.api+json', 'json'])) { case 'html': case null: this.respondWithRedirect(ctx); break; case 'json': this.respondWithJson(ctx); break; case 'application/vnd.api+json': this.respondWithJsonAPI(ctx); break; } } } exports.InvalidCredentialsException = InvalidCredentialsException;