Back

Appwrite serive :: getCurrentUser :: error AppwriteException: User (role: guests) missing scope

  • 0
  • Web
Harshad002
27 May, 2024, 19:42

hello, I have got this error when using 'account. get();' this method. I have provided the error msg and code can you help me with any more information you need I will provide it.

CODE Auth.js

TypeScript

import conf from "../conf/conf.js";
import { Client, Account, ID } from "appwrite";

export class AuthService {
  client = new Client();
  account;

  constructor() {
    this.client
        .setEndpoint(conf.appwriteUrl)
        .setProject(conf.appwriteProjectId);
    this.account = new Account(this.client);  
   
}

  async createAccount({ email, password, name }) {
    // eslint-disable-next-line no-useless-catch
    try {
      const userAccount = await this.account.create(
        ID.unique(),
        email,
        password,
        name
      );
      if (userAccount) {
        // call another mwthod
        return this.login({ email, password });
      } else {
        // throw new Error("Failed to create user account");
        return userAccount;
      }
    } catch (error) {
      throw error;
    }
  }

  async login({ email, password }) {
    // eslint-disable-next-line no-useless-catch
    try {
      return await this.account.createSession(email, password);
    } catch (error) {
      throw error;
    }
  }

  async getCurrentUser() {
    // eslint-disable-next-line no-useless-catch
    try {
      return await this.account.get();
    } catch (error) {
      console.log("Appwrite serive :: getCurrentUser :: error", error);
      return null;
    }

    
  }

  async logout(){

   // eslint-disable-next-line no-useless-catch
   try {
    await this.account.deleteSessions();
   } catch (error) {
    throw error;
   }
        
  }
}

const authService = new AuthService();

export default authService;

/```
TL;DR
Issue with logging in user due to missing scope error 'User (role: guests) missing scope' Potential solution: Instead of using `createSession()`, developers should use `createEmailPasswordSession()` when trying to login using email/password. Additionally, ensure you are using the correct SDK version and check the permissions/roles assigned to the user account in Appwrite settings. Error in code snippet `Auth.js` due to missing scope issue. The provided code does not show the login method using createEmailPasswordSession.
Ryan
27 May, 2024, 19:48

Which version of the SDK are you using?

Harshad002
27 May, 2024, 19:54

not use SDK

Harshad002
27 May, 2024, 19:55

"appwrite": "^14.0.1", in node

Ryan
27 May, 2024, 19:55

If you're trying to login using email/password, you'll want to use createEmailPasswordSession() instead of createSession() https://appwrite.io/docs/references/cloud/client-web/account#createEmailPasswordSession

Ryan
27 May, 2024, 19:56

This looks like it's the issue as it's never actually logging the user in

Harshad002
27 May, 2024, 19:59

import { useEffect, useState } from 'react'; import './App.css'; import { useDispatch } from 'react-redux'; import authService from './appwrit/auth'; import { login, logout } from './store/authSlice'; import { Footer, Header } from './components'; import { Outlet } from 'react-router-dom';

function App() { const [loading, setLoading] = useState(true); const dispatch = useDispatch();

useEffect(() => { authService.getCurrentUser() .then((userData) => { if (userData) { dispatch(login({userData})) } else { dispatch(logout()) } }) .finally(() => setLoading(false)) }, [])

return !loading ? ( <div className='w-full min-h-screen flex flex-wrap content-baseline bg-slate-400'> <div className='w-full block'> <Header /> <main> <Outlet /> </main> <Footer /> </div> </div> ) : ( <div className='w-full min-h-screen flex justify-center items-center bg-slate-400'> <p>Loading...</p> </div> )

}

export default App;

Harshad002
27 May, 2024, 19:59

this is my App.jsx

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