mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2025-07-10 06:10:35 +08:00
New Example - Firebase Google Authentication (#106)
* Added the firebase+google_authentication example * Modified Readme.md
This commit is contained in:
49
firebase_google_authentication/lib/Services/google_auth.dart
Normal file
49
firebase_google_authentication/lib/Services/google_auth.dart
Normal file
@ -0,0 +1,49 @@
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:google_sign_in/google_sign_in.dart';
|
||||
|
||||
|
||||
class GoogleSignInProvider extends ChangeNotifier{
|
||||
|
||||
final googleSignIn = GoogleSignIn();
|
||||
|
||||
GoogleSignInAccount? _user;
|
||||
User? user1;
|
||||
GoogleSignInAccount get user => _user!;
|
||||
String? userId;
|
||||
FirebaseAuth auth = FirebaseAuth.instance;
|
||||
|
||||
Future googleLogin()async{
|
||||
try{
|
||||
final googleUser = await googleSignIn.signIn();
|
||||
if(googleUser == null) return;
|
||||
_user = googleUser;
|
||||
|
||||
final googleAuth = await googleUser.authentication;
|
||||
|
||||
final credentials = GoogleAuthProvider.credential(
|
||||
accessToken: googleAuth.accessToken,
|
||||
idToken: googleAuth.idToken,
|
||||
);
|
||||
|
||||
final UserCredential authresult= await FirebaseAuth.instance.signInWithCredential(credentials);
|
||||
notifyListeners();
|
||||
}catch(e){
|
||||
print(e.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Future googleLogout()async{
|
||||
try{
|
||||
print('Logging Out');
|
||||
await googleSignIn.disconnect();
|
||||
FirebaseAuth.instance.signOut();
|
||||
}catch(e){
|
||||
FirebaseAuth.instance.signOut();
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user