import { ModuleContext } from '@graphql-modules/core' ; import { CreateUserServicePassword } from '@accounts/types' ; import { AccountsPassword , CreateUserErrors , SendResetPasswordEmailErrors , SendVerificationEmailErrors , } from '@accounts/password' ; import { AccountsServer , AccountsJsError } from '@accounts/server' ; import { AccountsModuleContext } from '../../accounts' ; import { MutationResolvers } from '../../../models' ; export const Mutation = { addEmail : async ( _ , { newEmail } , { user , injector } ) => { if ( ! ( user && user . id ) ) { throw new Error ( 'Unauthorized' ) ; } const userId = user . id ; await injector . get ( AccountsPassword ) . addEmail ( userId , newEmail ) ; return null ; } , changePassword : async ( _ , { oldPassword , newPassword } , { user , injector } ) => { if ( ! ( user && user . id ) ) { throw new Error ( 'Unauthorized' ) ; } const userId = user . id ; await injector . get ( AccountsPassword ) . changePassword ( userId , oldPassword , newPassword ) ; return null ; } , createUser : async ( _ , { user } , ctx ) => { const { injector , infos } = ctx ; const accountsServer = injector . get ( AccountsServer ) ; const accountsPassword = injector . get ( AccountsPassword ) ; let userId ; try { userId = await accountsPassword . createUser ( user as CreateUserServicePassword ) ; } catch ( error ) { if ( accountsServer . options . ambiguousErrorMessages && error instanceof AccountsJsError && ( error . code === CreateUserErrors . EmailAlreadyExists || error . code === CreateUserErrors . UsernameAlreadyExists ) ) { return { } ; } throw error ; } if ( ! accountsServer . options . enableAutologin ) { return { userId : accountsServer . options . ambiguousErrorMessages ? null : userId , } ; } const createdUser = await accountsServer . findUserById ( userId ) ; const loginResult = await accountsServer . loginWithUser ( createdUser ! , infos ) ; return { userId , loginResult , } ; } , twoFactorSet : async ( _ , { code , secret } , { user , injector } ) => { if ( ! ( user && user . id ) ) { throw new Error ( 'Unauthorized' ) ; } const userId = user . id ; await injector . get ( AccountsPassword ) . twoFactor . set ( userId , secret as any , code ) ; return null ; } , twoFactorUnset : async ( _ , { code } , { user , injector } ) => { if ( ! ( user && user . id ) ) { throw new Error ( 'Unauthorized' ) ; } const userId = user . id ; await injector . get ( AccountsPassword ) . twoFactor . unset ( userId , code ) ; return null ; } , resetPassword : async ( _ , { token , newPassword } , { injector , infos } ) => { return injector . get ( AccountsPassword ) . resetPassword ( token , newPassword , infos ) ; } , sendResetPasswordEmail : async ( _ , { email } , { injector } ) => { const accountsServer = injector . get ( AccountsServer ) ; const accountsPassword = injector . get ( AccountsPassword ) ; try { await accountsPassword . sendResetPasswordEmail ( email ) ; } catch ( error ) { if ( accountsServer . options . ambiguousErrorMessages && error instanceof AccountsJsError && error . code === SendResetPasswordEmailErrors . UserNotFound ) { return null ; } throw error ; } return null ; } , verifyEmail : async ( _ , { token } , { injector } ) => { await injector . get ( AccountsPassword ) . verifyEmail ( token ) ; return null ; } , sendVerificationEmail : async ( _ , { email } , { injector } ) => { const accountsServer = injector . get ( AccountsServer ) ; const accountsPassword = injector . get ( AccountsPassword ) ; try { await accountsPassword . sendVerificationEmail ( email ) ; } catch ( error ) { if ( accountsServer . options . ambiguousErrorMessages && error instanceof AccountsJsError && error . code === SendVerificationEmailErrors . UserNotFound ) { return null ; } throw error ; } return null ; } , } ;