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
- Getting current user has been blocked
Getting current user has been blocked error while signing in the appwrite console. Got this during I am testing with the my saas web project. Please help to unb...
- Android SDK: Kotlin null Value Not Updat...
Hi team, I think you should check the Android SDK implementation once, as it seems to have an issue handling Kotlin **null** values. For example: When I send:...
- Can't connect GitHub — 500 error at the ...
Appwrite Cloud, Pro plan, SFO region. All my Git connections vanished today — Sites and all 15 Functions now show no repository connected. When I try to add a ...