Last active
March 4, 2019 23:12
-
-
Save dizco/5ad777bde46572f5c2786a93d7aa9968 to your computer and use it in GitHub Desktop.
Custom auth token fields Nebular auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ModuleWithProviders, NgModule, Optional, SkipSelf } from '@angular/core'; | |
import { CommonModule } from '@angular/common'; | |
import { NbAuthModule, NbPasswordAuthStrategy } from '@nebular/auth'; | |
import { throwIfAlreadyLoaded } from './module-import-guard'; | |
import { DataModule } from './data/data.module'; | |
import { environment as env } from '../../environments/environment'; | |
import { HttpResponse } from '@angular/common/http'; | |
import { getDeepFromObject } from '@nebular/auth/helpers'; | |
import { AuthToken, AuthTokenFields } from './auth/token'; | |
import { NbRoleProvider, NbSecurityModule } from '@nebular/security'; | |
export function tokenGetter(module: string, res: HttpResponse<Object>) { | |
const authToken: AuthTokenFields = { | |
token: getDeepFromObject(res.body, 'access_token'), | |
validUntil: getDeepFromObject(res.body, 'valid_until'), | |
}; | |
return JSON.stringify(authToken); //Unfortunately, we need to return a string. We parse on the other side | |
} | |
export const NB_CORE_PROVIDERS = [ | |
...DataModule.forRoot().providers, | |
...NbAuthModule.forRoot({ | |
strategies: [ | |
NbPasswordAuthStrategy.setup({ | |
name: 'email', | |
baseEndpoint: `${env.api.url}`, | |
login: { | |
endpoint: 'login', | |
method: 'post', | |
defaultErrors: ['auth.login.incorrect-combination'], | |
}, | |
token: { | |
class: AuthToken, | |
key: 'access_token', | |
getter: tokenGetter, | |
}, | |
}), | |
], | |
}).providers, | |
...NbSecurityModule.forRoot({ | |
accessControl: {}, | |
}).providers, | |
{ | |
provide: NbRoleProvider, useClass: AbilitiesService, | |
}, | |
]; | |
@NgModule({ | |
imports: [ | |
CommonModule, | |
], | |
exports: [ | |
NbAuthModule, | |
], | |
declarations: [], | |
entryComponents: [], | |
}) | |
export class CoreModule { | |
constructor(@Optional() @SkipSelf() parentModule: CoreModule) { | |
throwIfAlreadyLoaded(parentModule, 'CoreModule'); | |
} | |
static forRoot(): ModuleWithProviders { | |
return <ModuleWithProviders>{ | |
ngModule: CoreModule, | |
providers: [ | |
...NB_CORE_PROVIDERS, | |
], | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NbAuthSimpleToken } from '@nebular/auth'; | |
import * as moment from 'moment'; | |
export interface AuthTokenFields { | |
token: string; | |
validUntil: string; | |
} | |
/** | |
* Wrapper for auth token with additional methods. | |
*/ | |
export class AuthToken extends NbAuthSimpleToken { | |
static NAME = 'auth:token'; | |
private parsedToken: AuthTokenFields; | |
getValue(): string { | |
return this.parsePayload().token; | |
} | |
/** | |
* Returns expiration date | |
* @returns moment.Moment | |
*/ | |
getTokenExpiration(): moment.Moment { | |
const payload = this.parsePayload(); | |
return moment(payload.validUntil); | |
} | |
/** | |
* Is the token still valid | |
* @returns {boolean} | |
*/ | |
isValid(): boolean { | |
return super.isValid() && !this.getTokenExpiration().isBefore(); | |
} | |
/** | |
* Returns payload object | |
* @returns AuthTokenFields | |
*/ | |
protected parsePayload(): AuthTokenFields { | |
if (!this.token) { | |
throw new Error('Cannot extract payload from an empty token.'); | |
} | |
if (!this.parsedToken) { | |
this.parsedToken = JSON.parse(this.token); | |
} | |
return this.parsedToken; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment