Skip to content

Commit efd0d0b

Browse files
authored
feat: enable credential flow by switching to user-runtime config approach, add docs, improve some types (#10)
1 parent a126953 commit efd0d0b

File tree

16 files changed

+580
-452
lines changed

16 files changed

+580
-452
lines changed

.github/nuxt-user-demo.jpg

186 KB
Loading

.github/nuxt-user-demo.png

-435 KB
Binary file not shown.

README.md

Lines changed: 204 additions & 99 deletions
Large diffs are not rendered by default.

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"@nuxt/kit": "^3.0.0-rc.12",
3030
"defu": "^6.1.0",
3131
"nanoid": "^4.0.0",
32-
"next-auth": "^4.14.0"
32+
"next-auth": "^4.14.0",
33+
"ufo": "^0.8.6"
3334
},
3435
"devDependencies": {
3536
"@nuxt/module-builder": "^0.2.0",

playground/nuxt.config.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
1-
import { defineNuxtConfig } from 'nuxt/config'
2-
import GithubProvider from 'next-auth/providers/github'
31
import NuxtAuth from '..'
42

53
export default defineNuxtConfig({
6-
modules: [
7-
NuxtAuth
8-
],
4+
modules: [NuxtAuth],
95
auth: {
10-
nextAuth: {
11-
options: {
12-
providers: [
13-
GithubProvider({
14-
clientId: 'enter-your-client-id-here',
15-
clientSecret: 'enter-your-client-secret-here'
16-
})
17-
]
18-
}
19-
}
6+
origin: 'http://localhost:3000'
207
}
218
})

playground/pages/index.vue

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
<template>
22
<div>
3-
<button @click="signIn({ callbackUrl: '/'})">
4-
sign in with github
3+
<button @click="signIn(undefined, { callbackUrl: '/'})">
4+
sign in
55
</button>
6-
<button @click="signIn({ callbackUrl: '/protected/inline' })">
7-
sign in with redirect
6+
<br>
7+
<button @click="signIn('credentials', { callbackUrl: '/'})">
8+
sign in (credential)
89
</button>
10+
<br>
11+
<button @click="signIn('github', { callbackUrl: '/'})">
12+
sign in (github)
13+
</button>
14+
<br>
15+
<button @click="signIn(undefined, { callbackUrl: '/protected/inline' })">
16+
sign in (with redirect to protected page)
17+
</button>
18+
<br>
919
<button @click="signOut">
1020
sign out
1121
</button>
22+
<br>
1223
<button @click="getSession()">
1324
refresh session
1425
</button>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import CredentialsProvider from 'next-auth/providers/credentials'
2+
import GithubProvider from 'next-auth/providers/github'
3+
import { NuxtAuthHandler } from '#auth'
4+
5+
export default NuxtAuthHandler({
6+
providers: [
7+
// @ts-ignore Import is exported on .default during SSR, so we need to call it this way. May be fixed via Vite at some point
8+
GithubProvider.default({
9+
clientId: 'your-client-id',
10+
clientSecret: 'your-client-secret'
11+
}),
12+
// @ts-ignore Import is exported on .default during SSR, so we need to call it this way. May be fixed via Vite at some point
13+
CredentialsProvider.default({
14+
// The name to display on the sign in form (e.g. 'Sign in with...')
15+
name: 'Credentials',
16+
// The credentials is used to generate a suitable form on the sign in page.
17+
// You can specify whatever fields you are expecting to be submitted.
18+
// e.g. domain, username, password, 2FA token, etc.
19+
// You can pass any HTML attribute to the <input> tag through the object.
20+
credentials: {
21+
username: { label: 'Username', type: 'text', placeholder: 'jsmith' },
22+
password: { label: 'Password', type: 'password' }
23+
},
24+
authorize (credentials: any) {
25+
// You need to provide your own logic here that takes the credentials
26+
// submitted and returns either a object representing a user or value
27+
// that is false/null if the credentials are invalid.
28+
// e.g. return { id: 1, name: 'J Smith', email: 'jsmith@example.com' }
29+
// You can also use the `req` object to obtain additional parameters
30+
// (i.e., the request IP address)
31+
// eslint-disable-next-line no-console
32+
console.log('provided credentials: ', credentials)
33+
const user = { id: '1', name: 'J Smith', email: '[email protected]' }
34+
35+
if (user) {
36+
// Any object returned will be saved in `user` property of the JWT
37+
return user
38+
} else {
39+
// If you return null then an error will be displayed advising the user to check their details.
40+
return null
41+
42+
// You can also Reject this callback with an Error thus the user will be sent to the error page with the error message as a query parameter
43+
}
44+
}
45+
})
46+
]
47+
})

playground/server/api/protected/inline.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getServerSession } from '#sidebase/server'
1+
import { getServerSession } from '#auth'
22

33
export default eventHandler(async (event) => {
44
const session = await getServerSession(event)

playground/server/middleware/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getServerSession } from '#sidebase/server'
1+
import { getServerSession } from '#auth'
22

33
export default eventHandler(async (event) => {
44
// Only protect a certain backend route

0 commit comments

Comments
 (0)