UNPKG

@adonisjs/ally

Version:

Social authentication provider for AdonisJS

120 lines (117 loc) 3.46 kB
var __defProp = Object.defineProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; // src/errors.ts var errors_exports = {}; __export(errors_exports, { E_LOCAL_SIGNUP_DISALLOWED: () => E_LOCAL_SIGNUP_DISALLOWED, E_OAUTH_MISSING_CODE: () => E_OAUTH_MISSING_CODE, E_OAUTH_STATE_MISMATCH: () => E_OAUTH_STATE_MISMATCH, E_UNKNOWN_ALLY_PROVIDER: () => E_UNKNOWN_ALLY_PROVIDER, HttpResponseException: () => HttpResponseException }); import { Exception } from "@adonisjs/core/exceptions"; var HttpResponseException = class extends Exception { /** * Returns the message to be sent in the HTTP response. * Feel free to override this method and return a custom * response. */ getResponseMessage(error, ctx) { if ("i18n" in ctx) { return ctx.i18n.t(error.identifier, {}, error.message); } return error.message; } /** * Converts exception to an HTTP response */ async handle(error, ctx) { const message = this.getResponseMessage(error, ctx); switch (ctx.request.accepts(["html", "application/vnd.api+json", "json"])) { case "html": case null: if (ctx.session) { ctx.session.flash("error", message); ctx.session.flashErrors({ [error.code]: message }); ctx.response.redirect().back(); } else { ctx.response.status(error.status).send(message); } break; case "json": ctx.response.status(error.status).send({ errors: [ { message } ] }); break; case "application/vnd.api+json": ctx.response.status(error.status).send({ errors: [ { code: error.code, title: message } ] }); break; } } }; var E_OAUTH_MISSING_CODE = class extends HttpResponseException { static status = 500; static code = "E_OAUTH_MISSING_CODE"; /** * Translation identifier. Can be customized */ identifier = "errors.E_OAUTH_MISSING_CODE"; constructor(args, options) { super( `Cannot request access token. Redirect request is missing the "${args[0]}" param`, options ); } }; var E_OAUTH_STATE_MISMATCH = class extends HttpResponseException { static status = 400; static code = "E_OAUTH_STATE_MISMATCH"; static message = "Unable to verify re-redirect state"; /** * Translation identifier. Can be customized */ identifier = "errors.E_OAUTH_STATE_MISMATCH"; }; var E_UNKNOWN_ALLY_PROVIDER = class extends HttpResponseException { static status = 404; static code = "E_UNKNOWN_ALLY_PROVIDER"; /** * Translation identifier. Can be customized */ identifier = "errors.E_UNKNOWN_ALLY_PROVIDER"; constructor(args, options) { super(`Unknown authentication provider "${args[0]}"`, options); } }; var E_LOCAL_SIGNUP_DISALLOWED = class extends HttpResponseException { static status = 403; static code = "E_LOCAL_SIGNUP_DISALLOWED"; /** * Translation identifier. Can be customized */ identifier = "errors.E_LOCAL_SIGNUP_DISALLOWED"; constructor(args, options) { super(`Cannot signup using "${args[0]}". Local signup is disabled for this provider`, options); } }; export { E_OAUTH_MISSING_CODE, E_OAUTH_STATE_MISMATCH, E_UNKNOWN_ALLY_PROVIDER, E_LOCAL_SIGNUP_DISALLOWED, errors_exports };