Skip to content

Commit c7337df

Browse files
committed
chore(docs): cleanup credentials provider docs and add custom error example
1 parent 6db1500 commit c7337df

File tree

1 file changed

+40
-26
lines changed

1 file changed

+40
-26
lines changed

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

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,9 @@ The Credentials provider allows you to handle signing in with arbitrary credenti
77

88
It is intended to support use cases where you have an existing system you need to authenticate users against, and therefore users authenticated in this manner are not persisted in the database.
99

10-
<Callout type="warning">
11-
OAuth providers spend significant amounts of money, time, and engineering effort to build:
12-
13-
- abuse detection (bot-protection, rate-limiting)
14-
- password management (password reset, credential stuffing, rotation)
15-
- data security (encryption/salting, strength validation)
10+
## Resources
1611

17-
and much more for authentication solutions. It is likely that your application would benefit from leveraging these battle-tested solutions rather than try to rebuild them from scratch.
18-
19-
If you'd still like to build password-based authentication for your application despite these risks, Auth.js gives you full control to do so.
20-
21-
</Callout>
22-
23-
<Callout type="warning">
24-
There is no validation on the user inputs by default. We recommend validating
25-
user input at runtime using a library like [Zod](https://zod.dev) or
26-
[Valibot](https://github.com/fabian-hiller/valibot).
27-
</Callout>
12+
- [Client-side Input Validation Example](/getting-started/authentication/credentials#verifying-data-with-zod)
2813

2914
## Configuration
3015

@@ -103,15 +88,44 @@ app.use("/auth/*", ExpressAuth({
10388
</Code.Express>
10489
</Code>
10590
106-
See the [callbacks documentation](/reference/core#authconfig#callbacks) for more information on how to interact with the token. For example, you can add additional information to the token by returning an object from the `jwt()` callback:
91+
### Custom Error Messages
92+
93+
You can throw a custom error in the `authorize` function to return a custom error message to the user.
94+
95+
```ts filename="@/auth.ts" /InvalidLoginError/
96+
import NextAuth, { CredentialsSignin } from "next-auth"
97+
import Credentials from "next-auth/providers/credentials"
10798

108-
```js
109-
callbacks: {
110-
async jwt({ token, user, account, profile, isNewUser }) {
111-
if (user) {
112-
token.id = user.id
113-
}
114-
return token
115-
}
99+
class InvalidLoginError extends CredentialsSignin {
100+
code = "Invalid identifier or password"
116101
}
102+
103+
export const { handlers, auth } = NextAuth({
104+
providers: [
105+
Credentials({
106+
credentials: {
107+
username: { label: "Username" },
108+
password: { label: "Password", type: "password" },
109+
},
110+
async authorize(credentials) {
111+
throw new InvalidLoginError()
112+
},
113+
}),
114+
],
115+
})
117116
```
117+
118+
You will then receive that custom error code in the query parameters of the signin page your user returns to after a failed login attempt, for example `https://app.company.com/auth/signin?error=CredentialsSignin&code=Invalid+identifier+or+password`.
119+
120+
<Callout type="warning">
121+
OAuth providers spend significant amounts of money, time, and engineering effort to build:
122+
123+
- abuse detection (bot-protection, rate-limiting)
124+
- password management (password reset, credential stuffing, rotation)
125+
- data security (encryption/salting, strength validation)
126+
127+
and much more for authentication solutions. It is likely that your application would benefit from leveraging these battle-tested solutions rather than try to rebuild them from scratch.
128+
129+
If you'd still like to build password-based authentication for your application despite these risks, Auth.js gives you full control to do so.
130+
131+
</Callout>

0 commit comments

Comments
 (0)