Back

Nextjs Working with Node SDK and Web SDK

  • 0
  • Auth
  • Web
KHRM
17 Mar, 2025, 06:44

So I am using Nextjs v15 as a full stack and im not sure what the proper flow is

I am trying to compare with supabase where i make a server client and a client client

the 1-1 equivalent i assume is node sdk (server) and web sdk (client)

i use server actions to signup / login / create session with the node sdk client

this works, i can see my user in the appwrite dashboard AND can retrieve the session

but when i am trying to get the client in my react context that I created I am presented with the following errors

TypeScript
GET https://cloud.appwrite.io/v1/account 401 (Unauthorized)
AppwriteException: User (role: guests) missing scope (account)

am i doing something wrong or am i unable to retrieve a session created by the server sdk with the web sdk?

TL;DR
Developers are working with Next.js using a Node SDK and Web SDK for Appwrite. They have server and client functions for user actions. The issue arises when trying to fetch the client in the React context, leading to a "401 Unauthorized" error. The problem seems to be related to the scope, possibly due to the session created by the server SDK not being accessible by the web SDK.
KHRM
17 Mar, 2025, 06:47
TypeScript
// client.ts
'use client'

import { Client, Account } from 'appwrite'

const NEXT_PUBLIC_APPWRITE_ENDPOINT = String(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT)
const NEXT_PUBLIC_APPWRITE_PROJECT = String(process.env.NEXT_PUBLIC_APPWRITE_PROJECT)

const client = new Client()
  .setEndpoint(NEXT_PUBLIC_APPWRITE_ENDPOINT)
  .setProject(NEXT_PUBLIC_APPWRITE_PROJECT)

const account = new Account(client)

export { account }

// user-provider.tsx
useEffect(() => { 
  ;(async function run() {
    try {
      const loggedInUser = await account.get()
      setUser(loggedInUser)
    } catch (err) {
      setUser(null)
    }
  })()
}, [])
KHRM
17 Mar, 2025, 06:47
TypeScript
// server.ts
'use server'
import { Client, Account } from 'node-appwrite'
import { cookies } from 'next/headers'

const NEXT_PUBLIC_APPWRITE_ENDPOINT = String(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT)
const NEXT_PUBLIC_APPWRITE_PROJECT = String(process.env.NEXT_PUBLIC_APPWRITE_PROJECT)
const NEXT_APPWRITE_KEY = String(process.env.NEXT_APPWRITE_KEY)

export async function createSessionClient() {
  const client = new Client()
    .setEndpoint(NEXT_PUBLIC_APPWRITE_ENDPOINT)
    .setProject(NEXT_PUBLIC_APPWRITE_PROJECT)

  const session = (await cookies()).get('my-custom-session')
  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(NEXT_PUBLIC_APPWRITE_ENDPOINT)
    .setProject(NEXT_PUBLIC_APPWRITE_PROJECT)
    .setKey(NEXT_APPWRITE_KEY)

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

/signin.action.ts
'use server'

import { createAdminClient } from '@/utils/appwrite/server'
import { cookies } from 'next/headers'
import { redirect } from 'next/navigation'

export async function signinAction(formData: FormData) {
  const values = Object.fromEntries(formData.entries())
  const email = `${values.email}`
  const password = `${values.password}`

  const { account } = await createAdminClient()

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

  ;(await cookies()).set('my-custom-session', session.secret, {
    path: '/',
    httpOnly: true,
    sameSite: 'strict',
    secure: true,
  })

  redirect('/')
}
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