Created
September 16, 2020 00:12
-
-
Save panuavakul/3e1581e4b55ff40a7076f09e2292fc97 to your computer and use it in GitHub Desktop.
Just testing out Firebase Auth with Flutter for web
This file contains 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/firebase.dart' as firebase; | |
import 'package:firebase_auth/firebase_auth.dart' as firebaseAuth; | |
import 'package:firebase_core/firebase_core.dart'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Firebase Auth Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key}) : super(key: key); | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
User _user; | |
bool _isAuthReady = false; | |
final Future<FirebaseApp> _initFirebase = Firebase.initializeApp(); | |
Future<void> _signin() async { | |
final auth = firebaseAuth.FirebaseAuth.instance; | |
await auth.signInWithEmailAndPassword( | |
email: 'test@example.com', | |
password: 'example1', | |
); | |
final user = User( | |
uid: auth.currentUser.uid, | |
email: auth.currentUser.email, | |
); | |
setState(() { | |
_user = user; | |
}); | |
} | |
Future<void> _signOut() async { | |
final auth = firebaseAuth.FirebaseAuth.instance; | |
await auth.signOut(); | |
setState(() { | |
_user = null; | |
}); | |
print('Signed out'); | |
} | |
Future<void> _getUser() async { | |
final firebaseUser = | |
await firebase.auth(firebase.app()).onAuthStateChanged.first; | |
final user = firebaseUser != null | |
? User(uid: firebaseUser.uid, email: firebaseUser.email) | |
: null; | |
setState(() { | |
_isAuthReady = true; | |
_user = user; | |
}); | |
} | |
Future<void> _checkUser() async { | |
final auth = firebaseAuth.FirebaseAuth.instance; | |
print(auth.currentUser); | |
} | |
@override | |
void initState() { | |
_getUser(); | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Container( | |
child: FutureBuilder( | |
future: _initFirebase, | |
builder: (context, snapshot) { | |
if (snapshot.hasError) { | |
// Error | |
return Text('Error!'); | |
} | |
if (snapshot.connectionState == ConnectionState.done) { | |
// Success | |
return Column( | |
children: [ | |
RaisedButton( | |
onPressed: _signin, | |
child: Text('Sign in'), | |
), | |
RaisedButton( | |
onPressed: _checkUser, | |
child: Text('Check User'), | |
), | |
RaisedButton( | |
onPressed: _signOut, | |
child: Text('Sign out'), | |
), | |
Text(_isAuthReady | |
? _user?.email ?? 'No User' | |
: 'Loading Auth'), | |
], | |
); | |
} | |
// Loading | |
return Text('Loading'); | |
}, | |
), | |
), | |
); | |
} | |
} | |
class User { | |
User({ | |
@required this.uid, | |
@required this.email, | |
}); | |
final String uid; | |
final String email; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment