Skip to content

Commit c20ef8d

Browse files
resleark-taro56ndom91
authored
chore(docs): add express credentials example (#11097)
Co-authored-by: Kawahara Shotaro <[email protected]> Co-authored-by: Nico Domino <[email protected]>
1 parent a617edb commit c20ef8d

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

docs/pages/getting-started/authentication/credentials.mdx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,50 @@ export { handle } from "./auth"
102102
```
103103

104104
</Code.Svelte>
105+
106+
<Code.Express>
107+
108+
```ts filename="./src/routes/auth.route.ts" {2, 11}
109+
import { ExpressAuth } from '@auth/express'
110+
import Credentials from '@auth/express/providers/credentials'
111+
import express from "express"
112+
// Your own logic for dealing with plaintext password strings; be careful!
113+
import { saltAndHashPassword } from "@/utils/password"
114+
115+
const app = express()
116+
app.use("/auth/*", ExpressAuth({
117+
providers: [
118+
Credentials({
119+
// You can specify which fields should be submitted, by adding keys to the `credentials` object.
120+
// e.g. domain, username, password, 2FA token, etc.
121+
credentials: {
122+
email: {},
123+
password: {},
124+
},
125+
authorize: async (credentials) => {
126+
let user = null
127+
128+
// logic to salt and hash password
129+
const pwHash = saltAndHashPassword(credentials.password)
130+
131+
// logic to verify if user exists
132+
user = await getUserFromDb(credentials.email, pwHash)
133+
134+
if (!user) {
135+
// No user found, so this is their first attempt to login
136+
// meaning this is also the place you could do registration
137+
throw new Error("User not found.")
138+
}
139+
140+
// return user object with the their profile data
141+
return user
142+
},
143+
}),
144+
],
145+
}))
146+
```
147+
148+
</Code.Express>
105149
</Code>
106150

107151
<Callout type="info">

0 commit comments

Comments
 (0)