Skip to content
>/
1 Jan, 2025, 17:57

I'm encountering an OAuth2 error while implementing Google login with Appwrite. I need help to resolve this issue.

api:

`export async function signInWithGoogle() { try { const session = await account.createOAuth2Session( 'google', 'http://localhost:5173', 'http://localhost:5173/sign-in' ); return session; } catch (error) { console.log(error); return null; } }

export async function saveGoogleUser(user: { accountId: string; email: string; name: string; imageUrl: URL; }) { try { const newUser = await databases.createDocument( appwriteConfig.databaseId, appwriteConfig.userCollectionId, ID.unique(), { accountId: user.accountId, email: user.email, name: user.name, username: user.email.split('@')[0], imageUrl: user.imageUrl, } );

TypeScript
return newUser;

} catch (error) { console.log(error); return null; } }

export async function getGoogleUserInfo() { try { const currentAccount = await getAccount(); if (!currentAccount) throw Error;

TypeScript
const existingUser = await databases.listDocuments(
  appwriteConfig.databaseId,
  appwriteConfig.userCollectionId,
  [Query.equal("email", currentAccount.email)]
);

if (existingUser.documents.length > 0) {
  return existingUser.documents[0];
}

const avatarUrl = avatars.getInitials(currentAccount.name);
return await saveGoogleUser({
  accountId: currentAccount.$id,
  name: currentAccount.name,
  email: currentAccount.email,
  imageUrl: avatarUrl,
});

} catch (error) { console.log(error); return null; } }

TL;DR
Developers are facing OAuth2 error with Google login implementation in Appwrite. The error might be due to misconfiguration or authorization issue. Check OAuth2 setup and permissions.
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