46 lines
1.2 KiB
Dart
46 lines
1.2 KiB
Dart
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:google_sign_in/google_sign_in.dart';
|
|
|
|
class LoginView extends StatefulWidget {
|
|
const LoginView({super.key});
|
|
|
|
@override
|
|
State<LoginView> createState() => _LoginViewState();
|
|
}
|
|
|
|
class _LoginViewState extends State<LoginView> {
|
|
|
|
Future<UserCredential> signInWithGoogle() async {
|
|
// Trigger the authentication flow
|
|
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
|
|
|
|
// Obtain the auth details from the request
|
|
final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication;
|
|
|
|
// Create a new credential
|
|
final credential = GoogleAuthProvider.credential(
|
|
accessToken: googleAuth?.accessToken,
|
|
idToken: googleAuth?.idToken,
|
|
);
|
|
|
|
// Once signed in, return the UserCredential
|
|
return await FirebaseAuth.instance.signInWithCredential(credential);
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
signInWithGoogle().then((value) => print(value));
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return const Scaffold(
|
|
body: Center(
|
|
child: Icon(Icons.login)
|
|
)
|
|
);
|
|
}
|
|
}
|