
Hi, I have problem with auth. When I try to login/signup using OTP, at the end session.secret is empty, but looks like a whole procces passed correctly. I have tried creating new API key (someone response from simillar post), but still don't work
Using Nextjs 14.2.8, node-appwrite 13
// app/signup/page.tsx
import { createAdminClient, getLoggedInUser } from "@/lib/server/appwrite"
import { cookies } from "next/headers"
import { redirect } from "next/navigation"
import { ID } from "node-appwrite"
export default async function SignupPage() {
const user = await getLoggedInUser()
if (user) redirect("/rooms")
return (
<>
<form action={sendEmail}>
<input type="email" name="email" placeholder="email@email.com" />
<button type="submit">Send Email</button>
</form>
<form action={login}>
<input type="text" name="code" placeholder="code" />
<button type="submit">Submit code</button>
</form>
</>
)
}
const sendEmail = async (formData: FormData) => {
'use server'
const email = formData.get('email') as string
const { account } = await createAdminClient()
const sessionToken = await account.createEmailToken(
ID.unique(),
email
)
cookies().set('user-id', sessionToken.userId, {
path: '/',
})
}
const login = async (formData: FormData) => {
'use server'
const code = formData.get('code') as string
const { account } = await createAdminClient()
const userID = cookies().get('user-id')?.value
const session = await account.createSession(
userID as string,
code
)
console.log(session, session.secret)
cookies().set('user-session', session.secret, {
path: '/',
sameSite: 'lax',
})
redirect('/rooms')
}```

// @/lib/server/appwrite
"use server"
import { cookies } from "next/headers"
import { Account, Client } from "node-appwrite"
export const createSessionClient = async () => {
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('user-session')
if (!session || !session.value) {
throw new Error('No session found')
}
client.setSession(session.value)
return {
get account() {
return new Account(client)
}
}
}
export const createAdminClient = async () => {
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.APPWRITE_API_KEY as string)
return {
get account() {
return new Account(client)
}
}
}
export const getLoggedInUser = async () => {
try {
const { account } = await createSessionClient()
return await account.get()
} catch (err) {
return null
}
}
{
'$id': '66e99b59ae204f8cea19',
'$createdAt': '2024-09-17T15:08:09.719+00:00',
'$updatedAt': '2024-09-17T15:08:09.719+00:00',
userId: '66e996ea0020587be57b',
expire: '2025-09-17T15:08:09.744+00:00',
provider: 'token',
providerUid: '',
providerAccessToken: '',
providerAccessTokenExpiry: '',
providerRefreshToken: '',
ip: '10.0.0.2',
osCode: 'LIN',
osName: 'GNU/Linux',
osVersion: '',
clientType: '',
clientCode: '',
clientName: '',
clientVersion: '',
clientEngine: '',
clientEngineVersion: '',
deviceName: 'desktop',
deviceBrand: '',
deviceModel: '',
countryCode: '--',
countryName: 'Unknown',
current: true,
factors: [ 'email' ],
secret: '',
mfaUpdatedAt: ''
}
Recommended threads
- CORS + 401 Error with Appwrite Authentic...
I'm getting a CORS + 401 Error with Appwrite Authentication Access to fetch at 'https://cloud.appwrite.io/v1/account/sessions/email' from origin 'https://my-c...
- CLI login on self hosted with docker
Hi, I have a working docker setup with traefik in front of the services. Everything works as expected except for login with CLI. I've tried with `appwrite cli...
- Exposing project id and endpoint on GitH...
Is it best practice to expose your project id and endpoint in your appwrite.js file when publishing an NextJS or Angular project. If you use a .env file, that i...
