Email not being set after magic link authentication

Hi!

I’m trying to save the user’s email after login with magic link. I’ve tried multiple ways, but nothing seems to work. What I have tried:

1.with user after login

 const loginWithMagicLink = async () =>{
          
                await authenticate({
                    provider:"magicLink",
                    email:email,
                    apiKey:"my_pk",
                    network: customeNodeOptions
                }).then(async (user) => {
                 user.set("email", email)
                 await user.save()
                  
                   })
                   .catch(function (error) {
                     console.log(error);
                   });
                  
                  
            }

it will return null user

2. with Moralis.User.current()

const loginWithMagicLink = async () =>{
          
                await authenticate({
                    provider:"magicLink",
                    email:email,
                    apiKey:"my_pk",
                    network: customeNodeOptions
                }).then(async () => {
                 const user = Moralis.User.current()
                 user.set("email", email)
                 await user.save()
                  
                   })
                   .catch(function (error) {
                     console.log(error);
                   });
                  
                  
            }

throws error ā€œclient it’s not allowed to maually update email verificationā€

3. cloud function

const loginWithMagicLink = async () =>{
          
                await authenticate({
                    provider:"magicLink",
                    email:email,
                    apiKey:"my_pk",
                    network: customeNodeOptions
                }).then(async (user) => {
const params = {objectId:userId, email:email}
                 await Moralis.Cloud.run("setUserEmail", params)
                   })
                   .catch(function (error) {
                     console.log(error);
                   });
                  
                  
            }

throws error ā€œresult.set()ā€ is not a function or ā€œcannot read properties of undefinedā€

this is the cloud function:

Moralis.Cloud.define("setUserEmail", async(request) =>{
    const query = new Moralis.Query("User")
    await query.equalTo("objectId", request.params.userId)
    const result = await query.first()
    result.set("email", request.params.email)
    const logger = Moralis.Cloud.getLogger()
    logger.info(result)
    return result.save({useMasterKey:true})
    
})

Am I missing something? Any help would be appreciated!

I’ve managed to make it work by simply checking if the user is null or not.

 const loginWithMagicLink = async () =>{
          
                await authenticate({
                    provider:"magicLink",
                    email:email,
                    apiKey:"pk",
                    network: customeNodeOptions
                }).then(async (user) => {
                      
                          user?.set("email", email)
                          await user?.save()
                      
                       
                    
                  
                   })
                   .catch(function (error) {
                     console.log(error);
                   });
                  
                  
            }
1 Like