-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
146 lines (146 loc) · 4.98 KB
/
index.js
File metadata and controls
146 lines (146 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* _If you are looking to migrate from v4, visit the [Upgrade Guide (v5)](https://authjs.dev/getting-started/migrating-to-v5)._
*
* ## Installation
*
* ```bash npm2yarn
* npm install next-auth@beta
* ```
*
* ## Environment variable inference
*
* `NEXTAUTH_URL` and `NEXTAUTH_SECRET` have been inferred since v4.
*
* Since NextAuth.js v5 can also automatically infer environment variables that are prefixed with `AUTH_`.
*
* For example `AUTH_GITHUB_ID` and `AUTH_GITHUB_SECRET` will be used as the `clientId` and `clientSecret` options for the GitHub provider.
*
* :::tip
* The environment variable name inferring has the following format for OAuth providers: `AUTH_{PROVIDER}_{ID|SECRET}`.
*
* `PROVIDER` is the uppercase snake case version of the provider's id, followed by either `ID` or `SECRET` respectively.
* :::
*
* `AUTH_SECRET` and `AUTH_URL` are also aliased for `NEXTAUTH_SECRET` and `NEXTAUTH_URL` for consistency.
*
* To add social login to your app, the configuration becomes:
*
* ```ts title="auth.ts"
* import NextAuth from "next-auth"
* import GitHub from "next-auth/providers/github"
* export const { handlers, auth } = NextAuth({ providers: [ GitHub ] })
* ```
*
* And the `.env.local` file:
*
* ```sh title=".env.local"
* AUTH_GITHUB_ID=...
* AUTH_GITHUB_SECRET=...
* AUTH_SECRET=...
* ```
*
* :::tip
* In production, `AUTH_SECRET` is a required environment variable - if not set, NextAuth.js will throw an error. See [MissingSecretError](https://authjs.dev/reference/core/errors#missingsecret) for more details.
* :::
*
* If you need to override the default values for a provider, you can still call it as a function `GitHub({...})` as before.
*
* ## Lazy initialization
* You can also initialize NextAuth.js lazily (previously known as advanced intialization), which allows you to access the request context in the configuration in some cases, like Route Handlers, Middleware, API Routes or `getServerSideProps`.
* The above example becomes:
*
* ```ts title="auth.ts"
* import NextAuth from "next-auth"
* import GitHub from "next-auth/providers/github"
* export const { handlers, auth } = NextAuth(req => {
* if (req) {
* console.log(req) // do something with the request
* }
* return { providers: [ GitHub ] }
* })
* ```
*
* :::tip
* This is useful if you want to customize the configuration based on the request, for example, to add a different provider in staging/dev environments.
* :::
*
* @module next-auth
*/
import { Auth } from "@auth/core";
import { reqWithBasePathURL, reqWithEnvURL, setEnvDefaults } from "./lib/env.js";
import { initAuth } from "./lib/index.js";
import { signIn, signOut, update } from "./lib/actions.js";
export { AuthError, CredentialsSignin } from "@auth/core/errors";
/**
* Initialize NextAuth.js.
*
* @example
* ```ts title="auth.ts"
* import NextAuth from "next-auth"
* import GitHub from "@auth/core/providers/github"
*
* export const { handlers, auth } = NextAuth({ providers: [GitHub] })
* ```
*
* Lazy initialization:
*
* @example
* ```ts title="auth.ts"
* import NextAuth from "next-auth"
* import GitHub from "@auth/core/providers/github"
*
* export const { handlers, auth } = NextAuth((req) => {
* console.log(req) // do something with the request
* return {
* providers: [GitHub],
* },
* })
* ```
*/
export default function NextAuth(config) {
let _config;
if (typeof config === "function") {
const httpHandler = (req) => {
_config = config(req);
setEnvDefaults(_config);
return Auth(reqWithEnvURL(req), _config);
};
return {
handlers: { GET: httpHandler, POST: httpHandler },
// @ts-expect-error
auth: initAuth(config, (c) => setEnvDefaults(c)),
signIn: (provider, options, authorizationParams) => {
const _config = config(undefined);
setEnvDefaults(_config);
return signIn(provider, options, authorizationParams, _config);
},
signOut: (options) => {
const _config = config(undefined);
setEnvDefaults(_config);
return signOut(options, _config);
},
unstable_update: (data) => {
const _config = config(undefined);
setEnvDefaults(_config);
return update(data, _config);
},
};
}
setEnvDefaults(config);
_config = config;
const httpHandler = (req) => Auth(reqWithBasePathURL(reqWithEnvURL(req), _config), _config);
return {
handlers: { GET: httpHandler, POST: httpHandler },
// @ts-expect-error
auth: initAuth(_config),
signIn: (provider, options, authorizationParams) => {
return signIn(provider, options, authorizationParams, _config);
},
signOut: (options) => {
return signOut(options, _config);
},
unstable_update: (data) => {
return update(data, _config);
},
};
}