mirror of
https://github.com/arthur-pbty/portfolio2023.git
synced 2026-06-05 13:52:25 +02:00
113 lines
3.2 KiB
JavaScript
113 lines
3.2 KiB
JavaScript
"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 {
|
|
constructor() {
|
|
super(...arguments);
|
|
this.responseText = this.message;
|
|
}
|
|
/**
|
|
* 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: this.responseText,
|
|
},
|
|
],
|
|
});
|
|
}
|
|
/**
|
|
* Flash error message and redirect the user back
|
|
*/
|
|
respondWithRedirect(ctx) {
|
|
if (!ctx.session) {
|
|
return ctx.response.status(this.status).send(this.responseText);
|
|
}
|
|
ctx.session.flashExcept(['_csrf']);
|
|
ctx.session.flash('auth', {
|
|
error: this.responseText,
|
|
/**
|
|
* Will be removed in the future
|
|
*/
|
|
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: this.responseText,
|
|
source: null,
|
|
},
|
|
],
|
|
});
|
|
}
|
|
/**
|
|
* Self handle exception and attempt to make the best response based
|
|
* upon the type of request
|
|
*/
|
|
async handle(_, ctx) {
|
|
/**
|
|
* Use translation when using i18n
|
|
*/
|
|
if ('i18n' in ctx) {
|
|
this.responseText = ctx.i18n.formatMessage(`auth.${this.code}`, {}, this.message);
|
|
}
|
|
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;
|