Back

Next.js Login Self-hosted (role: applications) missing scope (public) Error

  • 0
  • Self Hosted
  • Auth
  • Web
Tin Chu
16 Oct, 2024, 19:45

Im trying to follow the tutorial from: https://www.youtube.com/watch?v=l9zh0pqEHyc but when I created the API Key it has all the permissions. When Im trying to createSession it says: (role: applications) missing scope (public)

can someone guide me? πŸ™ thanks!

TL;DR
Error occurs when creating email password session due to missing 'public' scope. Ensure API Key has necessary permissions.
Kenny
16 Oct, 2024, 19:55

Please post a snippit of the code you're using that is throwing the error.

Tin Chu
16 Oct, 2024, 19:58
TypeScript
const createAdminClient = async () => {
  const client = new Client()
    .setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_URL)
    .setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID)
    .setKey(process.env.NEXT_PRIVATE_APPWRITE_KEY)

  return {
    get account() {
      return new Account(client)
    },
    get databases() {
      return new Databases(client)
    },
    get storage() {
      return new Storage(client)
    },
  }
}```
Tin Chu
16 Oct, 2024, 19:59

here you can see that its getgging the AdminClient.

Tin Chu
16 Oct, 2024, 19:59
TypeScript
'use server'

import { createAdminClient } from '@/lib/appwrite'
import { signinSchema } from '@/schemas'
import { cookies } from 'next/headers'

export async function createSession(
  previousState: any,
  formData: any
): Promise<{ success: boolean; error?: string | Record<string, string[]> }> {
  const validatedFields = signinSchema.safeParse(formData)

  if (!validatedFields.success) {
    return {
      success: false,
      error: validatedFields.error.flatten().fieldErrors,
    }
  }

  // Get account instance
  const { account } = await createAdminClient()

  try {
    console.log('Creating session...', {
      email: validatedFields.data.email,
      password: validatedFields.data.password,
    })
    const session = await account.createEmailPasswordSession(validatedFields.data.email, validatedFields.data.password)

    // Create cookie
    cookies().set('next-appwrite-session', session.secret, {
      httpOnly: true,
      secure: true,
      sameSite: 'strict',
      expires: new Date(session.expire),
      path: '/',
    })

    return {
      success: true,
      error: null,
    }
  } catch (error) {
    console.log('πŸ›‘ createSession: ', error.message)
    return {
      success: false,
      error: error.message,
    }
  }
}
Tin Chu
16 Oct, 2024, 19:59

and the problem is when the account its being instantiated and creating the email password session :/

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