UNPKG

@akveo/nga-auth

Version:
1,006 lines (992 loc) 64.2 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common'), require('@angular/router'), require('@angular/forms'), require('@angular/http'), require('@akveo/nga-theme'), require('rxjs/add/operator/do'), require('rxjs/BehaviorSubject'), require('rxjs/Observable'), require('rxjs/Subject'), require('rxjs/add/operator/publish'), require('rxjs/add/operator/map'), require('rxjs/add/operator/switchMap'), require('rxjs/add/operator/catch')) : typeof define === 'function' && define.amd ? define(['exports', '@angular/core', '@angular/common', '@angular/router', '@angular/forms', '@angular/http', '@akveo/nga-theme', 'rxjs/add/operator/do', 'rxjs/BehaviorSubject', 'rxjs/Observable', 'rxjs/Subject', 'rxjs/add/operator/publish', 'rxjs/add/operator/map', 'rxjs/add/operator/switchMap', 'rxjs/add/operator/catch'], factory) : (factory((global.nga = global.nga || {}, global.nga.auth = global.nga.auth || {}),global.ng.core,global.ng.common,global.ng.router,global.ng.forms,global.ng.http,global.nga.theme,global.Rx.Observable.prototype,global.Rx,global.Rx,global.Rx)); }(this, (function (exports,_angular_core,_angular_common,_angular_router,_angular_forms,_angular_http,_akveo_ngaTheme,rxjs_add_operator_do,rxjs_BehaviorSubject,rxjs_Observable,rxjs_Subject) { 'use strict'; var ngaAuthOptionsToken = new _angular_core.InjectionToken('NGA_AUTH_OPTIONS'); var ngaAuthProvidersToken = new _angular_core.InjectionToken('NGA_AUTH_OPTIONS'); /** * Extending object that entered in first argument. * * Returns extended object or false if have no target object or incorrect type. * * If you wish to clone source object (without modify it), just use empty new * object as first argument, like this: * deepExtend({}, yourObj_1, [yourObj_N]); */ /** * Extending object that entered in first argument. * * Returns extended object or false if have no target object or incorrect type. * * If you wish to clone source object (without modify it), just use empty new * object as first argument, like this: * deepExtend({}, yourObj_1, [yourObj_N]); */ var deepExtend = function () { var objects = []; for (var _i = 0; _i < arguments.length; _i++) { objects[_i] = arguments[_i]; } if (arguments.length < 1 || typeof arguments[0] !== 'object') { return false; } if (arguments.length < 2) { return arguments[0]; } var target = arguments[0]; // convert arguments to array and cut off target object var args = Array.prototype.slice.call(arguments, 1); var val, src; args.forEach(function (obj) { // skip argument if it is array or isn't object if (typeof obj !== 'object' || Array.isArray(obj)) { return; } Object.keys(obj).forEach(function (key) { src = target[key]; // source value val = obj[key]; // new value // recursion prevention if (val === target) { return; /** * if new value isn't object then just overwrite by new value * instead of extending. */ } else if (typeof val !== 'object' || val === null) { target[key] = val; return; // just clone arrays (and recursive clone objects inside) } else if (Array.isArray(val)) { target[key] = deepCloneArray(val); return; // custom cloning and overwrite for specific objects } else if (isSpecificValue(val)) { target[key] = cloneSpecificValue(val); return; // overwrite by new value if source isn't object or array } else if (typeof src !== 'object' || src === null || Array.isArray(src)) { target[key] = deepExtend({}, val); return; // source value and new value is objects both, extending... } else { target[key] = deepExtend(src, val); return; } }); }); return target; }; function isSpecificValue(val) { return (val instanceof Date || val instanceof RegExp) ? true : false; } function cloneSpecificValue(val) { if (val instanceof Date) { return new Date(val.getTime()); } else if (val instanceof RegExp) { return new RegExp(val); } else { throw new Error('cloneSpecificValue: Unexpected situation'); } } /** * Recursive cloning array. */ function deepCloneArray(arr) { var clone = []; arr.forEach(function (item, index) { if (typeof item === 'object' && item !== null) { if (Array.isArray(item)) { clone[index] = deepCloneArray(item); } else if (isSpecificValue(item)) { clone[index] = cloneSpecificValue(item); } else { clone[index] = deepExtend({}, item); } } else { clone[index] = item; } }); return clone; } // getDeepFromObject({result: {data: 1}}, 'result.data', 2); // returns 1 function getDeepFromObject(object, name, defaultValue) { if (object === void 0) { object = {}; } var keys = name.split('.'); // clone the object var level = deepExtend({}, object || {}); keys.forEach(function (k) { if (level && typeof level[k] !== 'undefined') { level = level[k]; } else { level = undefined; } }); return typeof level === 'undefined' ? defaultValue : level; } var __decorate$2 = (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$1 = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param$1 = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; exports.NgaTokenService = (function () { function NgaTokenService(options) { var _this = this; this.options = options; this.defaultConfig = { token: { key: 'auth_app_token', getter: function () { return rxjs_Observable.Observable.of(localStorage.getItem(_this.getConfigValue('token.key'))); }, setter: function (token) { return rxjs_Observable.Observable.of(token) .do(function (t) { return localStorage.setItem(_this.getConfigValue('token.key'), t); }); }, deleter: function (token) { return rxjs_Observable.Observable.of(token) .do(function (t) { return localStorage.removeItem(_this.getConfigValue('token.key')); }); }, }, }; this.config = {}; this.token$ = new rxjs_BehaviorSubject.BehaviorSubject(null); this.setConfig(options); this.get().subscribe(function (token) { return _this.publishToken(token); }); } NgaTokenService.prototype.setConfig = function (config) { this.config = deepExtend({}, this.defaultConfig, config); }; NgaTokenService.prototype.getConfigValue = function (key) { return getDeepFromObject(this.config, key, null); }; NgaTokenService.prototype.set = function (token) { this.publishToken(token); return this.getConfigValue('token.setter')(token); }; NgaTokenService.prototype.get = function () { return this.getConfigValue('token.getter')(); }; NgaTokenService.prototype.tokenChange = function () { return this.token$.publish().refCount(); }; NgaTokenService.prototype.clear = function () { this.publishToken(null); return this.getConfigValue('token.deleter')(); }; NgaTokenService.prototype.publishToken = function (token) { this.token$.next(token); }; return NgaTokenService; }()); exports.NgaTokenService = __decorate$2([ _angular_core.Injectable(), __param$1(0, _angular_core.Inject(ngaAuthOptionsToken)), __metadata$1("design:paramtypes", [Object]) ], exports.NgaTokenService); var __decorate$1 = (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); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; /** * @license * Copyright Akveo. All Rights Reserved. * Licensed under the MIT License. See License.txt in the project root for license information. */ var NgaAuthResult = (function () { // TODO pass arguments in options object function NgaAuthResult(success, response, redirect, errors, messages, token) { this.success = success; this.response = response; this.redirect = redirect; this.errors = []; this.messages = []; this.errors = this.errors.concat([errors]); if (errors instanceof Array) { this.errors = errors; } this.messages = this.messages.concat([messages]); if (messages instanceof Array) { this.messages = messages; } this.token = token; } NgaAuthResult.prototype.getResponse = function () { return this.response; }; NgaAuthResult.prototype.getTokenValue = function () { return this.token; }; NgaAuthResult.prototype.getRedirect = function () { return this.redirect; }; NgaAuthResult.prototype.getErrors = function () { return this.errors.filter(function (val) { return !!val; }); }; NgaAuthResult.prototype.getMessages = function () { return this.messages.filter(function (val) { return !!val; }); }; NgaAuthResult.prototype.isSuccess = function () { return this.success; }; NgaAuthResult.prototype.isFailure = function () { return !this.success; }; return NgaAuthResult; }()); exports.NgaAuthService = (function () { function NgaAuthService(tokenService, providers) { if (providers === void 0) { providers = {}; } this.tokenService = tokenService; this.providers = providers; } /** * Retrieves current authenticated token stored * @returns {Observable<any>} */ NgaAuthService.prototype.getToken = function () { return this.tokenService.get(); }; /** * Returns true if auth token is presented in the token storage * @returns {Observable<any>} */ NgaAuthService.prototype.isAuthenticated = function () { return this.getToken().map(function (token) { return !!token; }); }; /** * Returns tokens stream * @returns {Observable<any>} */ NgaAuthService.prototype.onTokenChange = function () { return this.tokenService.tokenChange(); }; /** * Returns authentication status stream * @returns {Observable<any>} */ NgaAuthService.prototype.onAuthenticationChange = function () { return this.onTokenChange().map(function (token) { return !!token; }); }; /** * Authenticates with the selected provider * Stores received token in the token storage * * Example: * authenticate('email', {email: 'email@example.com', password: 'test'}) * * @param provider * @param data * @returns {Observable<NgaAuthResult>} */ NgaAuthService.prototype.authenticate = function (provider, data) { var _this = this; return this.getProvider(provider).authenticate(data) .do(function (result) { if (result.isSuccess() && result.getTokenValue()) { _this.tokenService.set(result.getTokenValue()).subscribe(function () { }); } }); }; /** * Registers with the selected provider * Stores received token in the token storage * * Example: * register('email', {email: 'email@example.com', name: 'Some Name', password: 'test'}) * * @param provider * @param data * @returns {Observable<NgaAuthResult>} */ NgaAuthService.prototype.register = function (provider, data) { var _this = this; return this.getProvider(provider).register(data) .do(function (result) { if (result.isSuccess() && result.getTokenValue()) { _this.tokenService.set(result.getTokenValue()).subscribe(function () { }); } }); }; /** * Sign outs with the selected provider * Removes token from the token storage * * Example: * logout('email') * * @param provider * @returns {Observable<NgaAuthResult>} */ NgaAuthService.prototype.logout = function (provider) { var _this = this; return this.getProvider(provider).logout() .do(function (result) { if (result.isSuccess()) { _this.tokenService.clear().subscribe(function () { }); } }); }; /** * Sends forgot password request to the selected provider * * Example: * requestPassword('email', {email: 'email@example.com'}) * * @param provider * @param data * @returns {Observable<NgaAuthResult>} */ NgaAuthService.prototype.requestPassword = function (provider, data) { return this.getProvider(provider).requestPassword(data); }; /** * Tries to reset password with the selected provider * * Example: * resetPassword('email', {newPassword: 'test'}) * * @param provider * @param data * @returns {Observable<NgaAuthResult>} */ NgaAuthService.prototype.resetPassword = function (provider, data) { return this.getProvider(provider).resetPassword(data); }; NgaAuthService.prototype.getProvider = function (provider) { if (!this.providers[provider]) { throw new TypeError("Nga auth provider '" + provider + "' is not registered"); } return this.providers[provider].object; }; return NgaAuthService; }()); exports.NgaAuthService = __decorate$1([ _angular_core.Injectable(), __param(1, _angular_core.Optional()), __param(1, _angular_core.Inject(ngaAuthProvidersToken)), __metadata("design:paramtypes", [exports.NgaTokenService, Object]) ], exports.NgaAuthService); var NgaAbstractAuthProvider = (function () { function NgaAbstractAuthProvider() { this.defaultConfig = {}; this.config = {}; } NgaAbstractAuthProvider.prototype.setConfig = function (config) { this.config = deepExtend({}, this.defaultConfig, config); }; NgaAbstractAuthProvider.prototype.getConfigValue = function (key) { return getDeepFromObject(this.config, key, null); }; NgaAbstractAuthProvider.prototype.createFailResponse = function (data) { return new _angular_http.Response(new _angular_http.ResponseOptions({ body: '{}', status: 401 })); }; NgaAbstractAuthProvider.prototype.createSuccessResponse = function (data) { return new _angular_http.Response(new _angular_http.ResponseOptions({ body: '{}', status: 200 })); }; NgaAbstractAuthProvider.prototype.getJsonSafe = function (res) { var json; try { json = res.json(); } catch (e) { json = {}; } return json; }; return NgaAbstractAuthProvider; }()); 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$3 = (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; }; exports.NgaDummyAuthProvider = (function (_super) { __extends(NgaDummyAuthProvider, _super); function NgaDummyAuthProvider() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.defaultConfig = { delay: 1000, }; return _this; } NgaDummyAuthProvider.prototype.authenticate = function (data) { return rxjs_Observable.Observable.of(this.createDummyResult(data)) .delay(this.getConfigValue('delay')); }; NgaDummyAuthProvider.prototype.register = function (data) { return rxjs_Observable.Observable.of(this.createDummyResult(data)) .delay(this.getConfigValue('delay')); }; NgaDummyAuthProvider.prototype.requestPassword = function (data) { return rxjs_Observable.Observable.of(this.createDummyResult(data)) .delay(this.getConfigValue('delay')); }; NgaDummyAuthProvider.prototype.resetPassword = function (data) { return rxjs_Observable.Observable.of(this.createDummyResult(data)) .delay(this.getConfigValue('delay')); }; NgaDummyAuthProvider.prototype.logout = function (data) { return rxjs_Observable.Observable.of(this.createDummyResult(data)) .delay(this.getConfigValue('delay')); }; NgaDummyAuthProvider.prototype.createDummyResult = function (data) { if (this.getConfigValue('alwaysFail')) { return new NgaAuthResult(false, this.createFailResponse(data), null, ['Something went wrong.']); } return new NgaAuthResult(true, '/', this.createSuccessResponse(data), ['Successfully logged in.']); }; return NgaDummyAuthProvider; }(NgaAbstractAuthProvider)); exports.NgaDummyAuthProvider = __decorate$3([ _angular_core.Injectable() ], exports.NgaDummyAuthProvider); var __extends$1 = (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$4 = (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$2 = (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. */ exports.NgaEmailPassAuthProvider = (function (_super) { __extends$1(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 _angular_http.Response) { errors = _this.getConfigValue('errors.getter')('login', res); } else { errors.push('Something went wrong.'); } return rxjs_Observable.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 _angular_http.Response) { errors = _this.getConfigValue('errors.getter')('register', res); } else { errors.push('Something went wrong.'); } return rxjs_Observable.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 _angular_http.Response) { errors = _this.getConfigValue('errors.getter')('requestPass', res); } else { errors.push('Something went wrong.'); } return rxjs_Observable.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 _angular_http.Response) { errors = _this.getConfigValue('errors.getter')('resetPass', res); } else { errors.push('Something went wrong.'); } return rxjs_Observable.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 _angular_http.Response) { errors = _this.getConfigValue('errors.getter')('logout', res); } else { errors.push('Something went wrong.'); } return rxjs_Observable.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)); exports.NgaEmailPassAuthProvider = __decorate$4([ _angular_core.Injectable(), __metadata$2("design:paramtypes", [_angular_http.Http, _angular_router.ActivatedRoute]) ], exports.NgaEmailPassAuthProvider); var __decorate$5 = (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$3 = (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. */ exports.NgaAuthComponent = (function () { // showcase of how to use the onAuthenticationChange method function NgaAuthComponent(auth) { var _this = this; this.auth = auth; this.authenticated = false; this.token = ''; this.subscription = auth.onAuthenticationChange() .subscribe(function (authenticated) { _this.authenticated = authenticated; }); } NgaAuthComponent.prototype.ngOnDestroy = function () { this.subscription.unsubscribe(); }; return NgaAuthComponent; }()); exports.NgaAuthComponent = __decorate$5([ _angular_core.Component({ selector: 'nga-auth', template: "\n <nga-layout>\n <nga-layout-column>\n <nga-auth-block></nga-auth-block>\n </nga-layout-column>\n </nga-layout>\n ", }), __metadata$3("design:paramtypes", [exports.NgaAuthService]) ], exports.NgaAuthComponent); var __decorate$6 = (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$4 = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; exports.NgaSecuredHttp = (function () { function NgaSecuredHttp(injector, http) { this.injector = injector; this.http = http; this.onErrorSubject = new rxjs_Subject.Subject(); } NgaSecuredHttp.prototype.get = function (url, options) { var _this = this; if (options === void 0) { options = {}; } return this.doRequest(this.prepareOptions(options) .switchMap(function (newOptions) { return _this.http.get(url, newOptions); })); }; NgaSecuredHttp.prototype.post = function (url, body, options) { var _this = this; if (options === void 0) { options = {}; } return this.doRequest(this.prepareOptions(options) .switchMap(function (newOptions) { return _this.http.post(url, body, newOptions); })); }; NgaSecuredHttp.prototype.put = function (url, body, options) { var _this = this; if (options === void 0) { options = {}; } return this.doRequest(this.prepareOptions(options) .switchMap(function (newOptions) { return _this.http.put(url, body, newOptions); })); }; NgaSecuredHttp.prototype.delete = function (url, options) { var _this = this; if (options === void 0) { options = {}; } return this.doRequest(this.prepareOptions(options) .switchMap(function (newOptions) { return _this.http.delete(url, newOptions); })); }; NgaSecuredHttp.prototype.onRequestError = function () { return this.onErrorSubject.publish().refCount(); }; NgaSecuredHttp.prototype.prepareOptions = function (options) { var _this = this; if (options === void 0) { options = {}; } return this.authService.getToken() .map(function (token) { options.headers = options.headers || new _angular_http.Headers(); _this.addAuthTokenHeader(options.headers, token); return options; }); }; NgaSecuredHttp.prototype.doRequest = function (requestObservable) { var _this = this; return requestObservable .catch(function (err) { _this.onErrorSubject.next(err); throw err; }); }; NgaSecuredHttp.prototype.addAuthTokenHeader = function (headers, token) { headers.append('Authorization', "Bearer " + token); }; Object.defineProperty(NgaSecuredHttp.prototype, "authService", { get: function () { return this.injector.get(exports.NgaAuthService); }, enumerable: true, configurable: true }); return NgaSecuredHttp; }()); exports.NgaSecuredHttp = __decorate$6([ _angular_core.Injectable(), __metadata$4("design:paramtypes", [_angular_core.Injector, _angular_http.Http]) ], exports.NgaSecuredHttp); var __decorate$7 = (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; }; /** * @license * Copyright Akveo. All Rights Reserved. * Licensed under the MIT License. See License.txt in the project root for license information. */ exports.NgaAuthBlockComponent = (function () { function NgaAuthBlockComponent() { } return NgaAuthBlockComponent; }()); exports.NgaAuthBlockComponent = __decorate$7([ _angular_core.Component({ selector: 'nga-auth-block', styles: [":host .auth-block{margin:0 auto}:host .auth-block /deep/ .checkbox{display:flex;justify-content:space-between;margin-bottom:10px;font-weight:normal}:host .auth-block /deep/ .form-control{position:relative;height:auto;padding:10px}:host .auth-block /deep/ .form-control:focus{z-index:2}:host .auth-block /deep/ input.middle{border-radius:0;margin:-1px 0}:host .auth-block /deep/ input.first{margin-bottom:-1px;border-bottom-right-radius:0;border-bottom-left-radius:0}:host .auth-block /deep/ input.last{margin-bottom:10px;border-top-left-radius:0;border-top-right-radius:0}:host .auth-block /deep/ .links{padding-top:2rem} "], template: "\n <div class=\"auth-block\">\n <router-outlet></router-outlet>\n </div>\n ", }) ], exports.NgaAuthBlockComponent); var NgaUser = (function () { function NgaUser(id, email, password, rememberMe, terms, confirmPassword, fullName) { this.id = id; this.email = email; this.password = password; this.rememberMe = rememberMe; this.terms = terms; this.confirmPassword = confirmPassword; this.fullName = fullName; } return NgaUser; }()); var __decorate$8 = (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$5 = (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. */ exports.NgaLoginComponent = (function () { function NgaLoginComponent(service, router) { this.service = service; this.router = router; this.redirectDelay = 1500; this.submitted = false; this.errors = []; this.messages = []; this.user = new NgaUser(); } NgaLoginComponent.prototype.login = function (provider) { var _this = this; this.errors = this.messages = []; this.submitted = true; this.service.authenticate(provider, this.user).subscribe(function (result) { _this.submitted = false; if (result.isSuccess()) { _this.messages = result.getMessages(); } else { _this.errors = result.getErrors(); } var redirect = result.getRedirect(); if (redirect) { setTimeout(function () { return _this.router.navigateByUrl(redirect); }, _this.redirectDelay); } }); }; NgaLoginComponent.prototype.getConfigValue = function (provider, key) { return this.service.getProvider(provider).getConfigValue(key); }; return NgaLoginComponent; }()); exports.NgaLoginComponent = __decorate$8([ _angular_core.Component({ selector: 'nga-login', styles: [""], template: "\n <h2>Please sign in</h2>\n <form (ngSubmit)=\"login('email')\" #loginForm=\"ngForm\">\n\n <div *ngIf=\"errors && errors.length > 0 && !submitted\" class=\"alert alert-danger\" role=\"alert\">\n <div><strong>Oh snap!</strong></div>\n <div *ngFor=\"let error of errors\">{{ error }}</div>\n </div>\n <div *ngIf=\"messages && messages.length > 0 && !submitted\" class=\"alert alert-success\" role=\"alert\">\n <div><strong>Hooray!</strong></div>\n <div *ngFor=\"let message of messages\">{{ message }}</div>\n </div>\n\n <label for=\"input-email\" class=\"sr-only\">Email address</label>\n <input name=\"email\" [(ngModel)]=\"user.email\" type=\"email\" id=\"input-email\"\n class=\"form-control form-control-lg first\" placeholder=\"Email address\"\n [required]=\"getConfigValue('email', 'validation.email.required')\"\n autofocus>\n\n <label for=\"input-password\" class=\"sr-only\">Password</label>\n <input name=\"password\" [(ngModel)]=\"user.password\" type=\"password\" id=\"input-password\"\n class=\"form-control form-control-lg last\" placeholder=\"Password\"\n [required]=\"getConfigValue('email', 'validation.password.required')\"\n [minlength]=\"getConfigValue('email', 'validation.password.minLength')\"\n [maxlength]=\"getConfigValue('email', 'validation.password.maxLength')\">\n\n <div class=\"checkbox\" *ngIf=\"getConfigValue('email', 'login.rememberMe')\">\n <label>\n <input name=\"rememberMe\" [(ngModel)]=\"user.rememberMe\" type=\"checkbox\" value=\"remember-me\"> Remember me\n </label>\n <a routerLink=\"../request-password\">Forgot Password</a>\n </div>\n <button [disabled]=\"submitted || !loginForm.form.valid\"\n class=\"btn btn-lg btn-primary btn-block\" type=\"submit\">Sign in</button>\n </form>\n\n <div class=\"links\">\n Don't have an account? <a routerLink=\"../register\">Register</a>\n </div>\n ", }), __metadata$5("design:paramtypes", [exports.NgaAuthService, _angular_router.Router]) ], exports.NgaLoginComponent); var __decorate$9 = (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$6 = (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. */ exports.NgaRegisterComponent = (function () { function NgaRegisterComponent(service, tokenService, router) { this.service = service; this.tokenService = tokenService; this.router = router; this.redirectDelay = 1500; this.submitted = false; this.errors = []; this.messages = []; this.user = new NgaUser(); } NgaRegisterComponent.prototype.register = function (provider) { var _this = this; this.errors = this.messages = []; this.submitted = true; this.service.register(provider, this.user).subscribe(function (result) { _this.submitted = false; if (result.isSuccess()) { _this.messages = result.getMessages(); } else { _this.errors = result.getErrors(); } var redirect = result.getRedirect(); if (redirect) { setTimeout(function () { return _this.router.navigateByUrl(redirect); }, _this.redirectDelay); } }); }; NgaRegisterComponent.prototype.getConfigValue = function (provider, key) { return this.service.getProvider(provider).getConfigValue(key); }; return NgaRegisterComponent; }()); exports.NgaRegisterComponent = __decorate$9([ _angular_core.Component({ selector: 'nga-register', styles: [""], template: "\n <h2>Create new account</h2>\n <form (ngSubmit)=\"register('email')\" #registerForm=\"ngForm\">\n <div *ngIf=\"errors && errors.length > 0 && !submitted\" class=\"alert alert-danger\" role=\"alert\">\n <div><strong>Oh snap!</strong></div>\n <div *ngFor=\"let error of errors\">{{ error }}</div>\n </div>\n <div *ngIf=\"messages && messages.length > 0 && !submitted\" class=\"alert alert-success\" role=\"alert\">\n <div><strong>Hooray!</strong></div>\n <div *ngFor=\"let message of messages\">{{ message }}</div>\n </div>\n\n <label for=\"input-name\" class=\"sr-only\">Full name</label>\n <input name=\"fullName\" [(ngModel)]=\"user.fullName\" type=\"text\" id=\"input-name\"\n class=\"form-control form-control-lg first\" placeholder=\"Full name\"\n [required]=\"getConfigValue('email', 'validation.fullName.required')\"\n [minlength]=\"getConfigValue('email', 'validation.fullName.minLength')\"\n [maxlength]=\"getConfigValue('email', 'validation.fullName.maxLength')\"\n autofocus>\n\n <label for=\"input-email\" class=\"sr-only\">Email address</label>\n <input name=\"email\" [(ngModel)]=\"user.email\" type=\"email\" id=\"input-email\"\n class=\"form-control form-control-lg middle\" placeholder=\"Email address\"\n [required]=\"getConfigValue('email', 'validation.email.required')\">\n\n <label for=\"input-password\" class=\"sr-only\">Password</label>\n <input name=\"password\" [(ngModel)]=\"user.password\" type=\"password\" id=\"input-password\"\n class=\"form-control form-control-lg middle\" placeholder=\"Password\"\n [required]=\"getConfigValue('email', 'validation.password.required')\"\n [minlength]=\"getConfigValue('email', 'validation.password.minLength')\"\n [maxlength]=\"getConfigValue('email', 'validation.password.maxLength')\">\n\n <label for=\"input-re-password\" class=\"sr-only\">Repeat password</label>\n <input name=\"confirmPassword\" [(ngModel)]=\"user.confirmPassword\" type=\"password\" id=\"input-re-password\"\n class=\"form-control form-control-lg last\" placeholder=\"Confirm Password\"\n [required]=\"getConfigValue('email', 'validation.password.required')\"\n [minlength]=\"getConfigValue('email', 'validation.password.minLength')\"\n [maxlength]=\"getConfigValue('email', 'validation.password.maxLength')\">\n\n <div class=\"checkbox\" *ngIf=\"getConfigValue('email', 'register.terms')\">\n <label>\n <input name=\"rememberMe\" [(ngModel)]=\"user.terms\"\n type=\"checkbox\" value=\"remember-me\"> Agree to <a href=\"#\" target=\"_blank\">Terms & Conditions</a>\n </label>\n </div>\n <button [disabled]=\"submitted || !registerForm.form.valid\"\n class=\"btn btn-lg btn-primary btn-block\" type=\"submit\">Register</button>\n </form>\n\n <div class=\"links\">\n Already have an account? <a routerLink=\"../login\">Sign in</a>\n </div>\n ", }), __metadata$6("design:paramtypes", [exports.NgaAuthService, exports.NgaTokenService, _angular_router.Router]) ], exports.NgaRegisterComponent); var __decorate$10 = (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$7 = (this && this.__metadata) || f