User (role: guests) missing scope (account) after creating OAuth2 session with Discord
- 0
- Self Hosted
- Auth
- Web
Hi all, I was looking through the support page for my specific issue but I was unable to find anything that I could adapt for my issue.
I rebuilt my Authentication in the last days after realizing that OAuth Authentication with SSR does not create a providerAccessToken right now. So I now try to create a session with account.createOAuth2Session instead using the Client SDK. Unfortunately, I seem to always get the Error 'User (role: guests) missing scope (account)' although I can see a session for my user in my selfhosted Appwrite as well as a cookie with my project name for my Appwrite Domain.
First off my function for calling createOAuth2Session
"use server"
export async function signUpWithDiscord() {
const client = new Client()
.setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT)
.setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT);
const account = new Account(client);
const origin = headers().get("origin");
const redirectUrl = await account.createOAuth2Session(
OAuthProvider.Discord,
`${origin}/auth/oauth2/success`,
`${origin}/auth/oauth2/failure`,
//scopes
);
return redirect(redirectUrl);
};```
After Authentication with Discord, I am redirected to /auth/oauth2/session where I execute the following code:
export async function GET(request) { const key = request.nextUrl.searchParams.get("key"); const secret = request.nextUrl.searchParams.get("secret"); const origin = headers().get("host"); cookies().set(key, secret, { domain: origin, path: "/", httpOnly: true, sameSite: "strict", maxAge: 60 * 60 * 24 * 7, secure: true, }); // make the session persistent as a cookie
const client = new Client() .setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT) .setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT);
const account = new Account(client); const currsession = await account.getSession('current'); // Here the error occurs
return NextResponse.redirect(${baseURL + "/"}
);
}
Thanks in advance!
I needed to put some info into a response:
Here are some metrics regarding my stack: Appwrite self-hosted 1.5.7 Appwrite SDK 15.0.0 NextJS 14.2.3 3rd party cookies are allowed in the browser I use for testing
My user is created/updated correctly and also gets a session in Appwrite Web Console (see appended Image)
In Addition, I can see that the user accont is linked with Discord properly (see appended Image) and in my Appwrite domain, I have cookies with the name a_session_[PROJECT_ID] and a_session_[PROJECT_ID]_legacy.
But when I try to access the User or the Session in the code, I always get the error as if my user is not authenticated.
Can anyone help me or point me in the correct direction? I am quite sure that I misunderstood something in the process and that is where the error is stemming from but I can't seem to find the specific step I do wrong.
Thanks in advance for the help!
Recommended threads
- Different appwrite IDs are getting expos...
File_URL_FORMAT= https://cloud.appwrite.io/v1/storage/buckets/[BUCKET_ID]/files/[FILE_ID]/preview?project=[PROJECT_ID] I'm trying to access files in my web app...
- Invalid document structure: missing requ...
I just pick up my code that's working a week ago, and now I got this error: ``` code: 400, type: 'document_invalid_structure', response: { message: 'Inv...
- Error: User (role: guests) missing scope...
I want to send a verification code to the user and the given phone number and check it and create a session right after the user entered the secret. For me that...