Back

Can't create User and send Verification

  • 0
  • Self Hosted
  • Web
Jonny
17 Jan, 2025, 21:37

Hey πŸ˜„ I'm not able to create a User und send him a verification mail. Always receive /v1/account/verification 401 (Unauthorized)

there is no old session, cause i get /v1/account/sessions/current 401 (Unauthorized) too

just using the signIn as documented with account.get() results in AppwriteException: User (role: guests) missing scope (account)

TL;DR
Developers are having issues creating a user and sending verification emails due to authorization errors. The error specified "User (role: guests) missing scope (account)". The solution involves creating a new session after deleting the current one. Ensure to include the `Account` object after creating an account.
Kenny
17 Jan, 2025, 21:47

Can you provide the code you're using?

Jonny
17 Jan, 2025, 21:47

sure 1 sec

Jonny
17 Jan, 2025, 21:51

appwrite.ts

TypeScript
import {Client, Databases, Account, Avatars} from 'appwrite';
import {environment} from '@environment/environment';

export const client = new Client();

client
  .setEndpoint('<endpoint>') // blanked but set in my Code
  .setProject(environment.projectId)
  .setLocale('de-DE');

export const account: Account = new Account(client);
export const databases: Databases = new Databases(client);
export const avatars: Avatars = new Avatars(client);

export { ID } from 'appwrite';

auth.service.ts

TypeScript
async signIn(email: string, password: string) {
    try {
      await account.deleteSession('current');

      const session = account.createEmailPasswordSession(email, password);

      session.then((response) => {
        console.info('Session created', response);
      }).catch((error) => {
        console.error('Session creation failed', error);
      })

      const user = await account.get();

      if (!user) {
        console.error('User not found');
        this.toast.error('User not found');
        return;
      }

    } catch (err) {
      console.error(err);
      throw new Error;
    }
  }

  async signUp(email: string, password: string, username: string) {
    try {
      const newAccount = await account.create(ID.unique(), email, password, username);

      if (!newAccount) {
        console.error('Account creation failed');
        this.toast.error('Account creation failed');
        return;
      }

      const avatarUrl = avatars.getInitials(username);

      await this.signIn(email, password);
      
      // Tried with verification here
      // await account.createVerification('https://environment.baseUrl/verify');

      const newUser = await databases.createDocument(
        this.databaseId,
        this.collectionId,
        ID.unique(),
        {
          userId: newAccount.$id,
          email,
          username,
          avatar: avatarUrl
        }
      );

      return newUser;
    } catch (err) {
      console.error(err);
      throw new Error;
    }
  }
Jonny
17 Jan, 2025, 21:52

tried the signUp without the signIn, just account.create -> account.createVerification

Jonny
17 Jan, 2025, 21:52

went through some posts and did alot try and error, now I'm a bit lost

Kenny
17 Jan, 2025, 21:57

After creating the account you need to create a new session so you will have to do the createEmailPasswordSession. Are you actually getting after account.deleteSession() ?

Kenny
17 Jan, 2025, 21:57

I can't remember if no current session exists does deleteSession throw and prevent the code from progressing

Jonny
17 Jan, 2025, 21:58

Removed the line and tried signIn

Kenny
17 Jan, 2025, 21:58

And?

Jonny
17 Jan, 2025, 21:58

/v1/account 401 (Unauthorized)

Jonny
17 Jan, 2025, 21:59

AppwriteException: User (role: guests) missing scope (account)

Kenny
17 Jan, 2025, 21:59

Does it tell you which method is throwing that?

Kenny
17 Jan, 2025, 21:59

Do you have a stack trace?

Kenny
17 Jan, 2025, 21:59

Can you try this?

TypeScript
async signIn(email: string, password: string) {
  try {
    const session = await account.createEmailPasswordSession(email, password);
    const user = await account.get();

    if (!user) {
      console.error('User not found');
      this.toast.error('User not found');
      return;
    }

  } catch (err) {
    console.error(err);
    throw new Error;
  }
}
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