import { compare } from 'bcryptjs' ; import { sign } from 'jsonwebtoken' ; import { AuthPayload , Ctx } from '../../../types' ; import { User } from '../../../generated/prisma-client' ; param { object } _ param { object } args param { object } context returns { object } export async function login ( _ , { email : nonParsedEmail , password : nonHashedPassword } , context ) { const email = nonParsedEmail ?. toLowerCase ( ) . trim ( ) ; const user = await context . prisma . user ( { email } ) ; if ( ! user ) { throw new Error ( `No such user found for email: ${ email }` ) ; } const isPasswordValid = await compare ( nonHashedPassword , user . password ) ; if ( ! isPasswordValid ) { throw new Error ( 'Invalid password' ) ; } return { token : sign ( { userId : user . id } , process . env . APP_SECRET ) , user , } ; }