Back

Function triggered by MagicURL

  • 0
  • Web
  • Cloud
Chris Nowicki
15 Jul, 2024, 21:31

We have a function that runs when a user is created. This function creates a profile for them in a collection in the DB.

I just added in MagicURL options. If a user does not exist with that email a new one is created via the createMagicURLToken.

here is the function:

TypeScript
export const createMagicURLToken = async (
  email: IUser['email'],
): Promise<void> => {
  try {
    await account.createMagicURLToken(
      ID.unique(),
      email,
      'http://localhost:3000/auth/magicURL',
    );
  } catch (error) {
    console.error(error);
    throw new Error('Error creating magic URL token');
  }
};

and then when the user clicks the link in their email it uses this function:

TypeScript
export const createSessionFromMagicURLToken = async ({
  userId,
  secret,
}: {
  userId: IUser['id'];
  secret: string;
}): Promise<void> => {
  try {
    await account.createSession(userId, secret);
  } catch (error) {
    console.error(error);
    throw new Error('Error creating session from magic URL token');
  }
};

so the idea is if the user doesn't exist, and is created, then the Appwrite Function runs. This works on registering with email/password. But does not work with magicURL.

Thougts?

TL;DR
A function triggers profile creation in DB on user creation. MagicURL option added to create user if not exist. Issue: the function doesn't run with MagicURL, only with email/password registration. Fix required to properly trigger the function with MagicURL.
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more