@phila/sso-nuxt 0.0.3 → 0.0.4
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
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# @phila/sso-nuxt
|
|
2
|
+
|
|
3
|
+
Nuxt 3 module for Azure AD B2C authentication. Installs Pinia, registers a client plugin, auto-imports `useAuth` and `useSSOStore`, and optionally guards all routes.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @phila/sso-nuxt @phila/sso-core @phila/sso-vue @azure/msal-browser
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Setup
|
|
12
|
+
|
|
13
|
+
Add the module to your `nuxt.config.ts`:
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
export default defineNuxtConfig({
|
|
17
|
+
modules: ["@phila/sso-nuxt"],
|
|
18
|
+
|
|
19
|
+
sso: {
|
|
20
|
+
globalAuth: true,
|
|
21
|
+
loginPath: "/login",
|
|
22
|
+
excludePaths: ["/logout"],
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Module Options
|
|
28
|
+
|
|
29
|
+
| Option | Type | Default | Description |
|
|
30
|
+
| --------------------- | ---------- | ------------------------- | ----------------------------------------------------------------------- |
|
|
31
|
+
| `globalAuth` | `boolean` | `true` | Register a global route middleware that redirects unauthenticated users |
|
|
32
|
+
| `loginPath` | `string` | `"/login"` | Path to redirect unauthenticated users to |
|
|
33
|
+
| `excludePaths` | `string[]` | `["/logout"]` | Paths that bypass the auth middleware (matched with `startsWith`) |
|
|
34
|
+
| `signInPolicy` | `string` | `"B2C_1A_AD_SIGNIN_ONLY"` | B2C sign-in user flow |
|
|
35
|
+
| `resetPasswordPolicy` | `string` | `"B2C_1A_PASSWORDRESET"` | B2C password reset user flow |
|
|
36
|
+
|
|
37
|
+
## Runtime Configuration
|
|
38
|
+
|
|
39
|
+
The module exposes all SSO settings via Nuxt runtime config. Set them in `nuxt.config.ts` or override with environment variables:
|
|
40
|
+
|
|
41
|
+
| Runtime Config Key | Environment Variable | Default |
|
|
42
|
+
| ------------------------ | --------------------------------------- | ---------------------------- |
|
|
43
|
+
| `ssoClientId` | `NUXT_PUBLIC_SSO_CLIENT_ID` | `""` |
|
|
44
|
+
| `ssoRedirectUri` | `NUXT_PUBLIC_SSO_REDIRECT_URI` | `"http://localhost:3000"` |
|
|
45
|
+
| `ssoTenant` | `NUXT_PUBLIC_SSO_TENANT` | `"PhilaB2CDev"` |
|
|
46
|
+
| `ssoAuthorityDomain` | `NUXT_PUBLIC_SSO_AUTHORITY_DOMAIN` | `"PhilaB2CDev.b2clogin.com"` |
|
|
47
|
+
| `ssoApiScope` | `NUXT_PUBLIC_SSO_API_SCOPE` | `""` |
|
|
48
|
+
| `ssoSignInPolicy` | `NUXT_PUBLIC_SSO_SIGN_IN_POLICY` | from module options |
|
|
49
|
+
| `ssoResetPasswordPolicy` | `NUXT_PUBLIC_SSO_RESET_PASSWORD_POLICY` | from module options |
|
|
50
|
+
| `ssoLoginPath` | `NUXT_PUBLIC_SSO_LOGIN_PATH` | from module options |
|
|
51
|
+
| `ssoExcludePaths` | `NUXT_PUBLIC_SSO_EXCLUDE_PATHS` | from module options |
|
|
52
|
+
|
|
53
|
+
Example `.env`:
|
|
54
|
+
|
|
55
|
+
```env
|
|
56
|
+
NUXT_PUBLIC_SSO_CLIENT_ID=your-client-id
|
|
57
|
+
NUXT_PUBLIC_SSO_TENANT=YourTenant
|
|
58
|
+
NUXT_PUBLIC_SSO_AUTHORITY_DOMAIN=YourTenant.b2clogin.com
|
|
59
|
+
NUXT_PUBLIC_SSO_REDIRECT_URI=http://localhost:3000
|
|
60
|
+
NUXT_PUBLIC_SSO_API_SCOPE=https://YourTenant.onmicrosoft.com/api/read
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### API Scopes
|
|
64
|
+
|
|
65
|
+
`ssoApiScope` accepts a comma-separated string. The module splits it into an array at runtime:
|
|
66
|
+
|
|
67
|
+
```env
|
|
68
|
+
NUXT_PUBLIC_SSO_API_SCOPE=https://tenant.onmicrosoft.com/api/read,https://tenant.onmicrosoft.com/api/write
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Auto-Imports
|
|
72
|
+
|
|
73
|
+
The module auto-imports these composables globally:
|
|
74
|
+
|
|
75
|
+
- `useAuth()` — primary auth composable (state, actions, utilities)
|
|
76
|
+
- `useSSOStore()` — direct Pinia store access
|
|
77
|
+
|
|
78
|
+
No `import` statements needed in your components:
|
|
79
|
+
|
|
80
|
+
```vue
|
|
81
|
+
<script setup>
|
|
82
|
+
const { isAuthenticated, userName, authReady, signIn, signOut } = useAuth();
|
|
83
|
+
</script>
|
|
84
|
+
|
|
85
|
+
<template>
|
|
86
|
+
<div v-if="!authReady">Loading...</div>
|
|
87
|
+
<div v-else-if="isAuthenticated">
|
|
88
|
+
<p>Welcome, {{ userName }}</p>
|
|
89
|
+
<button @click="signOut()">Sign out</button>
|
|
90
|
+
</div>
|
|
91
|
+
<div v-else>
|
|
92
|
+
<button @click="signIn()">Sign in</button>
|
|
93
|
+
</div>
|
|
94
|
+
</template>
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Route Middleware
|
|
98
|
+
|
|
99
|
+
When `globalAuth: true` (default), the module registers a global route middleware that:
|
|
100
|
+
|
|
101
|
+
1. Waits for auth initialization (`authReady`) before making decisions
|
|
102
|
+
2. Allows navigation to `loginPath` and all `excludePaths`
|
|
103
|
+
3. Redirects unauthenticated users to `loginPath`
|
|
104
|
+
|
|
105
|
+
### Excluding Paths
|
|
106
|
+
|
|
107
|
+
Paths in `excludePaths` are matched using `startsWith`, so `/public` also matches `/public/about`:
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
export default defineNuxtConfig({
|
|
111
|
+
sso: {
|
|
112
|
+
excludePaths: ["/logout", "/public", "/callback"],
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
The `loginPath` is always excluded automatically.
|
|
118
|
+
|
|
119
|
+
### Disabling the Middleware
|
|
120
|
+
|
|
121
|
+
Set `globalAuth: false` to disable the global middleware and handle auth checks manually:
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
export default defineNuxtConfig({
|
|
125
|
+
sso: {
|
|
126
|
+
globalAuth: false,
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## What the Module Does
|
|
132
|
+
|
|
133
|
+
During setup, the module automatically:
|
|
134
|
+
|
|
135
|
+
1. Installs `@pinia/nuxt` (apps don't need to add it separately)
|
|
136
|
+
2. Adds `@phila/sso-core` and `@phila/sso-vue` to the Vite transpile list
|
|
137
|
+
3. Configures Vite dev server to serve files from the monorepo root
|
|
138
|
+
4. Declares all SSO keys in `runtimeConfig.public`
|
|
139
|
+
5. Registers a client-only plugin that initializes the B2C auth client
|
|
140
|
+
6. Auto-imports `useAuth` and `useSSOStore`
|
|
141
|
+
7. Registers the global auth route middleware (when `globalAuth: true`)
|
|
142
|
+
|
|
143
|
+
## License
|
|
144
|
+
|
|
145
|
+
MIT
|
|
@@ -4,8 +4,10 @@ const middleware_auth = defineNuxtRouteMiddleware((to) => {
|
|
|
4
4
|
const config = useRuntimeConfig().public;
|
|
5
5
|
const store = useSSOStore();
|
|
6
6
|
if (!store.authReady) return;
|
|
7
|
-
const
|
|
8
|
-
|
|
7
|
+
const rawExclude = config.ssoExcludePaths;
|
|
8
|
+
const excludePaths = Array.isArray(rawExclude) ? rawExclude : [];
|
|
9
|
+
const excluded = [config.ssoLoginPath, ...excludePaths];
|
|
10
|
+
if (excluded.some((p) => p && to.path.startsWith(p))) return;
|
|
9
11
|
if (!store.isAuthenticated) return navigateTo({ path: config.ssoLoginPath });
|
|
10
12
|
});
|
|
11
13
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.auth.js","sources":["../../src/runtime/middleware.auth.ts"],"sourcesContent":["// ABOUTME: Route middleware registered by @phila/sso-nuxt module\n// ABOUTME: Redirects unauthenticated users to the configured login path\nimport { defineNuxtRouteMiddleware, useRuntimeConfig, navigateTo } from \"nuxt/app\";\nimport { useSSOStore } from \"@phila/sso-vue\";\n\nexport default defineNuxtRouteMiddleware(to => {\n const config = useRuntimeConfig().public;\n const store = useSSOStore();\n\n // Don't redirect while auth is still initialising\n if (!store.authReady) return;\n\n const excluded = [config.ssoLoginPath as string, ...
|
|
1
|
+
{"version":3,"file":"middleware.auth.js","sources":["../../src/runtime/middleware.auth.ts"],"sourcesContent":["// ABOUTME: Route middleware registered by @phila/sso-nuxt module\n// ABOUTME: Redirects unauthenticated users to the configured login path\nimport { defineNuxtRouteMiddleware, useRuntimeConfig, navigateTo } from \"nuxt/app\";\nimport { useSSOStore } from \"@phila/sso-vue\";\n\nexport default defineNuxtRouteMiddleware(to => {\n const config = useRuntimeConfig().public;\n const store = useSSOStore();\n\n // Don't redirect while auth is still initialising\n if (!store.authReady) return;\n\n const rawExclude = config.ssoExcludePaths;\n const excludePaths = Array.isArray(rawExclude) ? rawExclude : [];\n const excluded = [config.ssoLoginPath as string, ...excludePaths];\n if (excluded.some(p => p && to.path.startsWith(p))) return;\n\n if (!store.isAuthenticated) return navigateTo({ path: config.ssoLoginPath as string });\n});\n"],"names":[],"mappings":";;AAKA,MAAA,kBAAe,0BAA0B,CAAA,OAAM;AAC7C,QAAM,SAAS,mBAAmB;AAClC,QAAM,QAAQ,YAAA;AAGd,MAAI,CAAC,MAAM,UAAW;AAEtB,QAAM,aAAa,OAAO;AAC1B,QAAM,eAAe,MAAM,QAAQ,UAAU,IAAI,aAAa,CAAA;AAC9D,QAAM,WAAW,CAAC,OAAO,cAAwB,GAAG,YAAY;AAChE,MAAI,SAAS,KAAK,CAAA,MAAK,KAAK,GAAG,KAAK,WAAW,CAAC,CAAC,EAAG;AAEpD,MAAI,CAAC,MAAM,gBAAiB,QAAO,WAAW,EAAE,MAAM,OAAO,cAAwB;AACvF,CAAC;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@phila/sso-nuxt",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@nuxt/kit": "^3.0.0",
|
|
20
20
|
"defu": "^6.0.0",
|
|
21
|
-
"@phila/sso-core": "0.0.
|
|
22
|
-
"@phila/sso-vue": "0.0.
|
|
21
|
+
"@phila/sso-core": "0.0.4",
|
|
22
|
+
"@phila/sso-vue": "0.0.4"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
25
|
"nuxt": "^3.0.0"
|