Back

There is session but inside middleware.ts, cannot get session

  • 0
  • Self Hosted
  • Auth
  • Web
Suiii
7 Jan, 2025, 09:32
TypeScript
// function to get session.
export async function auth() {
  try {
    const cookieStore = await cookies()
    const sessionCookie = cookieStore.get("appwrite-session")
    if (!sessionCookie) {
      log.info("No session cookie found")
      return null
    }
    const { account } = await createSessionClient()
    return await account.get()
  } catch (error) {
    console.error("Error during authentication:", error)
    return null
  }
}
TL;DR
Developers are trying to access session inside 'middleware.ts' but facing difficulties. They mention that the application works fine on 'localhost' and 'self-hosted' setups. The provided code snippet shows an 'auth' function that tries to retrieve a session cookie but encounters issues. Solution: Check if the cookieStore.get("appwrite-session") is returning the session correctly. Make sure that the session is properly set up and accessible within the middleware. Double-check the middleware configuration and ensure the cookie is being passed correctly for session retrieval.
Suiii
7 Jan, 2025, 09:33

locallhost, and also self hosted woking just fine

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