( provider?: LiteralUnion<BuiltInProviderType>, options?: SignInOptions, authorizationParams?: SignInAuthorisationParams )
| 173 | * [Documentation](https://next-auth.js.org/getting-started/client#signin) |
| 174 | */ |
| 175 | export async function signIn< |
| 176 | P extends RedirectableProviderType | undefined = undefined |
| 177 | >( |
| 178 | provider?: LiteralUnion<BuiltInProviderType>, |
| 179 | options?: SignInOptions, |
| 180 | authorizationParams?: SignInAuthorisationParams |
| 181 | ): Promise< |
| 182 | P extends RedirectableProviderType ? SignInResponse | undefined : undefined |
| 183 | > { |
| 184 | const { callbackUrl = window.location.href, redirect = true } = options ?? {} |
| 185 | |
| 186 | const baseUrl = apiBaseUrl(__NEXTAUTH) |
| 187 | const providers = await getProviders() |
| 188 | |
| 189 | if (!providers) { |
| 190 | window.location.href = `${baseUrl}/error` |
| 191 | return |
| 192 | } |
| 193 | |
| 194 | if (!provider || !(provider in providers)) { |
| 195 | window.location.href = `${baseUrl}/signin?${new URLSearchParams({ |
| 196 | callbackUrl, |
| 197 | })}` |
| 198 | return |
| 199 | } |
| 200 | |
| 201 | const isCredentials = providers[provider].type === "credentials" |
| 202 | const isEmail = providers[provider].type === "email" |
| 203 | const isSupportingReturn = isCredentials || isEmail |
| 204 | |
| 205 | const signInUrl = `${baseUrl}/${ |
| 206 | isCredentials ? "callback" : "signin" |
| 207 | }/${provider}` |
| 208 | |
| 209 | const _signInUrl = `${signInUrl}?${new URLSearchParams(authorizationParams)}` |
| 210 | |
| 211 | const res = await fetch(_signInUrl, { |
| 212 | method: "post", |
| 213 | headers: { |
| 214 | "Content-Type": "application/x-www-form-urlencoded", |
| 215 | }, |
| 216 | // @ts-expect-error |
| 217 | body: new URLSearchParams({ |
| 218 | ...options, |
| 219 | csrfToken: await getCsrfToken(), |
| 220 | callbackUrl, |
| 221 | json: true, |
| 222 | }), |
| 223 | }) |
| 224 | |
| 225 | const data = await res.json() |
| 226 | |
| 227 | if (redirect || !isSupportingReturn) { |
| 228 | const url = data.url ?? callbackUrl |
| 229 | window.location.href = url |
| 230 | // If url contains a hash, the browser does not reload the page. We reload manually |
| 231 | if (url.includes("#")) window.location.reload() |
| 232 | return |
no test coverage detected
searching dependent graphs…