Admin
Possibly ..I don't know too much about Astro...is there any server side code?
Votes
0
Replies
24
Participants
Unknown
Messages
25
Admin
Possibly ..I don't know too much about Astro...is there any server side code?
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
internal 500 Error code
Here some console error that i get:
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:158app.d2040772.js:1:133492and here a network analyse seems your api host got rect.
Moderator
I just have middleware
Moderator
but running it with Astro on another instance for a different client and it works fine @Steven
Admin
do you mean you have the exact same code in another project and it works fine?
Admin
middlewares...is that executed server side?
Moderator
yes
Moderator
and yes, but all it's doing is redirecting on lack of auth
Moderator
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();});Moderator
my middleware
Admin
your session is probably only client side and doesn't exist server side
Moderator
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; }};Moderator
oh
Moderator
right. That would do that. But it's not redirecting me
Admin
do you need to call return after context.redirect()?
Admin
else, that next() would execute, right?
Moderator
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?
Moderator
@Steven still can't figure this out, here's all of the relevant code
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; }};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; }};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; }Moderator
I really don't understand why when it was working perfectly fine yesterday, I even logged in 
Moderator
okay so
Moderator
it works now, no idea why, I'm gonna close this, there's an issue with something else but that's on me
Moderator
[SOLVED] User (role: guests) missing scope (account) but I'm signed in...?