diff --git a/packages/auth/docs/getting-started.en-US.md b/packages/auth/docs/getting-started.en-US.md index 89b850c02..0628ca76f 100644 --- a/packages/auth/docs/getting-started.en-US.md +++ b/packages/auth/docs/getting-started.en-US.md @@ -48,6 +48,7 @@ Configure the `provideAuth` environment in `app.config.ts`: ```typescript providers: [ + // Indicates using JWT style and using `localStorage` to store Token provideAuth(withJWT(), withLocalStorage()), ] ``` diff --git a/packages/auth/docs/getting-started.zh-CN.md b/packages/auth/docs/getting-started.zh-CN.md index cb9920ff4..30be9cac1 100644 --- a/packages/auth/docs/getting-started.zh-CN.md +++ b/packages/auth/docs/getting-started.zh-CN.md @@ -48,6 +48,7 @@ npm i -S @delon/auth ```typescript providers: [ + // 表示使用JWT风格并用 `localStorage` 存储 Token provideAuth(withJWT(), withLocalStorage()), ] ``` diff --git a/packages/auth/docs/send.en-US.md b/packages/auth/docs/send.en-US.md index 67290da08..49f7c49ec 100644 --- a/packages/auth/docs/send.en-US.md +++ b/packages/auth/docs/send.en-US.md @@ -10,7 +10,7 @@ type: Documents It is better to add the corresponding authentication information to each request through the HTTP interceptor. `@delonn/auth` implements two separate HTTP interceptors based on two different authentication styles. -### withAuthSimple +### withSimple The parameter name and its sending location can be specified via `DelonAuthConfig`, for example: @@ -22,15 +22,15 @@ token_send_place = 'header'; Indicates the `{ token: 'Bearer token_string' }` data in the `header` of each request. -### withAuthJWT +### withJWT It is a standard JWT sending rule that automatically adds `{ Authorization: 'Bearer token_string' }` to `header`. ### How to choose? -`withAuthSimple` is a very liberal style, you can put `token` in the request body, request header, etc. +`withSimple` is a very liberal style, you can put `token` in the request body, request header, etc. -`withAuthJWT` is a JWT standard, which needs to ensure that the backend also uses such standards. +`withJWT` is a JWT standard, which needs to ensure that the backend also uses such standards. ## How to load @@ -38,8 +38,9 @@ In `app.config.ts` file: ```ts providers: [ - withAuthSimple(), - // or withAuthSimple() + provideAuth(withSimple()), + // Or JWT + provideAuth(withJWT()), ] ``` diff --git a/packages/auth/docs/send.zh-CN.md b/packages/auth/docs/send.zh-CN.md index c41bda7fa..46b857dfc 100644 --- a/packages/auth/docs/send.zh-CN.md +++ b/packages/auth/docs/send.zh-CN.md @@ -8,7 +8,7 @@ type: Documents 通过HTTP拦截器在每一个请求中加入相应的认证信息,这是再好不过。`@delonn/auth` 根据两种不同认证风格,实现两种各自的HTTP拦截器。 -### withAuthSimple +### withSimple 透过 `DelonAuthConfig` 可以指定参数名以及其发送位置,例如: @@ -20,15 +20,15 @@ token_send_place = 'header'; 表示在每一个请求的 `header` 加上 `{ token: 'Bearer token_string' }` 数据。 -### withAuthJWT +### withJWT 它是一个标准JWT的发送规则,即在 `header` 自动加上 `{ Authorization: 'Bearer token_string' }`。 ### 如何选择? -`withAuthSimple` 是一种自由度非常高的风格,你可以将 `token` 放在请求体、请求头等当中。 +`withSimple` 是一种自由度非常高的风格,你可以将 `token` 放在请求体、请求头等当中。 -`withAuthJWT` 是一个 JWT 标准,这需要确保后端也采用这类标准。 +`withJWT` 是一个 JWT 标准,这需要确保后端也采用这类标准。 ## 如何加载 @@ -36,8 +36,9 @@ token_send_place = 'header'; ```ts providers: [ - withAuthSimple(), - // or withAuthSimple() + provideAuth(withSimple()), + // Or JWT + provideAuth(withJWT()), ] ``` diff --git a/packages/auth/docs/set.en-US.md b/packages/auth/docs/set.en-US.md index 21031258b..6fcc342a0 100644 --- a/packages/auth/docs/set.en-US.md +++ b/packages/auth/docs/set.en-US.md @@ -28,35 +28,28 @@ constructor(@Inject(DA_SERVICE_TOKEN) service: ITokenService) { ## Storage type -The default is to use `LocalStorageStore` persistent storage, you can change other storage methods in `global-config.module.ts` or root module. +The default is to use `withLocalStorage` persistent storage. You can change other storage methods in `app.config.ts`. ```ts -export class GlobalConfigModule { - static forRoot(): ModuleWithProviders { - return { - ngModule: GlobalConfigModule, - providers: [ - { provide: DA_STORE_TOKEN, useClass: MemoryStore } - ] - }; - } -} +providers: [ + provideAuth(withJWT(), withLocalStorage()), +] ``` Contains three storage types: -### LocalStorageStore +### withLocalStorage `localStorage` storage, **not lost after closing the browser**. -### SessionStorageStore +### withSessionStorage `sessionStorage` storage, **lost after closing the browser**. -### MemoryStore +### withMemoryStorage Memory storage, **lost after closing the browser tab**. -### CookieStorageStore +### withCookie -`cookie` storage. \ No newline at end of file +`cookie` storage. diff --git a/packages/auth/docs/set.zh-CN.md b/packages/auth/docs/set.zh-CN.md index 9cb2dff74..47306d95d 100644 --- a/packages/auth/docs/set.zh-CN.md +++ b/packages/auth/docs/set.zh-CN.md @@ -26,35 +26,28 @@ constructor(@Inject(DA_SERVICE_TOKEN) service: ITokenService) { ## 存储类型 -默认是采用 `LocalStorageStore` 持久化存储,你可以在 `global-config.module.ts` 或根模块里变更其他存储方式。 +默认是采用 `withLocalStorage` 持久化存储,你可以在 `app.config.ts` 变更其他存储方式。 ```ts -export class GlobalConfigModule { - static forRoot(): ModuleWithProviders { - return { - ngModule: GlobalConfigModule, - providers: [ - { provide: DA_STORE_TOKEN, useClass: MemoryStore } - ] - }; - } -} +providers: [ + provideAuth(withJWT(), withLocalStorage()), +] ``` 包含三种存储类型: -### LocalStorageStore +### withLocalStorage `localStorage` 存储,关掉浏览器后**不丢失**。 -### SessionStorageStore +### withSessionStorage `sessionStorage` 存储,关掉浏览器后**丢失**。 -### MemoryStore +### withMemoryStorage 内存存储,关掉浏览器标签后**丢失**。 -### CookieStorageStore +### withCookie Cookie 存储。 diff --git a/packages/auth/src/provide.ts b/packages/auth/src/provide.ts index 5fbd1bd01..382beee8c 100644 --- a/packages/auth/src/provide.ts +++ b/packages/auth/src/provide.ts @@ -3,7 +3,7 @@ import { EnvironmentProviders, Provider, makeEnvironmentProviders } from '@angul import { CookieService } from '@delon/util/browser'; -import { CookieStorageStore, DA_STORE_TOKEN, LocalStorageStore, SessionStorageStore } from './store'; +import { CookieStorageStore, DA_STORE_TOKEN, LocalStorageStore, MemoryStore, SessionStorageStore } from './store'; import { JWTInterceptor, SimpleInterceptor } from './token/index'; export enum AuthFeatureKind { @@ -76,3 +76,8 @@ export function withLocalStorage(): AuthFeature { export function withSessionStorage(): AuthFeature { return makeAuthFeature(AuthFeatureKind.Store, [{ provide: DA_STORE_TOKEN, useClass: SessionStorageStore }]); } + +/** Memory storage, **lost after closing the browser tab**. */ +export function withMemoryStorage(): AuthFeature { + return makeAuthFeature(AuthFeatureKind.Store, [{ provide: DA_STORE_TOKEN, useClass: MemoryStore }]); +} diff --git a/packages/auth/src/store/memory.service.ts b/packages/auth/src/store/memory.service.ts index 3e9d3d0ec..2b1ab9468 100644 --- a/packages/auth/src/store/memory.service.ts +++ b/packages/auth/src/store/memory.service.ts @@ -3,11 +3,6 @@ import { ITokenModel } from '../token/interface'; /** * 内存存储,关掉浏览器标签后**丢失**。 - * - * ```ts - * // global-config.module.ts - * { provide: DA_STORE_TOKEN, useClass: MemoryStore } - * ``` */ export class MemoryStore implements IStore { private cache: { [key: string]: ITokenModel | null } = {};