Skip to content
Back

OAuth2 with flutter web - google

  • 0
  • Flutter
André Gomes
11 Feb, 2025, 00:26

I'm trying to implement Google login using Appwrite in my Flutter web app. The login process opens a new tab for authentication, but after logging in:

The popup remains open instead of closing automatically. The main page does not immediately update to show the logged-in user. The login button stays disabled even if I close the popup manually. There's no logout button appearing after login.

Future<void> googleLogin() async { setState(() => isLoading = true); try { final account = Account(widget.account.client); await account.createOAuth2Session( provider: OAuthProvider.google, success: 'http://localhost:3000/auth.html', );

TypeScript
await Future.delayed(Duration(seconds: 2));

final user = await account.get();
if (user != null) {
  setState(() {
    loggedInUser = user;
    isLoading = false;
  });
} else {
  setState(() => isLoading = false);
  ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(content: Text('Error retrieving user data')),
  );
}

} catch (e) { setState(() => isLoading = false); ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Login failed: $e')), ); } }

My auth.html file contains: <!DOCTYPE html>

<title>Authentication complete</title> <p>Authentication is complete. If this does not happen automatically, please close the window.</p> <script> window.opener.postMessage({ 'flutter-web-auth-2': window.location.href }, window.location.origin); window.close(); </script>

Questions: How can I make the popup window close automatically after login? How do I ensure the main page updates immediately with the logged-in user info? Is there a better approach to handling OAuth login in Flutter Web with Appwrite? Any help would be greatly appreciated! Thanks in advance.

TL;DR
- Developers facing issues with Google login implementation using Appwrite in Flutter web. - Popup window doesn't close automatically after login. - Main page doesn't update immediately with the logged-in user info. - Login button remains disabled post popup closure. - No logout button appears after login. - Code snippet provided for context. - Suggestions for improvements requested. - HTML script provided for authentication completion message. Solution: Update the script in `auth.html` to include an event listener to handle the message sent from the popup window, then close the window.
André Gomes
11 Feb, 2025, 00:31
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