Back

Creating session right after google OAuth2 login

  • 0
  • Auth
  • Web
  • Cloud
Sandman
9 Mar, 2024, 08:13

I've run into an error while integrating OAuth2 logins into my app. Specifically, after a successful Google login, I'm encountering an "AppwriteException" indicating that the user is missing the "account" scope.

AppwriteException: User (role: guests) missing scope (account)

After consulting the documentation and discussions on Discord, I realized that I forgot to create a session after the Google login. The documentation suggests using the account.createSession(userId, secret) method to create the session.

https://appwrite.io/docs/products/auth/oauth2#init

TypeScript
const urlParams = new URLSearchParams(window.location.search);
const secret = urlParams.get('secret');
const userId = urlParams.get('userId');

await account.createSession(userId, secret);

However, I'm unsure about the appropriate timing to call this method in my Next.js app. It doesn't seem efficient to call it every time the page loads using useEffect. Any thoughts on this?

If there's already a solution to this, please provide the relevant URL

TL;DR
Developers are experiencing session creation issues post Google OAuth2 login due to missing scopes. The solution involves implementing the account.createSession(userId, secret) method after Google login. For efficiency in a Next.js app, consider the timing of calling this method. Relevant documentation: https://appwrite.io/docs/products/auth/oauth2#init
ideclon
10 Mar, 2024, 10:34

You'd do this once, immediately after Google redirects back to your app

ideclon
10 Mar, 2024, 10:34

(In fact, it will only work once - if you try again you should receive an "Invalid secret" error, I think)

mrkumaramit
27 Mar, 2024, 06:04

did you find this fix?

I am facing similar problem.

scorcism
4 Sep, 2024, 14:26

bello

any updates ? 🙂

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