Created
April 4, 2024 07:39
-
-
Save tsukifell/c98e62d98b643dcefb84eec376a635dc to your computer and use it in GitHub Desktop.
Example of applying Google and Email Auth in Flutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:firebase_auth/firebase_auth.dart'; | |
import 'package:firebase_learn/presentation/auth/register_screen.dart'; | |
import 'package:firebase_learn/presentation/main_screen.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:google_sign_in/google_sign_in.dart'; | |
class LoginScreen extends StatefulWidget { | |
const LoginScreen({Key? key}); | |
@override | |
LoginScreenState createState() => LoginScreenState(); | |
} | |
class LoginScreenState extends State<LoginScreen> { | |
final _emailController = TextEditingController(); | |
final _passwordController = TextEditingController(); | |
String _errorMessage = ''; | |
Future<UserCredential> loginWithGoogle() async { | |
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn(); | |
final GoogleSignInAuthentication? googleAuth = | |
await googleUser?.authentication; | |
final credential = GoogleAuthProvider.credential( | |
accessToken: googleAuth?.accessToken, idToken: googleAuth?.idToken); | |
return await FirebaseAuth.instance.signInWithCredential(credential); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Sign In'), | |
), | |
body: Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: SingleChildScrollView( | |
child: Column( | |
children: [ | |
const SizedBox(height: 32.0), | |
TextField( | |
controller: _emailController, | |
decoration: const InputDecoration( | |
labelText: 'Email', | |
border: OutlineInputBorder(), | |
), | |
), | |
const SizedBox(height: 16.0), | |
TextField( | |
controller: _passwordController, | |
decoration: const InputDecoration( | |
labelText: 'Password', | |
border: OutlineInputBorder(), | |
), | |
obscureText: true, | |
), | |
const SizedBox(height: 16.0), | |
ElevatedButton( | |
onPressed: () async { | |
try { | |
await FirebaseAuth.instance.signInWithEmailAndPassword( | |
email: _emailController.text, | |
password: _passwordController.text, | |
); | |
Navigator.of(context).pushReplacement( | |
MaterialPageRoute( | |
builder: (context) => const MainScreen()), | |
); | |
} catch (error) { | |
setState(() { | |
_errorMessage = error.toString(); | |
}); | |
ScaffoldMessenger.of(context).showSnackBar( | |
SnackBar( | |
content: Text(_errorMessage), | |
), | |
); | |
} | |
}, | |
child: const Text('Sign In'), | |
), | |
const SizedBox(height: 16.0), | |
TextButton( | |
onPressed: () async { | |
try { | |
UserCredential userCredential = await loginWithGoogle(); | |
if (userCredential != null) { | |
Navigator.of(context).pushReplacement( | |
MaterialPageRoute( | |
builder: (context) => const MainScreen()), | |
); | |
} | |
} catch (error) { | |
setState(() { | |
_errorMessage = error.toString(); | |
}); | |
ScaffoldMessenger.of(context).showSnackBar( | |
SnackBar( | |
content: Text(_errorMessage), | |
), | |
); | |
} | |
}, | |
child: const Text('Login with Google'), | |
), | |
const SizedBox(height: 32.0), | |
TextButton( | |
onPressed: () { | |
Navigator.push( | |
context, | |
MaterialPageRoute( | |
builder: (context) => const RegisterScreen()), | |
); | |
}, | |
child: const Text('Don\'t have an account? Sign up'), | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment