Back

account.get() 401 (Unauthorized) in React Native for Web

  • 1
  • React Native
  • Web
  • Auth
Jacob
4 Sep, 2024, 05:47

Hi all, I'm following the repo https://github.com/adrianhajdin/aora to make the app work in browser with react-native-web. The sign-in feature works well on mobile devices, but not in web browsers.

In https://github.com/adrianhajdin/aora/blob/main/app/(auth)/sign-in.jsx, after clinking Sign In button with correct credentials, I got an error ```GET https://cloud.appwrite.io/v1/account 401 (Unauthorized)

AppwriteException: User (role: guests) missing scope (account) at Client.<anonymous> (http://localhost:8081/node_modules/expo-router/entry.bundle?platform=web&amp;dev=true&amp;hot=false&amp;lazy=true&amp;transform.routerRoot=app&amp;resolver.environment=client&amp;transform.environment=client:93245:19) at Generator.next (<anonymous>) at fulfilled (http://localhost:8081/node_modules/expo-router/entry.bundle?platform=web&amp;dev=true&amp;hot=false&amp;lazy=true&amp;transform.routerRoot=app&amp;resolver.environment=client&amp;transform.environment=client:92875:26)``` I've tried to add some debug code around the signIn call in sign-in.jsx:

TypeScript
      const session = await signIn(form.email, form.password);
      console.log(`User signed in: ${form.email}`);
      console.log(`Session: ${JSON.stringify(session)}`);
      const result = await getCurrentUser();```
and also in the `getAccount` method in https://github.com/adrianhajdin/aora/blob/main/lib/appwrite.js :```    console.log("Getting current account");
    const currentAccount = await account.get();
    console.log(`Current account: ${JSON.stringify(currentAccount)}`);```
I'll paste the whole console output I got from the browser below, since Discord says "Message is too long".

Any help/advice would be greatly appreciated.
TL;DR
Developers are getting a 401 (Unauthorized) error when utilizing the `account.get()` method in React Native for Web with the `react-native-appwrite` library. The issue is related to the platform configuration, specifically regarding the web platform and localhost settings. Adding "localhost" as a web app platform may resolve the error.
Jacob
4 Sep, 2024, 05:52
TypeScript
entry.bundle?platfor…onment=client:30573 props.pointerEvents is deprecated. Use style.pointerEvents
entry.bundle?platfor…onment=client:93249 Appwrite is using localStorage for session management. Increase your security by adding a custom domain as your API endpoint.
entry.bundle?platfor…onment=client:17566 User signed in: ******@*****.com
entry.bundle?platfor…onment=client:17567 Session: {"$id":"66d7f502c71f38ad8662","$createdAt":"2024-09-04T05:49:54.820+00:00","$updatedAt":"2024-09-04T05:49:54.820+00:00","userId":"66d6631300029d264ea3","expire":"2025-09-04T05:49:54.815+00:00","provider":"email","providerUid":"*******@*****.com","providerAccessToken":"","providerAccessTokenExpiry":"","providerRefreshToken":"","ip":"192.168.65.1","osCode":"MAC","osName":"Mac","osVersion":"10.15","clientType":"browser","clientCode":"CH","clientName":"Chrome","clientVersion":"128.0","clientEngine":"Blink","clientEngineVersion":"128.0.0.0","deviceName":"desktop","deviceBrand":"Apple","deviceModel":"","countryCode":"--","countryName":"Unknown","current":true,"factors":["password"],"secret":"","mfaUpdatedAt":""}
entry.bundle?platfor…onment=client:21499 Getting current account
entry.bundle?platfor…onment=client:93236 
 GET https://cloud.appwrite.io/v1/account 401 (Unauthorized)
entry.bundle?platfor…onment=client:21511 AppwriteException: User (role: guests) missing scope (account)
    at Client.<anonymous> (http://localhost:8081/node_modules/expo-router/entry.bundle?platform=web&de…Root=app&resolver.environment=client&transform.environment=client:93245:19)
    at Generator.next (<anonymous>)
    at fulfilled (http://localhost:8081/node_modules/expo-router/entry.bundle?platform=web&de…Root=app&resolver.environment=client&transform.environment=client:92875:26)```
trashcoder
4 Sep, 2024, 06:59

do you have add platform "localhost" as web app in your appwrite instance?

trashcoder
4 Sep, 2024, 07:01

But i don't know if it's a good behaviour for a public appwrite instance to add "localhost"

Jacob
4 Sep, 2024, 14:15

I thought "as long as it works, I don't care whether it's a good behaviour"😂 . I've even tried to put a wildcard * for both iOS and web platform, but the error persists.

Jacob
4 Sep, 2024, 14:20

Actually I don't have the web platform at first; I created it only because I was hoping it could fix the error. As I'm using Appwrite React Native SDK, I don't think it really matters whether I have a web app platform in my appwrite instance.

ideclon
5 Sep, 2024, 09:58

localhost is automatically allowed - there’s no need to set a Platform for it

ideclon
5 Sep, 2024, 10:01

How are you accessing the account object?

Jacob
5 Sep, 2024, 12:59

I'm accessing the account object by creating an instance of the Account class from the react-native-appwrite library. This instance is initialized with the client object, which is configured with the Appwrite endpoint, project ID, and platform.

Here is my step-by-step process:

  1. Importing the Account class:
TypeScript
import { Account } from "react-native-appwrite";
  1. Creating and configuring the client object:
TypeScript
const client = new Client();

client
  .setEndpoint(config.endpoint) 
  .setProject(config.projectId) 
  .setPlatform(config.platform); 
  1. Creating an instance of the Account class:
TypeScript
const account = new Account(client);
  1. Using the account object to call methods:
  • Creating a user:
TypeScript
const newAccount = await account.create(ID.unique(), email, password, username);
  • Signing in a user:
TypeScript
const session = await account.createEmailPasswordSession(email, password);
  • Getting the current user:
TypeScript
const currentAccount = await account.get();

Currently the account object works fine for users signup and signin on both mobile and web platforms, and it could get the correct session after verifying credentials. However, the account.get() would get an error after that.

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