Skip to content

Instantly share code, notes, and snippets.

@rsegecin
Created February 4, 2022 06:57
Show Gist options
  • Save rsegecin/f9cceb81d6bfd6e715badb0a032ae4ec to your computer and use it in GitHub Desktop.
Save rsegecin/f9cceb81d6bfd6e715badb0a032ae4ec to your computer and use it in GitHub Desktop.
Firebase not running on web
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:funtestic/firebase_options.dart';
import 'package:google_sign_in/google_sign_in.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final GoogleSignIn _googleSignIn = GoogleSignIn();
Future<void> _signInWithGoogle() async {
try {
GoogleSignInAccount? googleSignInAccount = await _googleSignIn.signIn();
if (googleSignInAccount != null) {
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
await FirebaseAuth.instance.signInWithCredential(credential);
String token = await FirebaseAuth.instance.currentUser!.getIdToken();
print("you've logged");
}
} catch (error) {}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
child: const Text("log in"),
onPressed: _signInWithGoogle,
)
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment