UNPKG

@akveo/nga-auth

Version:
251 lines 11.5 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; /** * @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 { Http, Response } from '@angular/http'; import { ActivatedRoute } from '@angular/router'; import { Observable } from 'rxjs/Observable'; import { NgaAuthResult } from '../services/auth.service'; import { NgaAbstractAuthProvider } from './abstract-auth.provider'; import { getDeepFromObject } from '../helpers'; var NgaEmailPassAuthProvider = (function (_super) { __extends(NgaEmailPassAuthProvider, _super); function NgaEmailPassAuthProvider(http, route) { var _this = _super.call(this) || this; _this.http = http; _this.route = route; _this.defaultConfig = { baseEndpoint: '', login: { alwaysFail: false, rememberMe: true, endpoint: '/api/auth/login', redirect: { success: '/', failure: null, }, defaultErrors: ['Login/Email combination is not correct, please try again.'], defaultMessages: ['You have been successfully logged in.'], }, register: { alwaysFail: false, rememberMe: true, endpoint: '/api/auth/register', redirect: { success: '/', failure: null, }, defaultErrors: ['Something went wrong, please try again.'], defaultMessages: ['You have been successfully registered.'], }, logout: { alwaysFail: false, endpoint: '/api/auth/logout', redirect: { success: '/', failure: null, }, defaultErrors: ['Something went wrong, please try again.'], defaultMessages: ['You have been successfully logged out.'], }, requestPass: { endpoint: '/api/auth/request-pass', redirect: { success: '/', failure: null, }, defaultErrors: ['Something went wrong, please try again.'], defaultMessages: ['Reset password instructions have been sent to your email.'], }, resetPass: { endpoint: '/api/auth/reset-pass', redirect: { success: '/', failure: null, }, resetPasswordTokenKey: 'reset_password_token', defaultErrors: ['Something went wrong, please try again.'], defaultMessages: ['Your password has been successfully changed.'], }, token: { key: 'data.token', getter: function (module, res) { return getDeepFromObject(_this.getJsonSafe(res), _this.getConfigValue('token.key')); }, }, errors: { key: 'data.errors', getter: function (module, res) { return getDeepFromObject(_this.getJsonSafe(res), _this.getConfigValue('errors.key'), _this.getConfigValue(module + ".defaultErrors")); }, }, messages: { key: 'data.messages', getter: function (module, res) { return getDeepFromObject(_this.getJsonSafe(res), _this.getConfigValue('messages.key'), _this.getConfigValue(module + ".defaultMessages")); }, }, validation: { password: { required: true, minLength: 4, maxLength: 30, }, email: { required: true, }, fullName: { required: false, minLength: 4, maxLength: 50, }, }, }; return _this; } NgaEmailPassAuthProvider.prototype.authenticate = function (data) { var _this = this; return this.http.post(this.getActionEndpoint('login'), data) .map(function (res) { if (_this.getConfigValue('login.alwaysFail')) { throw _this.createFailResponse(data); } return res; }) .map(function (res) { return new NgaAuthResult(true, res, _this.getConfigValue('login.redirect.success'), [], _this.getConfigValue('messages.getter')('login', res), _this.getConfigValue('token.getter')('login', res)); }) .catch(function (res) { var errors = []; if (res instanceof Response) { errors = _this.getConfigValue('errors.getter')('login', res); } else { errors.push('Something went wrong.'); } return Observable.of(new NgaAuthResult(false, res, _this.getConfigValue('login.redirect.failure'), errors)); }); }; NgaEmailPassAuthProvider.prototype.register = function (data) { var _this = this; return this.http.post(this.getActionEndpoint('register'), data) .map(function (res) { if (_this.getConfigValue('register.alwaysFail')) { throw _this.createFailResponse(data); } return res; }) .map(function (res) { return new NgaAuthResult(true, res, _this.getConfigValue('register.redirect.success'), [], _this.getConfigValue('messages.getter')('register', res), _this.getConfigValue('token.getter')('register', res)); }) .catch(function (res) { var errors = []; if (res instanceof Response) { errors = _this.getConfigValue('errors.getter')('register', res); } else { errors.push('Something went wrong.'); } return Observable.of(new NgaAuthResult(false, res, _this.getConfigValue('register.redirect.failure'), errors)); }); }; NgaEmailPassAuthProvider.prototype.requestPassword = function (data) { var _this = this; return this.http.post(this.getActionEndpoint('requestPass'), data) .map(function (res) { if (_this.getConfigValue('requestPass.alwaysFail')) { throw _this.createFailResponse(); } return res; }) .map(function (res) { return new NgaAuthResult(true, res, _this.getConfigValue('requestPass.redirect.success'), [], _this.getConfigValue('messages.getter')('requestPass', res)); }) .catch(function (res) { var errors = []; if (res instanceof Response) { errors = _this.getConfigValue('errors.getter')('requestPass', res); } else { errors.push('Something went wrong.'); } return Observable.of(new NgaAuthResult(false, res, _this.getConfigValue('requestPass.redirect.failure'), errors)); }); }; NgaEmailPassAuthProvider.prototype.resetPassword = function (data) { var _this = this; if (data === void 0) { data = {}; } var tokenKey = this.getConfigValue('resetPass.resetPasswordTokenKey'); data[tokenKey] = this.route.snapshot.queryParams[tokenKey]; return this.http.post(this.getActionEndpoint('resetPass'), data) .map(function (res) { if (_this.getConfigValue('resetPass.alwaysFail')) { throw _this.createFailResponse(); } return res; }) .map(function (res) { return new NgaAuthResult(true, res, _this.getConfigValue('resetPass.redirect.success'), [], _this.getConfigValue('messages.getter')('resetPass', res)); }) .catch(function (res) { var errors = []; if (res instanceof Response) { errors = _this.getConfigValue('errors.getter')('resetPass', res); } else { errors.push('Something went wrong.'); } return Observable.of(new NgaAuthResult(false, res, _this.getConfigValue('resetPass.redirect.failure'), errors)); }); }; NgaEmailPassAuthProvider.prototype.logout = function () { var _this = this; return this.http.delete(this.getActionEndpoint('logout')) .map(function (res) { if (_this.getConfigValue('logout.alwaysFail')) { throw _this.createFailResponse(); } return res; }) .map(function (res) { return new NgaAuthResult(true, res, _this.getConfigValue('logout.redirect.success'), [], _this.getConfigValue('messages.getter')('logout', res)); }) .catch(function (res) { var errors = []; if (res instanceof Response) { errors = _this.getConfigValue('errors.getter')('logout', res); } else { errors.push('Something went wrong.'); } return Observable.of(new NgaAuthResult(false, res, _this.getConfigValue('logout.redirect.failure'), errors)); }); }; NgaEmailPassAuthProvider.prototype.getActionEndpoint = function (action) { var actionEndpoint = this.getConfigValue(action + ".endpoint"); var baseEndpoint = this.getConfigValue('baseEndpoint'); return baseEndpoint + actionEndpoint; }; return NgaEmailPassAuthProvider; }(NgaAbstractAuthProvider)); NgaEmailPassAuthProvider = __decorate([ Injectable(), __metadata("design:paramtypes", [Http, ActivatedRoute]) ], NgaEmailPassAuthProvider); export { NgaEmailPassAuthProvider }; //# sourceMappingURL=email-pass-auth.provider.js.map