1 | import { HttpResponse } from '@angular/common/http';
|
2 | import { deepExtend, getDeepFromObject } from '../helpers';
|
3 | import { nbAuthCreateToken, NbAuthIllegalTokenError, } from '../services/token/token';
|
4 | export class NbAuthStrategy {
|
5 |
|
6 |
|
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 |
|
16 |
|
17 | if (failWhenInvalidToken && !token.isValid()) {
|
18 |
|
19 |
|
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 |
|
\ | No newline at end of file |