UNPKG

nakedobjects.spa

Version:

Single Page Application client for a Naked Objects application.

142 lines 6.38 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); }; import { Injectable } from '@angular/core'; import { tokenNotExpired } from 'angular2-jwt'; import { Router, NavigationStart } from '@angular/router'; import { UrlManagerService } from './url-manager.service'; import { LoggerService } from './logger.service'; import { ConfigService } from './config.service'; import Auth0Lock from 'auth0-lock'; var AuthService = (function () { function AuthService() { } return AuthService; }()); export { AuthService }; var Auth0AuthService = (function (_super) { __extends(Auth0AuthService, _super); function Auth0AuthService(router, urlManager, logger, configService) { var _this = _super.call(this) || this; _this.router = router; _this.urlManager = urlManager; _this.logger = logger; _this.configService = configService; _this.pendingAuthenticate = false; var clientId = configService.config.authClientId; var domain = configService.config.authDomain; var authenticate = configService.config.authenticate; if (authenticate && clientId && domain) { var options = { auth: { params: { scope: 'openid email' } } }; // Configure Auth0 // this is client id which is public _this.lock = new Auth0Lock(clientId, domain, options); // Add callback for lock `authenticated` event _this.lock.on("authenticated", function (authResult) { return localStorage.setItem('id_token', authResult.idToken); }); _this .router .events .filter(function (event) { return event instanceof NavigationStart; }) .filter(function (event) { return (/access_token|id_token|error/).test(event.url); }) .subscribe(function () { _this.lock.resumeAuth(window.location.hash, function (error, authResult) { if (error) { logger.error(error); } else if (authResult && authResult.idToken) { // some sort of race here with token response navigating us to a page, // we're making auth OK with token but app.component doesn't yet have router-outlet // so we see errors. Set the pending Authenticate flag which will make it look like // we're not authenticated and then clear and route home on next event loop. _this.pendingAuthenticate = true; localStorage.setItem('id_token', authResult.idToken); setTimeout(function () { _this.pendingAuthenticate = false; _this.urlManager.setHomeSinglePane(); }); } }); }); } return _this; } Auth0AuthService.prototype.login = function () { // Call the show method to display the widget. if (this.lock) { this.lock.show(); } }; Auth0AuthService.prototype.authenticated = function () { // Check if there's an unexpired JWT // This searches for an item in localStorage with key == 'id_token' return tokenNotExpired("id_token"); }; Auth0AuthService.prototype.logout = function () { // Remove token from localStorage localStorage.removeItem('id_token'); }; Auth0AuthService.prototype.canActivate = function () { return !this.pendingAuthenticate && this.authenticated(); }; Auth0AuthService.prototype.canDeactivate = function (component) { return !component.isActive; }; Auth0AuthService.prototype.userIsLoggedIn = function () { return this.authenticated(); }; return Auth0AuthService; }(AuthService)); Auth0AuthService = __decorate([ Injectable(), __metadata("design:paramtypes", [Router, UrlManagerService, LoggerService, ConfigService]) ], Auth0AuthService); export { Auth0AuthService }; var NullAuthService = (function (_super) { __extends(NullAuthService, _super); function NullAuthService() { return _super !== null && _super.apply(this, arguments) || this; } NullAuthService.prototype.login = function () { }; NullAuthService.prototype.authenticated = function () { return true; }; NullAuthService.prototype.logout = function () { }; NullAuthService.prototype.canActivate = function () { return true; }; NullAuthService.prototype.canDeactivate = function (component) { return true; }; NullAuthService.prototype.userIsLoggedIn = function () { return false; }; return NullAuthService; }(AuthService)); NullAuthService = __decorate([ Injectable() ], NullAuthService); export { NullAuthService }; //# sourceMappingURL=auth.service.js.map