Back

NextJS Auth SSR Session value is empty and session secret

  • 0
  • Self Hosted
  • Web
dev_goran
14 Aug, 2024, 16:05

Hi, I have a problem with SSR Auth I can create user on selfhosted appwrite and login give me that user information but session does not create. Nextjs 14.1.4 Appwrite 13. here is my appwrite config: ```'use server' import {Client, Account, Databases} from 'node-appwrite' import {cookies} from 'next/headers' import {SESSION_COOKIE} from './const'

export async function createSessionClient() { const client = new Client() .setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT as string) .setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT as string) const session = cookies().get(SESSION_COOKIE)

if (!session || !session.value) { throw new Error('No session') } client.setSession(session.value)

return { get account() { return new Account(client) }, } }

export async function createAdminClient() { const client = new Client() .setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT as string) .setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT as string) .setKey(process.env.NEXT_PUBLIC_API_KEY as string)

return { get account() { return new Account(client) },

TypeScript
get databases() {
  return new Databases(client)
},

} }

export async function getLoggedInUser() { try { const {account} = await createSessionClient() return await account.get() } catch (error) { console.log(error) return null } } and here is func for session const email = parsed.data.email const password = parsed.data.password

const {account} = await createAdminClient() const session = await account.createEmailPasswordSession(email, password)

cookies().set(SESSION_COOKIE, session.secret, { path: '/', httpOnly: true, sameSite: 'strict', secure: true, }) redirect('/dashboard')``` Thank you in advance!

TL;DR
Developers are having an issue with NextJS Auth SSR, where the session value is empty and the session secret is not being created despite successful sign-up or login. The code snippets provided show the creation of a session client and admin client, as well as functions for getting a logged-in user. It seems there may be an error in setting the session cookie. Verify the session creation logic and ensure the session value is properly stored.
Kenny
14 Aug, 2024, 16:09

Are you seeing any data returned after calling createEmailPasswordSession?

Kenny
14 Aug, 2024, 16:11

Is this for signing in or signing up?

dev_goran
14 Aug, 2024, 16:27

Yes. I'm seeing user data when I sign up or sign in except secret, session.value is empty

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