Skip to content

Commit

Permalink
feat(auth): warn about empty token, return falsy result
Browse files Browse the repository at this point in the history
BREAKING CHANGE: now if login/register returns no value for token, unsuccessful NbAuthResult is returned
  • Loading branch information
nnixaa committed Jan 24, 2018
1 parent ea0486d commit ad8afb0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,26 @@
"isStatic": false,
"shortDescription": "",
"description": ""
},
{
"examples": [],
"params": [
{
"name": "module",
"type": "string",
"required": null,
"shortDescription": "",
"description": ""
}
],
"platform": null,
"name": "validateToken",
"type": [
"any"
],
"isStatic": false,
"shortDescription": "",
"description": ""
}
],
"name": "NbEmailPassAuthProvider",
Expand Down
17 changes: 17 additions & 0 deletions src/framework/auth/providers/email-pass-auth.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export class NbEmailPassAuthProvider extends NbAbstractAuthProvider {

return res;
}),
this.validateToken('login'),
map((res) => {
return new NbAuthResult(
true,
Expand Down Expand Up @@ -245,6 +246,7 @@ export class NbEmailPassAuthProvider extends NbAbstractAuthProvider {

return res;
}),
this.validateToken('register'),
map((res) => {
return new NbAuthResult(
true,
Expand Down Expand Up @@ -401,6 +403,21 @@ export class NbEmailPassAuthProvider extends NbAbstractAuthProvider {
);
}

protected validateToken (module: string): any {
return map((res) => {
const token = this.getConfigValue('token.getter')(module, res);
if (!token) {
const key = this.getConfigValue('token.key');
console.warn(`NbEmailPassAuthProvider:
Token is not provided under '${key}' key
with getter '${this.getConfigValue('token.getter')}', check your auth configuration.`);

throw new Error('Could not extract token from the response.');
}
return res;
});
}

protected getActionEndpoint(action: string): string {
const actionEndpoint: string = this.getConfigValue(`${action}.endpoint`);
const baseEndpoint: string = this.getConfigValue('baseEndpoint');
Expand Down

0 comments on commit ad8afb0

Please sign in to comment.