-
Notifications
You must be signed in to change notification settings - Fork 15
/
login_page_viewmodel.dart
58 lines (54 loc) · 1.83 KB
/
login_page_viewmodel.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import 'dart:async';
import 'package:attendance/loginPage/login_page.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:google_sign_in/google_sign_in.dart';
abstract class LoginPageViewModel extends State<LoginPage> {
final FirebaseAuth auth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = new GoogleSignIn();
LoginPageViewModel() {
auth.currentUser().then((FirebaseUser user) {
if (user != null) {
handleUser(user);
}
});
}
Future<FirebaseUser> signInUser() async {
GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
FirebaseUser firebaseUser = await auth.signInWithGoogle(
idToken: googleSignInAuthentication.idToken,
accessToken: googleSignInAuthentication.accessToken);
return firebaseUser;
}
void handleUser(FirebaseUser user) {
if (user == null) {
Fluttertoast.showToast(
msg: "Failed to login",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIos: 1);
} else {
// @todo #9 just update the user class and call save
FirebaseDatabase.instance
.reference()
.child(user.uid)
.child("name")
.set(user.displayName);
FirebaseDatabase.instance
.reference()
.child(user.uid)
.child("mail")
.set(user.email);
FirebaseDatabase.instance
.reference()
.child(user.uid)
.child("photo")
.set(user.photoUrl);
Navigator.of(context).pushReplacementNamed('/home');
}
}
}