Skip to content

Commit 262db25

Browse files
docs: improve documentation in Credentials provider (#12873)
Co-authored-by: Thang Vu <hi@thvu.dev>
1 parent 1266520 commit 262db25

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

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

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,53 @@ so there are no plans to remove this provider.
3535

3636
### Credentials Provider
3737

38-
First, let's initialize the `Credentials` provider in the Auth.js configuration file. You'll have to import the provider and add it to your `providers` array.
38+
To use the `Credentials Provider`, you’ll first need to import and configure it in your `Auth.js` setup. This provider allows you to implement custom login logic based on form input values.
39+
40+
Here’s how to set it up:
41+
42+
1. Import the provider.
43+
2. Add it to the `providers` array in your `Auth.js` config.
44+
3. Define the `credentials` and `authorize` fields.
45+
46+
#### `credentials`
47+
48+
The `credentials` object defines the input fields displayed on the default sign-in page. These inputs are automatically rendered on the route:
49+
50+
- `/api/auth/signin` (Next.js)
51+
- `/auth/signin` (Other frameworks)
52+
53+
Each field accepts the following properties:
54+
55+
- `label`: Input label
56+
- `type`: HTML input type (`text`, `password`, etc.)
57+
- `placeholder`: Placeholder text
58+
59+
> These fields are also passed to the `authorize` function under the `credentials` argument.
60+
61+
For more details, see the [Built-in Pages guide](https://authjs.dev/guides/pages/built-in-pages).
62+
63+
```ts
64+
Credentials({
65+
credentials: {
66+
email: {
67+
type: "email",
68+
label: "Email",
69+
placeholder: "johndoe@gmail.com",
70+
},
71+
password: {
72+
type: "password",
73+
label: "Password",
74+
placeholder: "*****",
75+
},
76+
},
77+
})
78+
```
79+
80+
#### `authorize`
81+
82+
The `authorize` function handles the custom login logic and determines whether the credentials provided are valid.
83+
84+
It receives the input values defined in `credentials`, and you must return either a user object or `null`. If `null` is returned, the login fails.
3985

4086
<Code>
4187
<Code.Next>
@@ -541,3 +587,14 @@ export const { handle } = SvelteKitAuth({
541587

542588
</Code.Svelte>
543589
</Code>
590+
591+
<Callout>
592+
593+
When authorize returns null, Auth.js handles the error in one of two ways:
594+
595+
Using built-in pages:
596+
597+
- The user is redirected to the login page with the query string: `?error=CredentialsSignin&code=credentials`. You can customize the code using the [credentials provider options](https://authjs.dev/reference/core/providers/credentials#returns).
598+
- Using form actions or custom error handling (e.g., in Remix, SvelteKit): The error is thrown as credentialssignin and must be caught manually in your server action. See more in the [Auth.js error reference](https://authjs.dev/reference/core/errors#credentialssignin).
599+
600+
</Callout>

0 commit comments

Comments
 (0)