Skip to content

Commit 7864d47

Browse files
authored
docs(adapter): mention new types (#1916)
Containts the following squashed commits: * docs(adapters): mention new types * docs(adapters): rename interface on example * docs(adapters): move section above * docs(adapters): fix casing * docs(adapters): fix example import * fix(www): Typescript -> TypeScript
1 parent 98dc82e commit 7864d47

File tree

2 files changed

+83
-24
lines changed

2 files changed

+83
-24
lines changed

www/docs/getting-started/typescript.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,43 @@ The types at [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyType
1414

1515
---
1616

17+
## Adapters
18+
19+
If you're writing your own custom Adapter, you can take advantage of the types to make sure your implementation conforms to what's expected:
20+
21+
```ts
22+
import type { Adapter } from "next-auth/adapters"
23+
24+
const MyAdapter: Adapter = () => {
25+
return {
26+
async getAdapter() {
27+
return {
28+
// your adapter methods here
29+
}
30+
},
31+
}
32+
}
33+
```
34+
35+
When writing your own custom Adapter in plain JavaScript, note that you can use **JSDoc** to get helpful editor hints and auto-completion like so:
36+
37+
```js
38+
/** @type { import("next-auth/adapters").Adapter } */
39+
const MyAdapter = () => {
40+
return {
41+
async getAdapter() {
42+
return {
43+
// your adapter methods here
44+
}
45+
},
46+
}
47+
}
48+
```
49+
50+
:::note
51+
This will work in code editors with a strong TypeScript integration like VSCode or WebStorm. It might not work if you're using more lightweight editors like VIM or Atom.
52+
:::
53+
1754
## Module Augmentation
1855

1956
`next-auth` comes with certain types/interfaces, that are shared across submodules. Good examples are `Session` and `JWT`. Ideally, you should only need to create these types at a single place, and TS should pick them up in every location where they are referenced. Luckily, this is exactly what Module Augmentation can do for us. Define your shared interfaces in a single location, and get type-safety across your application, when you use `next-auth` (or one of its submodules).

www/docs/schemas/adapters.md

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ title: Database Adapters
55

66
An **Adapter** in NextAuth.js connects your application to whatever database or backend system you want to use to store data for user accounts, sessions, etc.
77

8-
You do not need to specify an adapter explicitly unless you want to use advanced options such as custom models or schemas, if you want to use the Prisma adapter instead of the default TypeORM adapter, or if you are creating a custom adapter to connect to a database that is not one of the supported databases.
8+
You do not need to specify an Adapter explicitly unless you want to use advanced options such as custom models or schemas, if you want to use the Prisma Adapter instead of the default TypeORM Adapter, or if you are creating a custom Adapter to connect to a database that is not one of the supported databases.
99

1010
### Database Schemas
1111

1212
Configure your database by creating the tables and columns to match the schema expected by NextAuth.js.
1313

14-
* [MySQL Schema](/schemas/mysql)
15-
* [Postgres Schema](/schemas/postgres)
16-
* [Microsoft SQL Server Schema](/schemas/mssql)
14+
- [MySQL Schema](/schemas/mysql)
15+
- [Postgres Schema](/schemas/postgres)
16+
- [Microsoft SQL Server Schema](/schemas/mssql)
1717

1818
## TypeORM Adapter
1919

20-
NextAuth.js comes with a default adapter that uses [TypeORM](https://typeorm.io/) so that it can be used with many different databases without any further configuration, you simply add the node module for the database driver you want to use to your project and pass a database connection string to NextAuth.js.
20+
NextAuth.js comes with a default Adapter that uses [TypeORM](https://typeorm.io/) so that it can be used with many different databases without any further configuration, you simply add the node module for the database driver you want to use to your project and pass a database connection string to NextAuth.js.
2121

22-
The default adapter is the TypeORM adapter, the following configuration options are exactly equivalent.
22+
The default Adapter is the TypeORM Adapter, the following configuration options are exactly equivalent.
2323

2424
```javascript
2525
database: {
@@ -31,21 +31,21 @@ database: {
3131

3232
```javascript
3333
adapter: Adapters.Default({
34-
type: 'sqlite',
35-
database: ':memory:',
36-
synchronize: true
34+
type: "sqlite",
35+
database: ":memory:",
36+
synchronize: true,
3737
})
3838
```
3939

4040
```javascript
4141
adapter: Adapters.TypeORM.Adapter({
42-
type: 'sqlite',
43-
database: ':memory:',
44-
synchronize: true
42+
type: "sqlite",
43+
database: ":memory:",
44+
synchronize: true,
4545
})
4646
```
4747

48-
The tutorial [Custom models with TypeORM](/tutorials/typeorm-custom-models) explains how to extend the built in models and schemas used by the TypeORM adapter. You can use these models in your own code.
48+
The tutorial [Custom models with TypeORM](/tutorials/typeorm-custom-models) explains how to extend the built in models and schemas used by the TypeORM Adapter. You can use these models in your own code.
4949

5050
:::tip
5151
The `synchronize` option in TypeORM will generate SQL that exactly matches the documented schemas for MySQL and Postgres.
@@ -55,31 +55,31 @@ However, it should not be enabled against production databases as it may cause d
5555

5656
## Prisma Adapter
5757

58-
You can also use NextAuth.js with the experimental adapter for [Prisma 2](https://www.prisma.io/docs/).
58+
You can also use NextAuth.js with the experimental Adapter for [Prisma 2](https://www.prisma.io/docs/).
5959

60-
To use this adapter, you need to install Prisma Client and Prisma CLI:
60+
To use this Adapter, you need to install Prisma Client and Prisma CLI:
6161

6262
```
6363
npm install @prisma/client
6464
npm install prisma --save-dev
6565
```
6666

67-
Configure your NextAuth.js to use the Prisma adapter:
67+
Configure your NextAuth.js to use the Prisma Adapter:
6868

6969
```javascript title="pages/api/auth/[...nextauth].js"
70-
import NextAuth from 'next-auth'
71-
import Providers from 'next-auth/providers'
72-
import Adapters from 'next-auth/adapters'
73-
import { PrismaClient } from '@prisma/client'
70+
import NextAuth from "next-auth"
71+
import Providers from "next-auth/providers"
72+
import Adapters from "next-auth/adapters"
73+
import { PrismaClient } from "@prisma/client"
7474

7575
const prisma = new PrismaClient()
7676

7777
export default NextAuth({
7878
providers: [
7979
Providers.Google({
8080
clientId: process.env.GOOGLE_CLIENT_ID,
81-
clientSecret: process.env.GOOGLE_CLIENT_SECRET
82-
})
81+
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
82+
}),
8383
],
8484
adapter: Adapters.Prisma.Adapter({ prisma }),
8585
})
@@ -175,6 +175,7 @@ datasource db {
175175
url = env("DATABASE_URL")
176176
}
177177
```
178+
178179
:::
179180

180181
### Generate Client
@@ -232,9 +233,30 @@ if (process.env.NODE_ENV === "production") {
232233
prisma = global.prisma
233234
}
234235
```
235-
:::
236236

237+
:::
237238

238239
## Custom Adapter
239240

240-
See the tutorial for [creating a database adapter](/tutorials/creating-a-database-adapter) for more information on how to create a custom adapter. Have a look at the [adapters repository](https://github.com/nextauthjs/adapters) to see community maintained custom adapters or add your own.
241+
See the tutorial for [creating a database Adapter](/tutorials/creating-a-database-adapter) for more information on how to create a custom Adapter. Have a look at the [Adapter repository](https://github.com/nextauthjs/adapters) to see community maintained custom Adapter or add your own.
242+
243+
### Editor integration
244+
245+
When writing your own custom Adapter in plain JavaScript, note that you can use **JSDoc** to get helpful editor hints and auto-completion like so:
246+
247+
```js
248+
/** @type { import("next-auth/adapters").Adapter } */
249+
const MyAdapter = () => {
250+
return {
251+
async getAdapter() {
252+
return {
253+
// your adapter methods here
254+
}
255+
},
256+
}
257+
}
258+
```
259+
260+
:::note
261+
This will work in code editors with a strong TypeScript integration like VSCode or WebStorm. It might not work if you're using more lightweight editors like VIM or Atom.
262+
:::

0 commit comments

Comments
 (0)