Back

401 to get /accounts

  • 0
  • Web
Qoyyuum
4 Feb, 2025, 06:19

I did

TL;DR
Developers are facing a 401 error when trying to access /accounts on their web app using Appwrite. They are trying to check if the user is logged in, but keep getting a 'role missing scope' error. The solution is to ensure the user has the required scope (account) in the console. The Quickstart code provided doesn't show how to use an API key.
Qoyyuum
4 Feb, 2025, 06:19

I have both Web and Android but I'm building for Web first

Qoyyuum
4 Feb, 2025, 06:20

Do I need to set up the API Key?

Raman
4 Feb, 2025, 06:21

than add the web on the platform?

Raman
4 Feb, 2025, 06:22

If you have than you should.

Qoyyuum
4 Feb, 2025, 06:22

I did. Its already there.

Qoyyuum
4 Feb, 2025, 06:22

The quickstart code doesn't show how to use API key though.

Raman
4 Feb, 2025, 06:23

can you share the logs

Qoyyuum
4 Feb, 2025, 06:24
TypeScript
AppwriteException: User (role: guests) missing scope (account)
    at Client.<anonymous> (file:///C:/Users/User/sides/ladanglink/node_modules/appwrite/dist/esm/sdk.js:655:23)
    at Generator.next (<anonymous>)
    at fulfilled (file:///C:/Users/User/sides/ladanglink/node_modules/appwrite/dist/esm/sdk.js:19:58)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 401,
  type: 'general_unauthorized_scope',
  response: {
    message: 'User (role: guests) missing scope (account)',
    code: 401,
    type: 'general_unauthorized_scope',
    version: '1.6.1'
  }
}
Qoyyuum
4 Feb, 2025, 06:29

I got it to work

Qoyyuum
4 Feb, 2025, 06:29
TypeScript
import { Client, Account, ID } from "appwrite";

const client = new Client()
    .setProject(import.meta.env.VITE_APPWRITE_PROJECT_ID); // Your project ID

const account = new Account(client);

const promise = account.create(ID.unique(), 'email@example.com', 'password');

promise.then(function (response) {
    console.log(response); // Success
}, function (error) {
    console.log(error); // Failure
});
Qoyyuum
4 Feb, 2025, 06:29

It seems I don't need to specify the endpoint... strange

Qoyyuum
4 Feb, 2025, 06:31

oh wait... its still not working when trying to get the account

Raman
4 Feb, 2025, 06:40

Do one thing check the session for the account

Qoyyuum
4 Feb, 2025, 06:42

how do I check?

Raman
4 Feb, 2025, 06:43

Yes

Qoyyuum
4 Feb, 2025, 06:45

I tried. Still same error message.

Qoyyuum
4 Feb, 2025, 06:46

Code that I tried

TypeScript
import { Client, Account } from "appwrite";

const client = new Client()
    .setProject('Your Project ID'); // Your project ID

const account = new Account(client);

const user_account = await account.getSession('current');

console.log(user_account); // Success
Qoyyuum
4 Feb, 2025, 06:46

Of course I swap out the Project ID with the actual Project ID

Qoyyuum
4 Feb, 2025, 06:48

Maybe I'm doing this wrong

Qoyyuum
4 Feb, 2025, 06:48

I want to check if the user is logged in or not when the app is opened on their device

Qoyyuum
4 Feb, 2025, 06:48

If the user is logged in, then they can see the dashboard

Qoyyuum
4 Feb, 2025, 06:49

If not, direct them to the login/sign up page

Qoyyuum
4 Feb, 2025, 06:49

I figure it should be checking if that account or session exists but not sure how to go about it

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