import getAuthorizationUrl from "../lib/oauth/authorization-url" ; import emailSignin from "../lib/email/signin" ; import { IncomingRequest , OutgoingResponse } from ".." ; import { InternalOptions } from "../../lib/types" ; import { Account , User } from "../.." ; /** Handle requests to /api/auth/signin */ export default async function signin ( params ) { const { options , query , body } = params ; const { url , adapter , callbacks , logger , provider } = options ; if ( ! provider . type ) { return { status : 500 , text : `Error: Type not specified for ${ provider . name }` , } ; } if ( provider . type === "oauth" ) { try { const response = await getAuthorizationUrl ( { options , query } ) ; return response ; } catch ( error ) { logger . error ( "SIGNIN_OAUTH_ERROR" , { error : error as Error , provider } ) ; return { redirect : `${ url }/error?error=OAuthSignin` } ; } } else if ( provider . type === "email" ) { const email = body ?. email ?. toLowerCase ( ) ?? null ; const { getUserByEmail } = adapter ! ; const user = ( email ? await getUserByEmail ( email ) : null ) ?? { email , id : email , } ; const account = { providerAccountId : email , userId : email , type : "email" , provider : provider . id , } ; try { const signInCallbackResponse = await callbacks . signIn ( { user , account , email : { verificationRequest : true } , } ) ; if ( ! signInCallbackResponse ) { return { redirect : `${ url }/error?error=AccessDenied` } ; } else if ( typeof signInCallbackResponse === "string" ) { return { redirect : signInCallbackResponse } ; } } catch ( error ) { return { redirect : `${ url }/error?${ new URLSearchParams ( { error : error as string , } ) }}` , } ; } try { await emailSignin ( email , options ) ; } catch ( error ) { logger . error ( "SIGNIN_EMAIL_ERROR" , error as Error ) ; return { redirect : `${ url }/error?error=EmailSignin` } ; } const params = new URLSearchParams ( { provider : provider . id , type : provider . type , } ) ; return { redirect : `${ url }/verify-request?${ params }` } ; } return { redirect : `${ url }/signin` } ; }