|
1 |
| -import type { |
2 |
| - BuiltInProviderType, |
3 |
| - RedirectableProviderType, |
4 |
| -} from "@auth/core/providers" |
| 1 | +import type { ProviderId } from "@auth/core/providers" |
5 | 2 |
|
6 |
| -type LiteralUnion<T extends U, U = string> = T | (U & Record<never, never>) |
7 |
| - |
8 |
| -interface SignInOptions extends Record<string, unknown> { |
| 3 | +interface SignInOptions<Redirect extends boolean = true> |
| 4 | + extends Record<string, unknown> { |
| 5 | + /** @deprecated Use `redirectTo` instead. */ |
| 6 | + callbackUrl?: string |
9 | 7 | /**
|
10 |
| - * Specify to which URL the user will be redirected after signing in. Defaults to the page URL the sign-in is initiated from. |
| 8 | + * Specify where the user should be redirected to after a successful signin. |
11 | 9 | *
|
12 |
| - * [Documentation](https://next-auth.js.org/getting-started/client#specifying-a-callbackurl) |
| 10 | + * By default, it is the page the sign-in was initiated from. |
13 | 11 | */
|
14 |
| - callbackUrl?: string |
15 |
| - /** [Documentation](https://next-auth.js.org/getting-started/client#using-the-redirect-false-option) */ |
16 |
| - redirect?: boolean |
| 12 | + redirectTo?: string |
| 13 | + /** |
| 14 | + * You might want to deal with the signin response on the same page, instead of redirecting to another page. |
| 15 | + * For example, if an error occurs (like wrong credentials given by the user), you might want to show an inline error message on the input field. |
| 16 | + * |
| 17 | + * For this purpose, you can set this to option `redirect: false`. |
| 18 | + */ |
| 19 | + redirect?: Redirect |
| 20 | +} |
| 21 | + |
| 22 | +export interface SignInResponse { |
| 23 | + error: string | undefined |
| 24 | + code: string | undefined |
| 25 | + status: number |
| 26 | + ok: boolean |
| 27 | + url: string | null |
17 | 28 | }
|
18 | 29 |
|
19 | 30 | interface SignOutParams<R extends boolean = true> {
|
@@ -41,59 +52,81 @@ export type SignInAuthorizationParams =
|
41 | 52 | * signIn("provider") // example: signIn("github")
|
42 | 53 | * ```
|
43 | 54 | */
|
44 |
| -export async function signIn< |
45 |
| - P extends RedirectableProviderType | undefined = undefined, |
46 |
| ->( |
47 |
| - providerId?: LiteralUnion< |
48 |
| - P extends RedirectableProviderType |
49 |
| - ? P | BuiltInProviderType |
50 |
| - : BuiltInProviderType |
51 |
| - >, |
52 |
| - options?: SignInOptions, |
| 55 | + |
| 56 | +/** |
| 57 | + * Initiates a signin flow or sends the user to the signin page listing all possible providers. |
| 58 | + * Handles CSRF protection. |
| 59 | + * |
| 60 | + * @note This method can only be used from Client Components ("use client" or Pages Router). |
| 61 | + * For Server Actions, use the `signIn` method imported from the `auth` config. |
| 62 | + */ |
| 63 | +export async function signIn( |
| 64 | + provider?: ProviderId, |
| 65 | + options?: SignInOptions<true>, |
53 | 66 | authorizationParams?: SignInAuthorizationParams
|
54 |
| -) { |
55 |
| - const { callbackUrl = window.location.href, redirect = true } = options ?? {} |
| 67 | +): Promise<void> |
| 68 | +export async function signIn( |
| 69 | + provider?: ProviderId, |
| 70 | + options?: SignInOptions<false>, |
| 71 | + authorizationParams?: SignInAuthorizationParams |
| 72 | +): Promise<SignInResponse> |
| 73 | +export async function signIn<Redirect extends boolean = true>( |
| 74 | + provider?: ProviderId, |
| 75 | + options?: SignInOptions<Redirect>, |
| 76 | + authorizationParams?: SignInAuthorizationParams |
| 77 | +): Promise<SignInResponse | void> { |
| 78 | + const { callbackUrl, ...rest } = options ?? {} |
| 79 | + const { |
| 80 | + redirect = true, |
| 81 | + redirectTo = callbackUrl ?? window.location.href, |
| 82 | + ...signInParams |
| 83 | + } = rest |
56 | 84 |
|
57 |
| - // TODO: Support custom providers |
58 |
| - const isCredentials = providerId === "credentials" |
59 |
| - const isEmail = providerId === "email" |
60 |
| - const isSupportingReturn = isCredentials || isEmail |
| 85 | + const isCredentials = provider === "credentials" |
61 | 86 |
|
62 |
| - // TODO: Handle custom base path |
63 | 87 | const signInUrl = `/api/auth/${
|
64 | 88 | isCredentials ? "callback" : "signin"
|
65 |
| - }/${providerId}` |
66 |
| - |
67 |
| - const _signInUrl = `${signInUrl}?${new URLSearchParams(authorizationParams)}` |
| 89 | + }/${provider}` |
68 | 90 |
|
69 | 91 | // TODO: Handle custom base path
|
70 | 92 | const csrfTokenResponse = await fetch("/api/auth/csrf")
|
71 | 93 | const { csrfToken } = await csrfTokenResponse.json()
|
| 94 | + const res = await fetch( |
| 95 | + `${signInUrl}?${new URLSearchParams(authorizationParams)}`, |
| 96 | + { |
| 97 | + method: "post", |
| 98 | + headers: { |
| 99 | + "Content-Type": "application/x-www-form-urlencoded", |
| 100 | + "X-Auth-Return-Redirect": "1", |
| 101 | + }, |
| 102 | + body: new URLSearchParams({ |
| 103 | + ...signInParams, |
| 104 | + csrfToken, |
| 105 | + callbackUrl: redirectTo, |
| 106 | + }), |
| 107 | + } |
| 108 | + ) |
72 | 109 |
|
73 |
| - const res = await fetch(_signInUrl, { |
74 |
| - method: "post", |
75 |
| - headers: { |
76 |
| - "Content-Type": "application/x-www-form-urlencoded", |
77 |
| - "X-Auth-Return-Redirect": "1", |
78 |
| - }, |
79 |
| - // @ts-ignore |
80 |
| - body: new URLSearchParams({ |
81 |
| - ...options, |
82 |
| - csrfToken, |
83 |
| - callbackUrl, |
84 |
| - }), |
85 |
| - }) |
| 110 | + const data = await res.json() |
86 | 111 |
|
87 |
| - const data = await res.clone().json() |
88 |
| - const error = new URL(data.url).searchParams.get("error") |
89 |
| - if (redirect || !isSupportingReturn || !error) { |
90 |
| - // TODO: Do not redirect for Credentials and Email providers by default in next major |
91 |
| - window.location.href = data.url ?? data.redirect ?? callbackUrl |
| 112 | + if (redirect) { |
| 113 | + const url = data.url ?? redirectTo |
| 114 | + window.location.href = url |
92 | 115 | // If url contains a hash, the browser does not reload the page. We reload manually
|
93 |
| - if (data.url.includes("#")) window.location.reload() |
| 116 | + if (url.includes("#")) window.location.reload() |
94 | 117 | return
|
95 | 118 | }
|
96 |
| - return res |
| 119 | + |
| 120 | + const error = new URL(data.url).searchParams.get("error") ?? undefined |
| 121 | + const code = new URL(data.url).searchParams.get("code") ?? undefined |
| 122 | + |
| 123 | + return { |
| 124 | + error, |
| 125 | + code, |
| 126 | + status: res.status, |
| 127 | + ok: res.ok, |
| 128 | + url: error ? null : data.url, |
| 129 | + } |
97 | 130 | }
|
98 | 131 |
|
99 | 132 | /**
|
|
0 commit comments