Appwrite serive :: getCurrentUser :: error AppwriteException: User (role: guests) missing scope
- 0
- Web
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
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;
/```
Which version of the SDK are you using?
not use SDK
"appwrite": "^14.0.1", in node
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
This looks like it's the issue as it's never actually logging the user in
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;
this is my App.jsx
Recommended threads
- How to Display File in Web?
I'm trying to use Appwrite's Storage to store images and display them in my app, however when I use the `getFileView`, `getFileDownload` or `getFilePreview` met...
- Project Paused Despite Daily Active Usag...
I noticed that my project was automatically **paused**, even though it is actively being used. The project is an **attendance application** that is used daily b...
- Sudden CORS Errors - Domain hasn't Chang...
I have an Appwrite project with two web apps configured, the first one has the hostname `*` and the second one I just added to test if it could fix the issue wi...