Skip to content

Commit

Permalink
chore(docs): add express credentials example (#11097)
Browse files Browse the repository at this point in the history
Co-authored-by: Kawahara Shotaro <[email protected]>
Co-authored-by: Nico Domino <[email protected]>
  • Loading branch information
3 people authored Jun 8, 2024
1 parent a617edb commit c20ef8d
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions docs/pages/getting-started/authentication/credentials.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,50 @@ export { handle } from "./auth"
```

</Code.Svelte>

<Code.Express>

```ts filename="./src/routes/auth.route.ts" {2, 11}
import { ExpressAuth } from '@auth/express'
import Credentials from '@auth/express/providers/credentials'
import express from "express"
// Your own logic for dealing with plaintext password strings; be careful!
import { saltAndHashPassword } from "@/utils/password"

const app = express()
app.use("/auth/*", ExpressAuth({
providers: [
Credentials({
// You can specify which fields should be submitted, by adding keys to the `credentials` object.
// e.g. domain, username, password, 2FA token, etc.
credentials: {
email: {},
password: {},
},
authorize: async (credentials) => {
let user = null

// logic to salt and hash password
const pwHash = saltAndHashPassword(credentials.password)

// logic to verify if user exists
user = await getUserFromDb(credentials.email, pwHash)

if (!user) {
// No user found, so this is their first attempt to login
// meaning this is also the place you could do registration
throw new Error("User not found.")
}

// return user object with the their profile data
return user
},
}),
],
}))
```

</Code.Express>
</Code>

<Callout type="info">
Expand Down

0 comments on commit c20ef8d

Please sign in to comment.