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
- New OAuth provider not appearing in Cons...
Hi team! I'm contributing to Appwrite and currently working on adding Hugging Face as a new OAuth provider. Here's what I've done so far: - Implemented the ...
- Pricing question for the civil society o...
Hey! Can someone help me out? 🥺 Do you have any discounts or programs for non-profit civil society organizations, even if they're not open-source? Or maybe you...
- Unable to add domain – “Domain is owned ...
Hello, I own and control the DNS for futlive9.com, but Appwrite shows “Domain is owned by a different organization”. The domain was previously connected to ano...