ngx-adal-angular
Version:
It is a easy way to implement microsoft adal authentication, it was taken of microsoft-adal-angular6 and compiled in Angular 14 to implement with higher versions to 7
165 lines (158 loc) • 6.4 kB
JavaScript
import * as i0 from '@angular/core';
import { Injectable, Inject, NgModule } from '@angular/core';
import { bindCallback } from 'rxjs';
import * as adalLib from 'adal-angular';
/// <reference path='./../../../../node_modules/@types/adal/index.d.ts'/>
class NgxAdalAngularService {
constructor(adalConfig) {
this.adalConfig = adalConfig;
if (typeof adalConfig === 'function')
this.adalConfig = adalConfig();
this.context = adalLib.inject(this.adalConfig);
this.handleCallback();
}
get LoggedInUserEmail() {
if (this.isAuthenticated)
return this.context.getCachedUser().userName;
return '';
}
get LoggedInUserName() {
if (this.isAuthenticated)
return this.context.getCachedUser().profile.name;
return '';
}
login() {
this.context.login();
}
logout() {
this.context.logOut();
}
GetResourceForEndpoint(url) {
let resource = null;
if (url) {
resource = this.context.getResourceForEndpoint(url);
if (!resource) {
resource = this.adalConfig.clientId;
}
}
return resource;
}
RenewToken(url) {
let resource = this.GetResourceForEndpoint(url);
return this.context.clearCacheForResource(resource); // Trigger the ADAL token renew
}
acquireToken(url) {
const _this = this; // save outer this for inner function
let errorMessage;
return bindCallback(acquireTokenInternal, (token) => {
if (!token && errorMessage) {
throw (errorMessage);
}
return token;
})();
function acquireTokenInternal(cb) {
let s = '';
let resource;
resource = _this.GetResourceForEndpoint(url);
_this.context.acquireToken(resource, (error, tokenOut) => {
if (error) {
_this.context.error('Error when acquiring token for resource: ' + resource, error);
errorMessage = error;
cb('');
}
else {
cb(tokenOut);
s = tokenOut;
}
});
return s;
}
}
getToken(url) {
var _a, _b;
const resource = this.context.getResourceForEndpoint(url);
const storage = this.adalConfig.cacheLocation;
let key = '';
if (resource)
key = 'adal.access.token.key' + resource;
else
key = 'adal.idtoken';
if (storage === 'localStorage')
return (_a = localStorage.getItem(key)) !== null && _a !== void 0 ? _a : '';
else
return (_b = sessionStorage.getItem(key)) !== null && _b !== void 0 ? _b : '';
}
handleCallback() {
this.context.handleWindowCallback();
}
get userInfo() {
return this.context.getCachedUser();
}
get accessToken() {
return this.context.getCachedToken(this.adalConfig.clientId);
}
get isAuthenticated() {
return (this.userInfo && this.accessToken) ? true : false;
}
}
NgxAdalAngularService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.9", ngImport: i0, type: NgxAdalAngularService, deps: [{ token: 'adalConfig' }], target: i0.ɵɵFactoryTarget.Injectable });
NgxAdalAngularService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.2.9", ngImport: i0, type: NgxAdalAngularService, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.9", ngImport: i0, type: NgxAdalAngularService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}], ctorParameters: function () {
return [{ type: undefined, decorators: [{
type: Inject,
args: ['adalConfig']
}] }];
} });
class NgxAdalGuard {
constructor(adalSvc) {
this.adalSvc = adalSvc;
}
canActivate(route, state) {
if (this.adalSvc.isAuthenticated)
return true;
else {
this.adalSvc.login();
return false;
}
}
canActivateChild(childRoute, state) {
return this.canActivate(childRoute, state);
}
}
NgxAdalGuard.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.9", ngImport: i0, type: NgxAdalGuard, deps: [{ token: NgxAdalAngularService }], target: i0.ɵɵFactoryTarget.Injectable });
NgxAdalGuard.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.2.9", ngImport: i0, type: NgxAdalGuard });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.9", ngImport: i0, type: NgxAdalGuard, decorators: [{
type: Injectable
}], ctorParameters: function () { return [{ type: NgxAdalAngularService }]; } });
class NgxAdalAngularModule {
static forRoot(adalConfig) {
return {
ngModule: NgxAdalAngularModule,
providers: [NgxAdalAngularService, { provide: 'adalConfig', useValue: adalConfig }]
};
}
}
NgxAdalAngularModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.9", ngImport: i0, type: NgxAdalAngularModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
NgxAdalAngularModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.2.9", ngImport: i0, type: NgxAdalAngularModule });
NgxAdalAngularModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.2.9", ngImport: i0, type: NgxAdalAngularModule });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.9", ngImport: i0, type: NgxAdalAngularModule, decorators: [{
type: NgModule,
args: [{
declarations: [],
imports: [],
exports: []
}]
}] });
/*
* Public API Surface of ngx-adal-angular
*/
/**
* Generated bundle index. Do not edit.
*/
export { NgxAdalAngularModule, NgxAdalAngularService, NgxAdalGuard };
//# sourceMappingURL=ngx-adal-angular.mjs.map