Skip to content

Commit

Permalink
refactor(auth): move token storage out of token service
Browse files Browse the repository at this point in the history
BREAKING CHANGES:
1) `NB_AUTH_TOKEN_WRAPPER_CLASS` renamed to `NB_AUTH_TOKEN_CLASS` and you should use `useValue` instead of `useClass` when providing a token:
`{ provide: NB_AUTH_TOKEN_WRAPPER_TOKEN, useClass: NbAuthJWTToken },` to `{ provide: NB_AUTH_TOKEN_CLASS, useValue: NbAuthJWTToken },`

2)`setValue` method removed from `NbAuthSimpleToken`, `NbAuthJWTToken`, tokens only accept read-only value when created through constructor now.

3) Token Storage moved out from `NbTokenService` into a separate `NbTokenStorage`.
If you need to change the storage behavior or provide your own - just extend your class from basic `NbTokenStorage`
or `NbTokenLocalStorage` and provide in your `app.module`:
```
  { provide: NbTokenStorage, useClass: NbTokenCustomStorage },
```
  • Loading branch information
nnixaa committed Feb 19, 2018
1 parent 78f699c commit c8273da
Show file tree
Hide file tree
Showing 34 changed files with 927 additions and 424 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { Component, Input } from '@angular/core';
<ng-container *ngIf="method.shortDescription || method.description">
<td>{{ method.name }} <br><i *ngIf="method.isStatic">static method</i></td>
<td>
<div class="method-signature" *ngIf="method.type[0] !== 'void'">
<div class="method-signature">
<div *ngIf="method.params.length > 0"><i>parameters:</i>
<span *ngFor="let param of method.params; let last = last">
{{param.name}}: <code>{{param.type}}</code><span *ngIf="!last">,</span>
Expand Down
16 changes: 6 additions & 10 deletions docs/articles/auth-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

## Installation steps

1) First, let's install the module as it's distributed as an npm package, but make sure you have the [Nebular theme module up and running](https://akveo.github.io/nebular/#/docs/installation/add-into-existing-project).
1) First, let's install the module as it's distributed as an npm package, but make sure you have the [Nebular Theme module up and running](https://akveo.github.io/nebular/#/docs/installation/add-into-existing-project).
Nebular Theme is required to use built-in Auth Components. If you are not going to use those at all, you can use `Auth Module` without `Nebular Theme`.

Let's assume that we need to setup email & password authentication based on Nebular Auth and NbEmailPassAuthProvider.

`npm i @nebular/auth`

Expand Down Expand Up @@ -39,9 +42,9 @@

```

We also specify a `forms` key, which configures available options for the UI components, but let's leave it empty for now and get back to it in the [Configuring UI](#/docs/auth/configuring-ui) article.
We also specified a `forms` key, which configures available options for the Auth Components. We'll leave it empty for now and get back to it in the [Configuring UI](#/docs/auth/configuring-ui) article.

4) Next, we need to configure UI part, let's add UI components into your `app-routing.module.ts`:
4) Next, we need to configure Auth Components routes, let's add those into your `app-routing.module.ts`:


```typescript
Expand Down Expand Up @@ -91,13 +94,6 @@ export const routes: Routes = [
];
```

<div class="note note-info">
<div class="note-title">Note</div>
<div class="note-body">
The components are wrapped by `NbAuthBlockComponent`, which is optional and just provides some basic styling for the page.
</div>
</div>

5) Last but not least - install the component styles into your themes.scss ([more details](/#/docs/guides/enabling-theme-system)):

```scss
Expand Down
15 changes: 10 additions & 5 deletions docs/articles/auth-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ Authentication UI components:
- Register
- Password Recover
- Password Reset

You can use the built-in components or create your custom ones.

Two auth providers:
- Dummy auth provider - simple provider for testing purposes, could be used to simulate backend responses while API is in the development;
- EmailPass auth provider - the most common email/password authentication strategy.
Auth providers:
- `NbDummyAuthProvider` - simple provider for testing purposes, could be used to simulate backend responses while API is in the development;
- `NbEmailPassAuthProvider` - the most common email/password authentication strategy.

Other helper services:
- Token Service, JWT token, and Simple token - helper services for token management handling;
- JWT and Simple HTTP interceptors - intercepts the token into your HTTP requests.
- `NbAuthService` - facade service to communicate with a configured provider;
- `NbTokenService` - service that allows you to manage authentication token - get, set, clear and also listen to token changes over time;
- `NbTokenLocalStorage` - storage service for storing tokens in a browser local storage;
- `NbAuthJWTToken` and `NbAuthSimpleToken` - helper classes to work with your token;
- `NbAuthJWTInterceptor` and `NbAuthSimpleInterceptor` - http interceptors to inject token value into http requests.

<hr class="section-end">

Expand Down
4 changes: 2 additions & 2 deletions docs/articles/auth-provider.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## A provider

In Nebular terms `auth provider` is a class containing authentication logic using by the application UI.
It accepts user input (login/email/password/oauth token/etc), communicates the input to the backend API and finally provides some resulting output back to the UI layer.
In Nebular terms `auth provider` is a class containing authentication logic specific for some authentication flow (email&password, OAuth, etc).
It accepts user input (login/email/password/token/etc), communicates the input to the backend API and finally provides the resulting output back to the Auth UI layer.
Currently, there are two Auth Providers available out of the box:

Two auth providers:
Expand Down
14 changes: 7 additions & 7 deletions docs/articles/auth-token.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
At this step, we assume that Nebular Auth module is up and running,
you have successfully configured an auth provider and adjusted auth look & fell accordingly with your requirements.

It's time to get a user token after successful authentication to be able to communicate with the server and, for instance, show the username in the header of the application.
Let's assume that your backend returns a JWT token so that we can use the token payload to extract the user info out of it.
It's time to get a user token after successful authentication to be able to communicate with the server and, for instance, show a username in the header of the application.
Let's assume that your backend returns a JWT token so that we can use the token payload to extract a user info out of it.

1) Firstly, let's tell Nebular that we are waiting for JWT token, to do that we just need to provide a respective class. Open your `app.module.ts` and add the following:

Expand All @@ -17,12 +17,12 @@ import { NB_AUTH_TOKEN_WRAPPER_TOKEN, NbAuthJWTToken } from '@nebular/auth';

providers: [
...
{ provide: NB_AUTH_TOKEN_WRAPPER_TOKEN, useClass: NbAuthJWTToken },
{ provide: NB_AUTH_TOKEN_WRAPPER_TOKEN, useValue: NbAuthJWTToken },
],
});

```
This line tells Angular to inject `NbAuthJWTToken` (instead of the default `NbAuthSimpleToken`) which is a simple wrapper class around a value your API service returns.
This line tells Angular to inject `NbAuthJWTToken` (instead of the default `NbAuthSimpleToken`) which is a wrapper class around a value your API service returns.

2) Then, let's configure where Nebular should look for the token in the login/register response data. By default Nebular expects that your token is located under the `data.token` keys of the JSON response:

Expand All @@ -34,7 +34,7 @@ This line tells Angular to inject `NbAuthJWTToken` (instead of the default `NbAu
}
```

Imagine that our API returns the token as `{token: 'some-jwt-token'}` not wrapping your response in `data` property, let's tell that to Nebular:
We'll assume that our API returns a token as just `{token: 'some-jwt-token'}` not wrapping your response in the `data` property, let's tell that to Nebular:

```typescript
@NgModule({
Expand Down Expand Up @@ -98,7 +98,7 @@ export class HeaderComponent {
this.authService.onTokenChange()
.subscribe((token: NbAuthJWTToken) => {

if (token.getValue()) {
if (token.isValid()) {
this.user = token.getPayload(); // here we receive a payload from the token and assigne it to our `user` variable
}

Expand All @@ -108,7 +108,7 @@ export class HeaderComponent {
}
```

6) Lastly, let's grad the `user` variable and put it in the template to show the user info. The `nb-user` component is a great fit for this:
6) Lastly, let's grad a `user` variable and put it in the template to show the user info. The `nb-user` component is a great fit for this:


```typescript
Expand Down
2 changes: 1 addition & 1 deletion docs/articles/security-acl-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class RoleProvider implements NbRoleProvider {
return this.authService.onTokenChange()
.pipe(
map((token: NbAuthJWTToken) => {
return token ? token.getPayload()['role'] : 'guest';
return token.isValid() ? token.getPayload()['role'] : 'guest';
}),
);
}
Expand Down
Loading

0 comments on commit c8273da

Please sign in to comment.