Skip to content

Commit 4a0f039

Browse files
authored
Merge pull request #104 from privy-open-source/feat/middleware
feat: middleware
2 parents b008c3d + 2a7ad88 commit 4a0f039

File tree

9 files changed

+90
-12
lines changed

9 files changed

+90
-12
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,29 @@ export default defineNuxtConfig({
163163
})
164164
```
165165

166+
## Middleware Guard
167+
168+
Since `0.7.0`, NuAuth include global middleware which redirect to login page if user not authenticated.
169+
If you want disabled it in some page, you can use set meta page `auth` to `false`.
170+
171+
```vue
172+
<script setup lang="ts">
173+
import { definePageMeta } from '#imports'
174+
175+
definePageMeta({ auth: false })
176+
</script>
177+
```
178+
179+
Or you can set auth profile using it
180+
181+
```vue
182+
<script setup lang="ts">
183+
import { definePageMeta } from '#imports'
184+
185+
definePageMeta({ auth: 'github' })
186+
</script>
187+
```
188+
166189
## Contribution
167190

168191
- Clone this repository

playground/middleware/auth.global.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

playground/pages/doc/index.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<template>
2+
<h1>doc/index</h1>
3+
<NuxtPage />
4+
</template>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<template>
2+
<h1>doc/detail</h1>
3+
<NuxtPage />
4+
</template>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<h1>doc/detail/history</h1>
3+
</template>

playground/pages/iframe.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@
66
height="500px" />
77
</div>
88
</template>
9+
10+
<script setup lang="ts">
11+
import { definePageMeta } from '#imports'
12+
13+
definePageMeta({ auth: false })
14+
</script>

src/core/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ export interface NuAuth {
5858
refresh: () => Promise<string>,
5959
}
6060

61-
export function useNuAuth (profile_?: string): NuAuth {
61+
export function useNuAuth (authProfile?: string): NuAuth {
6262
const config = useRuntimeConfig()
63-
const profile = String(profile_ ?? config.public.defaultProfile ?? 'oauth')
63+
const profile = String(authProfile ?? config.public.defaultProfile ?? 'oauth')
6464
const event = useRequestEvent()
6565
const token = useState<string | undefined>(hash(`${profile}/token`), () => event && getCookie(event, `${profile}/token`))
6666
const refreshToken = useState<string | undefined>(hash(`${profile}/rtoken`), () => event && getCookie(event, `${profile}/refresh-token`))

src/module.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { defu } from 'defu'
22
import type { CookieSerializeOptions } from 'cookie-es'
33
import {
44
addImports,
5+
addRouteMiddleware,
56
addServerHandler,
67
createResolver,
78
defineNuxtModule,
@@ -41,6 +42,11 @@ export interface ModuleOptions {
4142
*/
4243
names?: string[],
4344
},
45+
/**
46+
* Add guard middleware
47+
* @default true
48+
*/
49+
middleware?: boolean,
4450
}
4551

4652
export default defineNuxtModule<ModuleOptions>({
@@ -61,6 +67,7 @@ export default defineNuxtModule<ModuleOptions>({
6167
default: 'oauth',
6268
names : ['oauth'],
6369
},
70+
middleware: true,
6471
},
6572
setup (options, nuxt) {
6673
const { resolve } = createResolver(import.meta.url)
@@ -79,5 +86,13 @@ export default defineNuxtModule<ModuleOptions>({
7986
from: resolve('./core/index'),
8087
})
8188
}
89+
90+
if (options.middleware) {
91+
addRouteMiddleware({
92+
global: true,
93+
name : 'nuauth',
94+
path : resolve('./runtime/middleware'),
95+
})
96+
}
8297
},
8398
})

src/runtime/middleware.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* eslint-disable @typescript-eslint/promise-function-async */
2+
import { defineNuxtRouteMiddleware } from '#imports'
3+
import { useNuAuth } from '@privyid/nuauth/core'
4+
5+
declare module '#app' {
6+
interface PageMeta {
7+
/**
8+
* Enable auth this page
9+
* @default true
10+
*
11+
* @example
12+
* // Enable auth
13+
* auth: true
14+
*
15+
* // Enable auth with other profile
16+
* auth: 'github'
17+
*
18+
* // Disable auth
19+
* auth: false
20+
*/
21+
auth?: boolean | string,
22+
}
23+
}
24+
25+
export default defineNuxtRouteMiddleware((to) => {
26+
if (to.meta.auth !== false) {
27+
const profile = typeof to.meta.auth === 'string' ? to.meta.auth : undefined
28+
const { token, login } = useNuAuth(profile)
29+
30+
if (!token.value)
31+
return login(to.fullPath)
32+
}
33+
})

0 commit comments

Comments
 (0)