nuxt-auth-utils 0.0.21 → 0.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -21
- package/dist/module.json +1 -1
- package/dist/runtime/server/api/session.get.d.ts +1 -1
- package/dist/runtime/server/lib/oauth/microsoft.d.ts +6 -0
- package/dist/runtime/server/lib/oauth/microsoft.mjs +1 -1
- package/dist/runtime/server/utils/session.d.ts +3 -5
- package/dist/runtime/types/index.d.ts +1 -1
- package/dist/runtime/types/session.d.ts +3 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,30 +23,13 @@ This module only works with SSR (server-side rendering) enabled as it uses serve
|
|
|
23
23
|
|
|
24
24
|
## Quick Setup
|
|
25
25
|
|
|
26
|
-
1. Add `nuxt-auth-utils`
|
|
26
|
+
1. Add `nuxt-auth-utils` in your Nuxt project
|
|
27
27
|
|
|
28
28
|
```bash
|
|
29
|
-
|
|
30
|
-
pnpm add -D nuxt-auth-utils
|
|
31
|
-
|
|
32
|
-
# Using yarn
|
|
33
|
-
yarn add --dev nuxt-auth-utils
|
|
34
|
-
|
|
35
|
-
# Using npm
|
|
36
|
-
npm install --save-dev nuxt-auth-utils
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
2. Add `nuxt-auth-utils` to the `modules` section of `nuxt.config.ts`
|
|
40
|
-
|
|
41
|
-
```js
|
|
42
|
-
export default defineNuxtConfig({
|
|
43
|
-
modules: [
|
|
44
|
-
'nuxt-auth-utils'
|
|
45
|
-
]
|
|
46
|
-
})
|
|
29
|
+
npx nuxi@latest module add auth-utils
|
|
47
30
|
```
|
|
48
31
|
|
|
49
|
-
|
|
32
|
+
2. Add a `NUXT_SESSION_PASSWORD` env variable with at least 32 characters in the `.env`.
|
|
50
33
|
|
|
51
34
|
```bash
|
|
52
35
|
# .env
|
|
@@ -57,7 +40,6 @@ Nuxt Auth Utils generates one for you when running Nuxt in development the first
|
|
|
57
40
|
|
|
58
41
|
3. That's it! You can now add authentication to your Nuxt app ✨
|
|
59
42
|
|
|
60
|
-
|
|
61
43
|
## Vue Composables
|
|
62
44
|
|
|
63
45
|
Nuxt Auth Utils automatically adds some plugins to fetch the current user session to let you access it from your Vue components.
|
|
@@ -116,6 +98,7 @@ const session = await requireUserSession(event)
|
|
|
116
98
|
You can define the type for your user session by creating a type declaration file (for example, `auth.d.ts`) in your project to augment the `UserSession` type:
|
|
117
99
|
|
|
118
100
|
```ts
|
|
101
|
+
// auth.d.ts
|
|
119
102
|
declare module '#auth-utils' {
|
|
120
103
|
interface User {
|
|
121
104
|
// Add your own fields
|
|
@@ -222,6 +205,37 @@ export default defineNitroPlugin(() => {
|
|
|
222
205
|
})
|
|
223
206
|
```
|
|
224
207
|
|
|
208
|
+
## Configuration
|
|
209
|
+
|
|
210
|
+
We leverage `runtimeConfig.session` to give the defaults option to [h3 `useSession`](https://h3.unjs.io/examples/handle-session).
|
|
211
|
+
|
|
212
|
+
You can overwrite the options in your `nuxt.config.ts`:
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
export default defineNuxtConfig({
|
|
216
|
+
modules: ['nuxt-auth-utils'],
|
|
217
|
+
runtimeConfig: {
|
|
218
|
+
session: {
|
|
219
|
+
maxAge: 60 * 60 * 24 * 7 // 1 week
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
})
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
Our defaults are:
|
|
226
|
+
|
|
227
|
+
```ts
|
|
228
|
+
{
|
|
229
|
+
name: 'nuxt-session',
|
|
230
|
+
password: process.env.NUXT_SESSION_PASSWORD || '',
|
|
231
|
+
cookie: {
|
|
232
|
+
sameSite: 'lax'
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Checkout the [`SessionConfig`](https://github.com/unjs/h3/blob/c04c458810e34eb15c1647e1369e7d7ef19f567d/src/utils/session.ts#L20) for all options.
|
|
238
|
+
|
|
225
239
|
## Development
|
|
226
240
|
|
|
227
241
|
```bash
|
package/dist/module.json
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<
|
|
1
|
+
declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<UserSessionRequired>>;
|
|
2
2
|
export default _default;
|
|
@@ -44,6 +44,12 @@ export interface OAuthMicrosoftConfig {
|
|
|
44
44
|
* @see https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow
|
|
45
45
|
*/
|
|
46
46
|
authorizationParams?: Record<string, string>;
|
|
47
|
+
/**
|
|
48
|
+
* Redirect URL to prevent in prod prevent redirect_uri mismatch http to https
|
|
49
|
+
* @default process.env.NUXT_OAUTH_MICROSOFT_REDIRECT_URL
|
|
50
|
+
* @see https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow
|
|
51
|
+
*/
|
|
52
|
+
redirectUrl?: string;
|
|
47
53
|
}
|
|
48
54
|
interface OAuthConfig {
|
|
49
55
|
config?: OAuthMicrosoftConfig;
|
|
@@ -20,7 +20,7 @@ export function microsoftEventHandler({ config, onSuccess, onError }) {
|
|
|
20
20
|
}
|
|
21
21
|
const authorizationURL = config.authorizationURL || `https://login.microsoftonline.com/${config.tenant}/oauth2/v2.0/authorize`;
|
|
22
22
|
const tokenURL = config.tokenURL || `https://login.microsoftonline.com/${config.tenant}/oauth2/v2.0/token`;
|
|
23
|
-
const redirectUrl = getRequestURL(event).href;
|
|
23
|
+
const redirectUrl = config.redirectUrl || getRequestURL(event).href;
|
|
24
24
|
if (!code) {
|
|
25
25
|
const scope = config.scope && config.scope.length > 0 ? config.scope : ["User.Read"];
|
|
26
26
|
return sendRedirect(
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { H3Event } from 'h3';
|
|
2
|
-
import type {
|
|
2
|
+
import type { UserSession, UserSessionRequired } from '#auth-utils';
|
|
3
3
|
export interface SessionHooks {
|
|
4
4
|
/**
|
|
5
5
|
* Called when fetching the session from the API
|
|
6
6
|
* - Add extra properties to the session
|
|
7
7
|
* - Throw an error if the session could not be verified (with a database for example)
|
|
8
8
|
*/
|
|
9
|
-
'fetch': (session:
|
|
9
|
+
'fetch': (session: UserSessionRequired, event: H3Event) => void | Promise<void>;
|
|
10
10
|
/**
|
|
11
11
|
* Called before clearing the session
|
|
12
12
|
*/
|
|
@@ -27,6 +27,4 @@ export declare function setUserSession(event: H3Event, data: UserSession): Promi
|
|
|
27
27
|
*/
|
|
28
28
|
export declare function replaceUserSession(event: H3Event, data: UserSession): Promise<UserSession>;
|
|
29
29
|
export declare function clearUserSession(event: H3Event): Promise<boolean>;
|
|
30
|
-
export declare function requireUserSession(event: H3Event): Promise<
|
|
31
|
-
user: User;
|
|
32
|
-
}>;
|
|
30
|
+
export declare function requireUserSession(event: H3Event): Promise<UserSessionRequired>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export type { User, UserSession, UserSessionComposable } from './session';
|
|
1
|
+
export type { User, UserSession, UserSessionRequired, UserSessionComposable } from './session';
|
|
2
2
|
export type { OAuthConfig } from './oauth-config';
|
|
@@ -4,6 +4,9 @@ export interface User {
|
|
|
4
4
|
export interface UserSession {
|
|
5
5
|
user?: User;
|
|
6
6
|
}
|
|
7
|
+
export interface UserSessionRequired extends UserSession {
|
|
8
|
+
user: User;
|
|
9
|
+
}
|
|
7
10
|
export interface UserSessionComposable {
|
|
8
11
|
loggedIn: ComputedRef<boolean>;
|
|
9
12
|
user: ComputedRef<User | null>;
|