Skip to content

Commit a1522c5

Browse files
authored
feat: Make use session sync, use a plugin based approach for session fetching, add auth middleware, add lastRefreshedAt property to session(#69)
* feat: use more h3 native utilities in nuxt auth handler * cleanup: move fetch and url helpers to utils, improve some typing * cleanup: restructure useSession to prepare for sync, move url and navigate utiltiy to urls.ts * feat: move all state to one composable, resolve empty object todo, start on pplugin * feat: switch to plugin based approach fully * cleanup: no more async _fetch as its unused * cleanup/feat!: rename lastSync to lastRefreshedAt and expose + document it, make useSession data read-only, improve docs by expanding getting started + configuration section + credit @JoaoPedroAS51 + fix type check command, add lastRefrehsedAt to app.vue, add a new example page to the playground that will always be unprotected, remove now() util in favor of a real date, make `getCsrfToken` return the token right away * fix: use `callWithNuxt` in nested composable calls * polishing: improve docs feature section, return promise from async function firectly * refactor: use url based on request event on client * refactor: only get session if it was not fetched server-side * refactor: dont make status readonly, return promise directly * refactor: stop using deprecated utility during module setup * feat: add note to local middleware docs, a bit better logging by module setup * polish: take over some env var config from next-auth nuxt example
1 parent c27d5a2 commit a1522c5

File tree

24 files changed

+524
-402
lines changed

24 files changed

+524
-402
lines changed

README.md

Lines changed: 82 additions & 92 deletions
Large diffs are not rendered by default.

docs/content/1.welcome/1.index.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ Done! You can now use all user-related functionality, for example:
4141
::code-group
4242
```ts [Application side]
4343
// file: e.g ~/pages/login.vue
44-
const { status, data, signIn, signOut } = await useSession({
45-
// Whether a session is required. If it is, a redirect to the signin page will happen if no active session exists
46-
required: true
47-
})
44+
const { status, data, signIn, signOut } = useSession()
4845

4946
status.value // Session status: `unauthenticated`, `loading`, `authenticated`
5047
data.value // Session data, e.g., expiration, user.email, ...

docs/content/4.usage/0.index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ To create your custom sign-in page you can use `signIn` to directly start a prov
106106
</template>
107107
108108
<script setup lang="ts">
109-
const { signIn } = await useSession({ required: false })
109+
const { signIn } = useSession()
110110
</script>
111111
```
112112

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@nuxt/kit": "^3.0.0-rc.12",
3030
"defu": "^6.1.0",
3131
"next-auth": "^4.14.0",
32+
"requrl": "^3.0.2",
3233
"ufo": "^1.0.0"
3334
},
3435
"devDependencies": {

playground/app.vue

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<p>See all available authentication & session information below. Navigate to different sub-pages to test out the app.</p>
55
<pre>Status: {{ status }}</pre>
66
<pre>Data: {{ data || 'no session data present, are you logged in?' }}</pre>
7+
<pre>Last refreshed at: {{ lastRefreshedAt || 'no refresh happened' }}</pre>
78
<pre>Decoded JWT token: {{ token || 'no token present, are you logged in?' }}</pre>
89
<pre>CSRF Token: {{ csrfToken }}</pre>
910
<pre>Providers: {{ providers }}</pre>
@@ -19,12 +20,12 @@
1920
-> globally protected page
2021
</nuxt-link>
2122
<br>
22-
<nuxt-link to="/protected/inline">
23-
-> inline protected page
23+
<nuxt-link to="/protected/locally">
24+
-> locally protected page
2425
</nuxt-link>
2526
<br>
26-
<nuxt-link to="/protected/named">
27-
-> named protected page
27+
<nuxt-link to="/always-unprotected">
28+
-> page that is always unprotected
2829
</nuxt-link>
2930
<br>
3031
<nuxt-link to="/api/protected/inline" external>
@@ -45,7 +46,7 @@
4546
<script setup lang="ts">
4647
import { useSession, useRoute, useFetch, useRequestHeaders } from '#imports'
4748
48-
const { data, status, getCsrfToken, getProviders } = await useSession({ required: false })
49+
const { data, status, lastRefreshedAt, getCsrfToken, getProviders } = useSession()
4950
5051
const providers = await getProviders()
5152
const csrfToken = await getCsrfToken()

playground/middleware/auth.global.ts

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

playground/middleware/auth.ts

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

playground/nuxt.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@ import NuxtAuth from '..'
22

33
export default defineNuxtConfig({
44
// @ts-expect-error See https://github.com/nuxt/framework/issues/8931
5-
modules: [NuxtAuth]
5+
modules: [NuxtAuth],
6+
auth: {
7+
enableGlobalAppMiddleware: true
8+
}
69
})
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<template>
2-
<div>I'm a secret! My protection works via an named middleware.</div>
2+
<div>I'm always public, even when the global auth middleware is enabled!</div>
33
</template>
44

55
<script setup lang="ts">
66
import { definePageMeta } from '#imports'
77
88
definePageMeta({
9-
middleware: ['auth']
9+
auth: false
1010
})
1111
</script>

0 commit comments

Comments
 (0)