Created
July 23, 2018 19:40
-
-
Save NilsBacke/a5ee2092eb824f89b571ced866014757 to your computer and use it in GitHub Desktop.
A example of using firebase authentication in a flutter application.
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 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:firebase_auth/firebase_auth.dart'; | |
void main() => runApp(new MaterialApp( | |
title: "Auth Demo", | |
home: Home(), | |
)); | |
final FirebaseAuth _auth = FirebaseAuth.instance; | |
class Home extends StatefulWidget { | |
@override | |
_HomeState createState() => _HomeState(); | |
} | |
class _HomeState extends State<Home> { | |
TextEditingController emailController = new TextEditingController(); | |
TextEditingController passwordController = new TextEditingController(); | |
@override | |
Widget build(BuildContext context) { | |
return Container(); | |
} | |
@override | |
void initState() { | |
super.initState(); | |
getUser().then((user) { | |
if (user != null) { | |
// send the user to the home page | |
// homePage(); | |
} | |
}); | |
} | |
Future<FirebaseUser> getUser() async { | |
return await _auth.currentUser(); | |
} | |
void signUpWithEmail() async { | |
// marked async | |
FirebaseUser user; | |
try { | |
user = await _auth.createUserWithEmailAndPassword( | |
email: emailController.text, | |
password: passwordController.text, | |
); | |
} catch (e) { | |
print(e.toString()); | |
} finally { | |
if (user != null) { | |
// sign in successful! | |
// ex: bring the user to the home page | |
} else { | |
// sign in unsuccessful | |
// ex: prompt the user to try again | |
} | |
} | |
} | |
void signInWithEmail() async { | |
// marked async | |
FirebaseUser user; | |
try { | |
user = await _auth.signInWithEmailAndPassword( | |
email: emailController.text, password: passwordController.text); | |
} catch (e) { | |
print(e.toString()); | |
} finally { | |
if (user != null) { | |
// sign in successful! | |
// ex: bring the user to the home page | |
} else { | |
// sign in unsuccessful | |
// ex: prompt the user to try again | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment