UNPKG

1.81 kBJavaScriptView Raw
1import { HttpResponse } from '@angular/common/http';
2import { deepExtend, getDeepFromObject } from '../helpers';
3import { nbAuthCreateToken, NbAuthIllegalTokenError, } from '../services/token/token';
4export class NbAuthStrategy {
5 // we should keep this any and validation should be done in `register` method instead
6 // otherwise it won't be possible to pass an empty object
7 setOptions(options) {
8 this.options = deepExtend({}, this.defaultOptions, options);
9 }
10 getOption(key) {
11 return getDeepFromObject(this.options, key, null);
12 }
13 createToken(value, failWhenInvalidToken) {
14 const token = nbAuthCreateToken(this.getOption('token.class'), value, this.getName());
15 // At this point, nbAuthCreateToken failed with NbAuthIllegalTokenError which MUST be intercepted by strategies
16 // Or token is created. It MAY be created even if backend did not return any token, in this case it is !Valid
17 if (failWhenInvalidToken && !token.isValid()) {
18 // If we require a valid token (i.e. isValid), then we MUST throw NbAuthIllegalTokenError so that the strategies
19 // intercept it
20 throw new NbAuthIllegalTokenError('Token is empty or invalid.');
21 }
22 return token;
23 }
24 getName() {
25 return this.getOption('name');
26 }
27 createFailResponse(data) {
28 return new HttpResponse({ body: {}, status: 401 });
29 }
30 createSuccessResponse(data) {
31 return new HttpResponse({ body: {}, status: 200 });
32 }
33 getActionEndpoint(action) {
34 const actionEndpoint = this.getOption(`${action}.endpoint`);
35 const baseEndpoint = this.getOption('baseEndpoint');
36 return actionEndpoint ? baseEndpoint + actionEndpoint : '';
37 }
38}
39//# sourceMappingURL=auth-strategy.js.map
\No newline at end of file