Back

Server SDK SSR -- Using accounts.get() unauthorized with session cookie

  • 0
  • Auth
  • Cloud
ZachHandley
15 May, 2024, 22:48

So using the Server SDK and Astro, I have set the cookie session and I have this function that sets an API key or a session and returns a client, but for whatever reason I'm getting a general_unauthorized_scope for accounts.get() in the server SDK regardless of if I initiate it with a token or not

TypeScript
  async getUser(): Promise<APIResponse<AuthUser>> {
    try {
      const user = await this.accounts.get();
      console.log(user);
      return {
        status: 200,
        data: user,
        message: "User retrieved successfully",
      };
    } catch (error) {
      // console.log("Error retrieving user:", error);
      return {
        status: 500,
        message: "Error retrieving user",
      };
    }
  }
TypeScript
export const getUserAppwriteClient = (request: AstroCookies): Client => {
  const sessionToken = request.has("session") ? request.get("session") : null;
  const client = new Client()
    .setEndpoint(import.meta.env.PUBLIC_APPWRITE_ENDPOINT)
    .setProject(import.meta.env.PUBLIC_APPWRITE_PROJECT_ID);
  if (sessionToken) {
    client.setSession(sessionToken.value);
  } else {
    client.setKey(import.meta.env.APPWRITE_SESSION_API_KEY);
  }
  return client;
};

but yeah I can't get it to work, just unauthorized over and over. Is this wrong for some reason?

TL;DR
Developers are encountering an `general_unauthorized_scope` error when trying to use `accounts.get()` with the Server SDK and Astro while using a session cookie. The provided code seems correct, but the issue persists.
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