UNPKG

@nebular/auth

Version:
276 lines 10.9 kB
/** * @license * Copyright Akveo. All Rights Reserved. * Licensed under the MIT License. See License.txt in the project root for license information. */ import { Injectable } from '@angular/core'; import { HttpClient, HttpErrorResponse } from '@angular/common/http'; import { ActivatedRoute } from '@angular/router'; import { of as observableOf } from 'rxjs'; import { switchMap, map, catchError } from 'rxjs/operators'; import { NbAuthResult } from '../../services/auth-result'; import { NbAuthStrategy } from '../auth-strategy'; import { passwordStrategyOptions } from './password-strategy-options'; import { NbAuthIllegalTokenError } from '../../services/token/token'; /** * The most common authentication provider for email/password strategy. * * Strategy settings. Note, there is no need to copy over the whole object to change the settings you need. * Also, this.getOption call won't work outside of the default options declaration * (which is inside of the `NbPasswordAuthStrategy` class), so you have to replace it with a custom helper function * if you need it. * * ```ts *export class NbPasswordAuthStrategyOptions extends NbAuthStrategyOptions { * name: string; * baseEndpoint? = '/api/auth/'; * login?: boolean | NbPasswordStrategyModule = { * alwaysFail: false, * endpoint: 'login', * method: 'post', * requireValidToken: true, * redirect: { * success: '/', * failure: null, * }, * defaultErrors: ['Login/Email combination is not correct, please try again.'], * defaultMessages: ['You have been successfully logged in.'], * }; * register?: boolean | NbPasswordStrategyModule = { * alwaysFail: false, * endpoint: 'register', * method: 'post', * requireValidToken: true, * redirect: { * success: '/', * failure: null, * }, * defaultErrors: ['Something went wrong, please try again.'], * defaultMessages: ['You have been successfully registered.'], * }; * requestPass?: boolean | NbPasswordStrategyModule = { * endpoint: 'request-pass', * method: 'post', * redirect: { * success: '/', * failure: null, * }, * defaultErrors: ['Something went wrong, please try again.'], * defaultMessages: ['Reset password instructions have been sent to your email.'], * }; * resetPass?: boolean | NbPasswordStrategyReset = { * endpoint: 'reset-pass', * method: 'put', * redirect: { * success: '/', * failure: null, * }, * resetPasswordTokenKey: 'reset_password_token', * defaultErrors: ['Something went wrong, please try again.'], * defaultMessages: ['Your password has been successfully changed.'], * }; * logout?: boolean | NbPasswordStrategyReset = { * alwaysFail: false, * endpoint: 'logout', * method: 'delete', * redirect: { * success: '/', * failure: null, * }, * defaultErrors: ['Something went wrong, please try again.'], * defaultMessages: ['You have been successfully logged out.'], * }; * refreshToken?: boolean | NbPasswordStrategyModule = { * endpoint: 'refresh-token', * method: 'post', * requireValidToken: true, * redirect: { * success: null, * failure: null, * }, * defaultErrors: ['Something went wrong, please try again.'], * defaultMessages: ['Your token has been successfully refreshed.'], * }; * token?: NbPasswordStrategyToken = { * class: NbAuthSimpleToken, * key: 'data.token', * getter: (module: string, res: HttpResponse<Object>, options: NbPasswordAuthStrategyOptions) => getDeepFromObject( * res.body, * options.token.key, * ), * }; * errors?: NbPasswordStrategyMessage = { * key: 'data.errors', * getter: (module: string, res: HttpErrorResponse, options: NbPasswordAuthStrategyOptions) => getDeepFromObject( * res.error, * options.errors.key, * options[module].defaultErrors, * ), * }; * messages?: NbPasswordStrategyMessage = { * key: 'data.messages', * getter: (module: string, res: HttpResponse<Object>, options: NbPasswordAuthStrategyOptions) => getDeepFromObject( * res.body, * options.messages.key, * options[module].defaultMessages, * ), * }; * validation?: { * password?: { * required?: boolean; * minLength?: number | null; * maxLength?: number | null; * regexp?: string | null; * }; * email?: { * required?: boolean; * regexp?: string | null; * }; * fullName?: { * required?: boolean; * minLength?: number | null; * maxLength?: number | null; * regexp?: string | null; * }; * }; *} * ``` */ export class NbPasswordAuthStrategy extends NbAuthStrategy { constructor(http, route) { super(); this.http = http; this.route = route; this.defaultOptions = passwordStrategyOptions; } static setup(options) { return [NbPasswordAuthStrategy, options]; } authenticate(data) { const module = 'login'; const method = this.getOption(`${module}.method`); const url = this.getActionEndpoint(module); const requireValidToken = this.getOption(`${module}.requireValidToken`); return this.http.request(method, url, { body: data, observe: 'response' }) .pipe(map((res) => { if (this.getOption(`${module}.alwaysFail`)) { throw this.createFailResponse(data); } return res; }), map((res) => { return new NbAuthResult(true, res, this.getOption(`${module}.redirect.success`), [], this.getOption('messages.getter')(module, res, this.options), this.createToken(this.getOption('token.getter')(module, res, this.options), requireValidToken)); }), catchError((res) => { return this.handleResponseError(res, module); })); } register(data) { const module = 'register'; const method = this.getOption(`${module}.method`); const url = this.getActionEndpoint(module); const requireValidToken = this.getOption(`${module}.requireValidToken`); return this.http.request(method, url, { body: data, observe: 'response' }) .pipe(map((res) => { if (this.getOption(`${module}.alwaysFail`)) { throw this.createFailResponse(data); } return res; }), map((res) => { return new NbAuthResult(true, res, this.getOption(`${module}.redirect.success`), [], this.getOption('messages.getter')(module, res, this.options), this.createToken(this.getOption('token.getter')('login', res, this.options), requireValidToken)); }), catchError((res) => { return this.handleResponseError(res, module); })); } requestPassword(data) { const module = 'requestPass'; const method = this.getOption(`${module}.method`); const url = this.getActionEndpoint(module); return this.http.request(method, url, { body: data, observe: 'response' }) .pipe(map((res) => { if (this.getOption(`${module}.alwaysFail`)) { throw this.createFailResponse(); } return res; }), map((res) => { return new NbAuthResult(true, res, this.getOption(`${module}.redirect.success`), [], this.getOption('messages.getter')(module, res, this.options)); }), catchError((res) => { return this.handleResponseError(res, module); })); } resetPassword(data = {}) { const module = 'resetPass'; const method = this.getOption(`${module}.method`); const url = this.getActionEndpoint(module); const tokenKey = this.getOption(`${module}.resetPasswordTokenKey`); data[tokenKey] = this.route.snapshot.queryParams[tokenKey]; return this.http.request(method, url, { body: data, observe: 'response' }) .pipe(map((res) => { if (this.getOption(`${module}.alwaysFail`)) { throw this.createFailResponse(); } return res; }), map((res) => { return new NbAuthResult(true, res, this.getOption(`${module}.redirect.success`), [], this.getOption('messages.getter')(module, res, this.options)); }), catchError((res) => { return this.handleResponseError(res, module); })); } logout() { const module = 'logout'; const method = this.getOption(`${module}.method`); const url = this.getActionEndpoint(module); return observableOf({}) .pipe(switchMap((res) => { if (!url) { return observableOf(res); } return this.http.request(method, url, { observe: 'response' }); }), map((res) => { if (this.getOption(`${module}.alwaysFail`)) { throw this.createFailResponse(); } return res; }), map((res) => { return new NbAuthResult(true, res, this.getOption(`${module}.redirect.success`), [], this.getOption('messages.getter')(module, res, this.options)); }), catchError((res) => { return this.handleResponseError(res, module); })); } refreshToken(data) { const module = 'refreshToken'; const method = this.getOption(`${module}.method`); const url = this.getActionEndpoint(module); const requireValidToken = this.getOption(`${module}.requireValidToken`); return this.http.request(method, url, { body: data, observe: 'response' }) .pipe(map((res) => { if (this.getOption(`${module}.alwaysFail`)) { throw this.createFailResponse(data); } return res; }), map((res) => { return new NbAuthResult(true, res, this.getOption(`${module}.redirect.success`), [], this.getOption('messages.getter')(module, res, this.options), this.createToken(this.getOption('token.getter')(module, res, this.options), requireValidToken)); }), catchError((res) => { return this.handleResponseError(res, module); })); } handleResponseError(res, module) { let errors = []; if (res instanceof HttpErrorResponse) { errors = this.getOption('errors.getter')(module, res, this.options); } else if (res instanceof NbAuthIllegalTokenError) { errors.push(res.message); } else { errors.push('Something went wrong.'); } return observableOf(new NbAuthResult(false, res, this.getOption(`${module}.redirect.failure`), errors)); } } NbPasswordAuthStrategy.decorators = [ { type: Injectable } ]; NbPasswordAuthStrategy.ctorParameters = () => [ { type: HttpClient }, { type: ActivatedRoute } ]; //# sourceMappingURL=password-strategy.js.map