Skip to content

Commit

Permalink
docs: Add maxAge for token/refresh token
Browse files Browse the repository at this point in the history
  • Loading branch information
trandaison committed Jun 20, 2024
1 parent 13a7e75 commit 1f7cc90
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 85 deletions.
80 changes: 40 additions & 40 deletions docs/api/$auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,6 @@ redirectPath: string | null;

The redirect path after successful login.

### `accessToken`

```ts
accessToken: string | null | undefined;
```

Contains the value of the access token, returns `null` if not logged in or the token has expired.

### `refreshToken`

```ts
refreshToken: string | null | undefined;
```

Contains the value of the refresh token, returns `null` if not logged in or the token has expired.

### `loggedIn`

```ts
Expand All @@ -109,30 +93,6 @@ const { $auth } = useNuxtApp();
const { loggedIn } = $auth;
```

### `hasTokens`

```ts
hasTokens: boolean;
```

Has a value of `true` when there are tokens in the cookie and the tokens are still valid (either access token or refresh token).

### `isSessionExpired`

```ts
isSessionExpired: boolean;
```

Has a value of `true` when the user is **logged in but the access token has expired** while **the refresh token is still valid**.

### `isSessionEnd`

```ts
isSessionEnd: boolean;
```

Has a value of `true` when both the access token and refresh token have expired.

### `isPersistent`

```ts
Expand Down Expand Up @@ -180,6 +140,46 @@ By default, this instance has the following features:

## Methods

### `accessToken`

```ts
function accessToken(): string | null | undefined;
```

Contains the value of the access token, returns `null` if not logged in or the token has expired.

### `refreshToken`

```ts
function refreshToken(): string | null | undefined;
```

Contains the value of the refresh token, returns `null` if not logged in or the token has expired.

### `hasTokens`

```ts
function hasTokens(): boolean;
```

Has a value of `true` when there are tokens in the cookie and the tokens are still valid (either access token or refresh token).

### `isSessionExpired`

```ts
function isSessionExpired(): boolean;
```

Has a value of `true` when the user is **logged in but the access token has expired** while **the refresh token is still valid**.

### `isSessionEnd`

```ts
function isSessionEnd(): boolean;
```

Has a value of `true` when both the access token and refresh token have expired.

### `login`

```ts
Expand Down
10 changes: 8 additions & 2 deletions docs/api/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,28 @@ token: {
headerName: 'Authorization',
type: 'Bearer',
property: 'token',
maxAge: 365 * 24 * 60 * 60, // 1 year
}
```

- `headerName` The name of the header containing the token.
- `type` The type of token.
- `property` The name of the token key in the response object from the login and refresh token API. In the [example above](#login), `property` has a value of `token`.
- `maxAge` The maximum age of the token in seconds. If the token is a JWT, the module will use the `exp` claim to calculate the expiration time. If the token is not a JWT, the module will use this `maxAge` value or the `cookie.maxAge` value to calculate the expiration time.

## `refreshToken`

```ts
refreshToken: {
paramName: 'token',
property: 'refresh_token',
maxAge: 365 * 24 * 60 * 60, // 1 year
}
```

- `paramName` The name of the parameter containing the refresh token in the request body.
- `property` The name of the property containing the refresh token in the response object from the login and refresh token API. In the [example above](#login), `property` has a value of `refresh_token`.
- `maxAge` The maximum age of the refresh token in seconds. If the refresh token is a JWT, the module will use the `exp` claim to calculate the expiration time. If the refresh token is not a JWT, the module will use this `maxAge` value or the `cookie.maxAge` value to calculate the expiration time.

:::tip
This module requires the login and refresh token API to return the same property containing the refresh token.
Expand All @@ -172,13 +176,15 @@ This module requires the login and refresh token API to return the same property
cookie: {
ssl: false,
maxAge: 365 * 24 * 60 * 60, // 1 year
domain: '',
path: '/',
}
```

- `ssl` Set `true` to enable `Secure` flag for cookies (recommended if possible).


- `maxAge` The maximum age of the cookie in seconds. The default value is 1 year. Make sure the value is larger than the `maxAge` of both access token and refresh token.
- `domain` The domain of the cookie. If not set, the cookie will be set to the domain of the current page.
- `path` The path of the cookie. The default value is `/`.

## `middleware`

Expand Down
82 changes: 41 additions & 41 deletions docs/vi/api/$auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,6 @@ redirectPath: string | null;

Đường dẫn redirect sau khi đăng nhập thành công.

### `accessToken`

```ts
accessToken: string | null | undefined;
```

Chứa giá trị của access token, trả về `null` nếu chưa đăng nhập hoặc token đã hết hạn.

### `refreshToken`

```ts
refreshToken: string | null | undefined;
```

Chứa giá trị của refesh token, trả về `null` nếu chưa đăng nhập hoặc token đã hết hạn.

### `loggedIn`

```ts
Expand All @@ -108,31 +92,6 @@ Là một `Ref` có giá trị `true` khi đã log in, ngược lại có giá t
const { $auth } = useNuxtApp();
const { loggedIn } = $auth;
```

### `hasTokens`

```ts
hasTokens: boolean;
```

Có giá trị `true` khi có token trong cookie và token còn hạn sử dụng (bất kể access token hoặc refresh token).

### `isSessionExpired`

```ts
isSessionExpired: boolean;
```

Có giá trị `true` khi user đã log in nhưng **access token đã hết hạn** nhưng **refresh token vẫn còn hạn sử dụng**.

### `isSessionEnd`

```ts
isSessionEnd: boolean;
```

Có giá trị `true` khi cả access token và refresh token đều đã hết hạn.

### `isPersistent`

```ts
Expand Down Expand Up @@ -180,6 +139,47 @@ Instance này mặc định được thiết lập sẵn một số cài đặt

## Methods


### `accessToken`

```ts
function accessToken(): string | null | undefined;
```

Chứa giá trị của access token, trả về `null` nếu chưa đăng nhập hoặc token đã hết hạn.

### `refreshToken`

```ts
function refreshToken(): string | null | undefined;
```

Chứa giá trị của refesh token, trả về `null` nếu chưa đăng nhập hoặc token đã hết hạn.

### `hasTokens`

```ts
function hasTokens(): boolean;
```

Có giá trị `true` khi có token trong cookie và token còn hạn sử dụng (bất kể access token hoặc refresh token).

### `isSessionExpired`

```ts
function isSessionExpired(): boolean;
```

Có giá trị `true` khi user đã log in nhưng **access token đã hết hạn** nhưng **refresh token vẫn còn hạn sử dụng**.

### `isSessionEnd`

```ts
function isSessionEnd(): boolean;
```

Có giá trị `true` khi cả access token và refresh token đều đã hết hạn.

### `login`

```ts
Expand Down
10 changes: 8 additions & 2 deletions docs/vi/api/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,28 @@ token: {
headerName: 'Authorization',
type: 'Bearer',
property: 'token',
maxAge: 365 * 24 * 60 * 60, // 1 year
}
```

- `headerName` Tên header đính kèm access token khi gọi API.
- `type` Tên loại token.
- `property` Tên key của token dùng để đọc vào object response từ api login. Như trong [ví dụ ở trên](#login) thì `property` có giá trị là `access_token`.
- `maxAge` Set giá trị `max-age` cho access token. Nếu token là JWT thì maxAge sẽ được sử dụng để tính toán dựa trên thời gian hết hạn của token. Ngược lại, giá trị `maxAge` này hoặc option `cookie.maxAge` sẽ được sử dụng để tính toán thời gian hết hạn của token.

## `refreshToken`

```ts
refreshToken: {
paramName: 'token',
property: 'refresh_token',
maxAge: 365 * 24 * 60 * 60, // 1 year
}
```

- `paramName` Tên của param khi gọi API refresh token.
- `property` Tên key của token dùng để đọc vào object response từ api login & refresh token. Như trong [ví dụ ở trên](#login) thì `property` có giá trị là `refresh_token`.
- `maxAge` Set giá trị `max-age` cho refresh token. Nếu token là JWT thì maxAge sẽ được sử dụng để tính toán dựa trên thời gian hết hạn của token. Ngược lại, giá trị `maxAge` này hoặc option `cookie.maxAge` sẽ được sử dụng để tính toán thời gian hết hạn của token.

:::tip
Hiện tại module này yêu cầu API login và refresh token trả về cùng một property chứa refresh token.
Expand All @@ -172,13 +176,15 @@ Hiện tại module này yêu cầu API login và refresh token trả về cùng
cookie: {
ssl: false,
maxAge: 365 * 24 * 60 * 60, // 1 year
domain: '',
path: '/',
}
```

- `ssl` Set `true` to enable `Secure` flag for cookies (khuyến khích sử dụng nếu có thể).


- `maxAge` Set giá trị `max-age` cho cookies. Hãy đảm bảo giá trị `maxAge` lớn hơn hoặc bằng thời gian expires của access token và refresh token.
- `domain` Tên domain của cookie.
- `path` Đường dẫn của cookie.

## `middleware`

Expand Down

0 comments on commit 1f7cc90

Please sign in to comment.