I'm implementing Google OAuth2 authentication in React using Appwrite SDK. The flow is:
- User clicks "Sign in with Google" button
- Gets redirected to Google login
- Google redirects back to success URL (http://localhost:5173)
- I redirect to Profile page
Problem: When Profile component mounts and calls account.getSession('current'),
I get 401 Unauthorized with error: "User (role: guests) missing scopes (["account"])"
This indicates the session isn't being properly stored/recognized after the OAuth2 redirect.
My Current Code:
appwrite.js:
import { Client, Account, OAuthProvider } from "appwrite";
const client = new Client()
.setEndpoint(import.meta.env.VITE_APPWRITE_ENDPOINT)
.setProject(import.meta.env.VITE_APPWRITE_PROJECT_ID);
const account = new Account(client);
export { client, account, OAuthProvider };
login.jsx - handleGoogleLogin:
const handleGoogleLogin = () => {
try {
account.createOAuth2Session({
provider: OAuthProvider.Google,
success: "http://localhost:5173",
failure: "http://localhost:5173/login"
});
} catch (err) {
setError(err.message);
}
};
profile.jsx - where the error occurs:
const getSession = async () => {
const session = await account.getSession({
sessionId: 'current'
});
console.log(session);
}
useEffect(() => {
getSession();
}, []);
Console Error:
GET https://sgp.cloud.appwrite.io/v1/account/sessions/current 401 (Unauthorized) AppwriteException: User (role: guests) missing scopes (["account"])
How to Fix
Recommended threads
- Appwrite github organization error
I have github student account. The auth has been paused however the database is working fine. I cant see any option to resume from my console panel. It is just ...
- Appwrite project paused
I have github student account. The auth has been paused however the database is working fine. I cant see any option to resume from my console panel. It is just ...
- Missing Invoice
Hi! I need help with billing on my account (marelu@gmail.com). I have never received a single invoice by email. My organization has been on the Pro plan since...