Skip to content

Commit

Permalink
Merge pull request #2 from dkhrunov/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
dkhrunov authored May 4, 2022
2 parents 333a9a5 + e3a745b commit 1fc9794
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 576 deletions.
4 changes: 0 additions & 4 deletions .vscode/extensions.json

This file was deleted.

20 changes: 0 additions & 20 deletions .vscode/launch.json

This file was deleted.

42 changes: 0 additions & 42 deletions .vscode/tasks.json

This file was deleted.

64 changes: 39 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# JwtAuth
# Ngx JWT Auth
<a href="https://jwt.io/">
<img src="https://jwt.io/img/badge-compatible.svg">
</a>

A library for Token-Based Authentication. This library is configurable for any use cases.
A library for Token-Based Authentication (JWT Authentication).

This library is configurable for any use cases.

## Other languages
- [Russian](./projects/ngx-jwt-auth/src/doc/ru/README.md)
- [Russian](./doc/ru/README.md)

## Content
- [Description](#description)
Expand Down Expand Up @@ -34,7 +39,7 @@ Features:
1. Import `JwtAuthModule` into the App/Core module of your application with a call to the `forRoot` method, and pass parameters to this method:

```typescript
import { JwtAuthModule } from 'jwt-auth';
import { JwtAuthModule } from '@dekh/ngx-jwt-auth';

@NgModule({
imports: [
Expand All @@ -44,7 +49,7 @@ import { JwtAuthModule } from 'jwt-auth';
export class AppModule {}
```

2. You need to create an Api-service by implementing the [BaseAuthApiService](./projects/ngx-jwt-auth/src/lib/services/base-auth-http-service.ts) base class. This class obliges to implement 3 methods `login`, `logout` and `refresh`. The `login` and `refresh` methods must return an Observable with the value `{ accessToken: string; refreshToken?: string; }`, if your server in the `login` authorization method and\or in the `refresh` access token refresh method returns a different format, then it is quite easy to map the value with the `map` operator from rxjs to the desired format. An example of such a service:
2. You need to create an Api-service by implementing the [BaseAuthApiService](./projects/ngx-jwt-auth/src/lib/services/base-auth-api-service.ts) base class. This class obliges to implement 3 methods `login`, `logout` and `refresh`. The `login` and `refresh` methods must return an Observable with the value `{ accessToken: string; refreshToken?: string; }`, if your server in the `login` authorization method and\or in the `refresh` access token refresh method returns a different format, then it is quite easy to map the value with the `map` operator from rxjs to the desired format. An example of such a service:

```typescript
@Injectable({
Expand Down Expand Up @@ -97,7 +102,7 @@ import {
JwtAuthModule,
InMemoryTokenStorage,
LocalStorageTokenStorage
} from 'jwt-auth';
} from '@dekh/ngx-jwt-auth';
import { AuthApiService } from '../services';

@NgModule({
Expand Down Expand Up @@ -127,7 +132,7 @@ import {
InMemoryTokenStorage,
LocalStorageTokenStorage,
JwtAuthInterceptor
} from 'jwt-auth';
} from '@dekh/ngx-jwt-auth';
import { AuthApiService } from '../services';

@NgModule({
Expand Down Expand Up @@ -219,7 +224,7 @@ In the example below, only an unauthorized user can access the `/auth/login` and
```typescript
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AuthGuard, UnAuthGuard } from 'jwt-auth';
import { AuthGuard, UnAuthGuard } from '@dekh/ngx-jwt-auth';
import { LoginComponent, RegistrationComponent } from '../auth';
import { DashboardComponent } from '../dashboard';

Expand Down Expand Up @@ -254,7 +259,7 @@ export class AppRoutingModule {}
```

## Description of all library parameters
- `authApiService: Type<BaseAuthApiService>` - A class that implements BaseAuthApiService and makes requests to the server.
- `authApiService: Type<BaseAuthApiService>` - A class that implements `BaseAuthApiService` and makes requests to the server.

- `tokenStorage: Type<BaseTokenStorage>` - Storage of regular jwt tokens (not authorization ones).

Expand Down Expand Up @@ -304,7 +309,7 @@ In order to create your own token storage, it is enough to implement the [BaseTo

```typescript
// my-custom-token-storage.ts
import { BaseTokenStorage } from 'jwt-auth';
import { BaseTokenStorage } from '@dekh/ngx-jwt-auth';

export class MyCustomTokenStorage extends BaseTokenStorage {
public get(key: string): string | null {
Expand Down Expand Up @@ -332,7 +337,7 @@ We define our storage in the parameters of the `JwtAuthModule` module:

```typescript
// app.module.ts
import { JwtAuthModule } from 'jwt-auth';
import { JwtAuthModule } from '@dekh/ngx-jwt-auth';
import { MyCustomTokenStorage } from '../auth';

@NgModule({
Expand All @@ -345,15 +350,26 @@ import { MyCustomTokenStorage } from '../auth';
export class AppModule {}
```

Or we can register our storage using the `TokenStorage Registry` service:
Or we can register our storage using the `TokenStorageRegistry` service:

```typescript
// app.service.ts
import { TokenStorageRegistry } from 'jwt-auth';
import {
LocalStorageTokenStorage,
InMemoryTokenStorage,
TokenStorageRegistry
} from '@dekh/ngx-jwt-auth';
import { AuthApiService } from '../services';
import { MyCustomTokenStorage } from '../auth';

@Injactable({
provideIn: 'root'
@NgModule({
imports: [
JwtAuthModule.forRoot({
authApiService: AuthApiService,
tokenStorage: LocalStorageTokenStorage,
authTokenStorage: InMemoryTokenStorage,
}),
],
})
export class AppModule {
constructor(private readonly _tokenStorageRegistry: TokenStorageRegistry) {
Expand All @@ -362,28 +378,26 @@ export class AppModule {
}
```

Source code of the `TokenStorageRegistry` [here](./projects/ngx-jwt-auth/src/lib/services/token-storage-registry.service.ts).

## Changing token storage at runtime

In rare cases, you may need to change the token storage at runtime, for this there are two services [TokenStorageManager](./projects/ngx-jwt-auth/src/lib/services/token-storage-manager.service.ts) and [AuthTokenStorageManager](./projects/ngx-jwt-auth/src/lib/services /auth-token-storage-manager.service.ts), both of these services have the same interaction interface. `TokenStorageManager` is used to manage the storage of __non-authorization__ tokens, and `AuthTokenStorageManager` is used to manage the storage of __authorization__ tokens.
In rare cases, you may need to change the token storage at runtime, for this there are two services [TokenStorageManager](./projects/ngx-jwt-auth/src/lib/services/token-storage-manager.service.ts) and [AuthTokenStorageManager](./projects/ngx-jwt-auth/src/lib/services/auth-token-storage-manager.service.ts), both of these services have the same interaction interface. `TokenStorageManager` is used to manage the storage of __non-authorization__ tokens, and `AuthTokenStorageManager` is used to manage the storage of __authorization__ tokens.

Пример:
Example:

```typescript
// app.service.ts
// token-storage-changer.service.ts
import {
AuthTokenStorageManager,
TokenStorageRegistry,
CookiesTokenStorage,
BaseTokenStorage,
} from 'jwt-auth';
} from '@dekh/ngx-jwt-auth';
import { MyCustomTokenStorage } from '../auth';

@Injactable({
provideIn: 'root'
@Injectable({
provideIn: 'root'
})
export class AppModule {
export class TokenStorageChangerService {
constructor(
private readonly _authTokenStorageManager: AuthTokenStorageManager,
private readonly _tokenStorageRegistry: TokenStrageRegistry,
Expand Down Expand Up @@ -425,4 +439,4 @@ export class AppModule {

The reason for this error is a cyclic call to `JwtAuthInterceptor`. Since the interceptor handles every request, except for those url requests specified in the `unsecuredUrls` config parameter, the token refresh request creates a circular dependency.

The solution to this problem is to specify in the `unsecuredUrls` array the URL or path of the accessToken update request, or specify the root path for all requests related to user authorization/registration, for example: `"/auth/"`, then all requests c path auth will be excluded from the interceptor check - `server.api/auth/login`, `server.api/auth/register`, `server.api/auth/refresh` and the like.
The solution to this problem is to specify in the `unsecuredUrls` array the URL or path of the accessToken update request, or specify the root path for all requests related to user authorization/registration, for example: `"/auth/"`, then all requests c path auth will be excluded from the interceptor check - `server.api/auth/login`, `server.api/auth/register`, `server.api/auth/refresh` and etc.
Loading

0 comments on commit 1fc9794

Please sign in to comment.