import faunadb from "faunadb" ; import { AccountStatus } from ".." ; import { query as q } from "faunadb" ; import { Driver } from "../api" ; export function createFaunaEmailDriver ( faunaClient ) { return { attemptEmailPasswordLogin : async ( email , password ) => { try { const loginRes = await faunaClient . query ( q . Let ( { loginData : q . Login ( q . Match ( q . Index ( "users_by_email" ) , email ) , { password , } ) , } , { userRef : q . Select ( [ "instance" ] , q . Var ( "loginData" ) ) , accountStatus : q . Select ( [ "data" , "accountStatus" ] , q . Get ( q . Select ( "instance" , q . Var ( "loginData" ) ) ) ) , } ) ) ; return { success : true , data : { userId : loginRes . userRef . id , accountStatus : loginRes . accountStatus , } , } ; } catch ( error ) { if ( error . message === "authentication failed" ) return { success : false , reason : "authentication failed" } ; throw error ; } } , createEmailUser : async ( email , password ) => { try { const user = await faunaClient . query ( q . Create ( q . Collection ( "User" ) , { credentials : { password } , data : { email , created : q . Now ( ) , accountStatus : AccountStatus . unconfirmed , } , } ) ) ; return { success : true , data : { userId : user . ref . id } } ; } catch ( error ) { if ( error . message === "instance not unique" ) return { success : false , reason : "instance not unique" } ; throw error ; } } , getUserIdByEmail : async ( email ) => { const response = await faunaClient . query < false | string > ( q . Let ( { match : q . Match ( q . Index ( "users_by_email" ) , email . toLowerCase ( ) ) , } , q . If ( q . Exists ( q . Var ( "match" ) ) , q . Select ( [ "ref" , "id" ] , q . Get ( q . Var ( "match" ) ) ) , false ) ) ) ; return response || null ; } , updateEmailUserPassword : async ( userId , password ) => { await faunaClient . query ( q . Update ( q . Ref ( q . Collection ( "User" ) , userId ) , { credentials : { password } , } ) ) ; } , changeEmailUserPassword : async ( userId , currentPassword , newPassword ) => { await faunaClient . query ( q . Do ( q . Login ( q . Ref ( q . Collection ( "User" ) , userId ) , { password : currentPassword , } ) , q . Update ( q . Ref ( q . Collection ( "User" ) , userId ) , { credentials : { password : newPassword } , } ) ) ) ; } , confirmAccount : async ( userId ) => { return faunaClient . query < boolean > ( q . Let ( { userRef : q . Ref ( q . Collection ( "User" ) , userId ) } , q . If ( q . Exists ( q . Var ( "userRef" ) ) , q . Let ( { accountStatus : q . Select ( [ "data" , "accountStatus" ] , q . Get ( q . Var ( "userRef" ) ) ) , } , q . If ( q . Or ( q . Equals ( q . Var ( "accountStatus" ) , AccountStatus . confirmed ) , q . Equals ( q . Var ( "accountStatus" ) , AccountStatus . unconfirmed ) ) , q . Do ( q . Update ( q . Var ( "userRef" ) , { data : { accountStatus : AccountStatus . confirmed } , } ) , true ) , false ) ) , false ) ) ) ; } , } ; }