Skip to content
Back

Need help with `Facebook` auth

  • 0
  • Auth
  • Web
Humayun Kabir
19 Jun, 2024, 18:46

Hello appwrite lovers,

I am trying to implement Facebook auth in my NextJS app. I am using /app directory.

Here is my action:

TypeScript
'use server';

import { Account, Client, OAuthProvider } from 'appwrite';
import { cookies } from 'next/headers';

export async function signinWithFacebook() {
  const client = new Client()
    .setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT!)
    .setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT!);

  const account = new Account(client);

  try {
    account.createOAuth2Session(OAuthProvider.Facebook, '/', '/auth/signin', [
      'email',
      'public_profile',
    ]);

    const session = await account.getSession('current');

    cookies().set('session', session.secret, {
      path: '/',
      httpOnly: true,
      sameSite: 'strict',
      secure: true,
    });
  } catch (error) {
    console.log(error);
  }
}

And here is my login button:

TypeScript
<form action={signinWithFacebook}>
  <Button variant='outline' type='submit'>
    <Facebook className='mr-2' /> Sign in With Facebook
  </Button>
</form>

But when I am clicking the button I am getting this error:

TypeScript
[AppwriteException: User (role: guests) missing scope (account)] {
  name: 'AppwriteException',
  code: 401,
  type: 'general_unauthorized_scope',
  response: {
  message: 'User (role: guests) missing scope (account)',
  code: 401,
  type: 'general_unauthorized_scope',
  version: '1.5.7'
}

Can anyone help me to resolve this issue. Big thanks in advance.

TL;DR
Developers are trying to implement Facebook authentication in a NextJS app using the Appwrite SDK but face an error regarding missing scope 'account.' To resolve this, they need to ensure that the user has the required scope.
Kenny
19 Jun, 2024, 18:50

Have you looked at this tutorial for ssr auth in nextjs?

https://appwrite.io/docs/tutorials/nextjs-ssr-auth/step-3

Right off the bat I see youre using the client side sdk, you should be using node-appwrite on server actions.

Humayun Kabir
19 Jun, 2024, 18:52

Yes, I was able to create EmailPasswordSession successfully following this article.

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