Back

[SOLVED] User (role: guests) missing scope (account) but I'm signed in...?

  • 0
  • Databases
  • Web
Drake
30 Oct, 2023, 19:07

Possibly ..I don't know too much about Astro...is there any server side code?

TL;DR
User is experiencing an issue where a role (guests) is missing a scope (account) despite being signed in. They have shared relevant code snippets. The user mentions trying a login function, but it didn't solve the issue. They suspect a middleware may be causing the problem, but they're unsure why it would interfere with the client-side. The user also wonders if they should include a return statement after context.redirect(). They provide additional code snippets for context. Another user suggests that the session may only exist client-side and not server-side. The user shares a middleware snippet that includes redirects based on authentication status. They inquire if the middleware
S3R43o3
30 Oct, 2023, 19:15

well get the same issue alltime if i want to logout i cant acces the "auth" category at my project from the website... logout is not possible... dunno

S3R43o3
30 Oct, 2023, 19:16

internal 500 Error code

S3R43o3
30 Oct, 2023, 19:17

Here some console error that i get:

TypeScript
AppwriteException: Aborted
    Immutable 19
        r
        call
        p
        promise callback*f
        s
        s
        call
        list
        s
        s
        list
        u
        de
        T
        he
        ce
        re
        goto
        xt
    <anonymous> https://cloud.appwrite.io/console/project-653d12ef8b1934bb1bac/auth:159
    promise callback* https://cloud.appwrite.io/console/project-653d12ef8b1934bb1bac/auth:158
app.d2040772.js:1:133492
S3R43o3
30 Oct, 2023, 19:18

and here a network analyse seems your api host got rect.

ZachHandley
30 Oct, 2023, 19:32

I just have middleware

ZachHandley
30 Oct, 2023, 19:32

but running it with Astro on another instance for a different client and it works fine @Steven

Drake
30 Oct, 2023, 21:15

do you mean you have the exact same code in another project and it works fine?

Drake
30 Oct, 2023, 21:16

middlewares...is that executed server side?

ZachHandley
30 Oct, 2023, 21:16

yes

ZachHandley
30 Oct, 2023, 21:16

and yes, but all it's doing is redirecting on lack of auth

ZachHandley
30 Oct, 2023, 21:16
TypeScript
import { defineMiddleware } from "astro:middleware";
import { $isLoggedIn } from "./store/authStore";

export const onRequest = defineMiddleware(async (context, next) => {
    if (context.url.pathname.includes("dashboard")) {
        const user = await $isLoggedIn();
        if (!user) {
            context.redirect("/login");
        }
    } else if (context.url.pathname.includes("login")) {
        const user = await $isLoggedIn();
        if (user) {
            context.redirect("/dashboard");
        }
    }
    next();
});
ZachHandley
30 Oct, 2023, 21:16

my middleware

Drake
30 Oct, 2023, 21:16

your session is probably only client side and doesn't exist server side

ZachHandley
30 Oct, 2023, 21:17
TypeScript
export const $isLoggedIn = async () => {
  try {
    const user = await account.get();
    if (user) {
      const userObject = AuthUser.parse(user);
      if (userObject.$id !== $user.get().$id || $user.get() != userObject) {
        $user.set(userObject);
      }
      return true;
    } else {
      return false;
    }
  } catch (e: any) {
    return false;
  }
};
ZachHandley
30 Oct, 2023, 21:17

oh

ZachHandley
30 Oct, 2023, 21:17

right. That would do that. But it's not redirecting me

Drake
30 Oct, 2023, 21:18

do you need to call return after context.redirect()?

Drake
30 Oct, 2023, 21:18

else, that next() would execute, right?

ZachHandley
30 Oct, 2023, 21:23

Hm, maybe, but I really don't think the middleware is causing it, but maybe, I don't know why that would interfere with my client side though?

ZachHandley
31 Oct, 2023, 01:19

@Steven still can't figure this out, here's all of the relevant code

TypeScript
const client = new Client()
  .setEndpoint("https://myapprwite.com/v1")
  .setProject("myproject");
export const account = new Account(client);
export const database = new Databases(client);
export const functions = new Functions(client);

export const $createDocument = async (
  collectionId: string,
  data: any,
  docId?: string
) => {
  try {
    let id = docId || ID.unique();
    const document = await database.createDocument(
      "maindb",
      collectionId,
      id,
      data
    );
    return document;
  } catch (e: any) {
    console.error(e);
    return null;
  }
};

export const $getDocument = async (collectionId: string, docId: string) => {
  try {
    const document = await database.getDocument("maindb", collectionId, docId);
    return document;
  } catch (e: any) {
    console.error(e);
    return null;
  }
};
TypeScript
export const $loginUser = async (email: string, password: string) => {
  try {
    const user = await account.createEmailSession(email, password);
    await $isLoggedIn();
    return true;
  } catch (e: any) {
    console.error(e);
    return null;
  }
};
TypeScript
const isLoggedIn = await $isLoggedIn();
  if (isLoggedIn) {
    const userPrefs = await account.getPrefs();
    if (!(userPrefs.bio && userPrefs.bio.length > 0)) {
      needBio.value = true;
      loaded.value = true;
    } else {
      needBio.value = false;
      userBio.value = userPrefs.bio;
      isUserAdmin.value = userPrefs.isAdmin ? true : false;
      const existingChat = await $getCurrentChat();
      if (
        curChat.value.userId &&
        curChat.value.userId.length > 0 &&
        curChat.value.$id.length > 0 &&
        !loaded.value
      ) {
        console.log("Getting current chat");
        if (!existingChat) {
          console.log("Creating chat");
          await createChat();
        } else {
          console.log("Getting current chat messages");
          await $getCurrentChatMessages();
        }
      } else {
        console.log("Current chat failed to load: ", curChat.value);
      }
      loaded.value = true;
      console.log("Loaded user prefs");
    }
  } else {
    console.error("User not logged in");
    needsRedirectToLogin.value = true;
  }
ZachHandley
31 Oct, 2023, 01:20

I really don't understand why when it was working perfectly fine yesterday, I even logged in <a:spinning_think:400736652089360394>

ZachHandley
31 Oct, 2023, 01:25

okay so

ZachHandley
31 Oct, 2023, 01:25

it works now, no idea why, I'm gonna close this, there's an issue with something else but that's on me

ZachHandley
31 Oct, 2023, 01:26

[SOLVED] User (role: guests) missing scope (account) but I'm signed in...?

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