Google Sign In
Flutter Package
Process - I
Package install : - firebase_auth / firebase_core / google_sign_in
Process -II
Create GoogleSignIn Class
import 'package:google_sign_in/google_sign_in.dart';
import 'package:firebase_auth/firebase_auth.dart';
class GoogleSignInClass{
static final FirebaseAuth auth = FirebaseAuth.instance;
static final GoogleSignIn googleSignIn=GoogleSignIn();
static Future<dynamic> SignIn()async{
final GoogleSignInAccount? googleSignInAccount=await googleSignIn.signIn();
final GoogleSignInAuthentication? googleSignInAuthentication=await googleSignInAccount!.authentication;
final AuthCredential credential=GoogleAuthProvider.credential(
accessToken: googleSignInAuthentication!.accessToken,
idToken: googleSignInAuthentication.idToken,
);
await auth.signInWithCredential(credential);
}
// Sign Out
static Future signOut()async{
try{
await auth.signOut();
}catch(e){
print(e);
}
}
// User
static User getUser(){
User user=auth.currentUser!;
return user;
}
}
Process - III
Use in Main File
void main()async{
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: FirebaseOptions(
apiKey: "------------------",
appId: "1:-------------------------1",
messagingSenderId: "9-------------8",
projectId: "f--------------d"
),
);
Call SignIn Method on Button Pressed
ElevatedButton(
child: Text("Sign Up"),
onPressed: ()async{
await GoogleSignInClass.SignIn();
},
),
No comments:
Post a Comment