This repository has been archived by the owner on Oct 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
login_page.dart
85 lines (73 loc) · 2.51 KB
/
login_page.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:twrpbuilder_plugin/twrpbuilder_plugin.dart';
import 'home_page.dart';
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = new GoogleSignIn();
String name = "";
String email = "";
String userProfile = "";
class GoogleLoginPage extends StatefulWidget {
@override
_GoogleLoginPageState createState() => new _GoogleLoginPageState();
}
class _GoogleLoginPageState extends State<GoogleLoginPage> {
static Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
Future<void> initRootRequest() async {
bool rootAccess = await TwrpbuilderPlugin.rootAccess;
final SharedPreferences prefs = await _prefs;
prefs.setBool("isRootGranted", rootAccess);
}
Future<FirebaseUser> _signInWithGoogle() async {
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication _gSAuth = await googleUser.authentication;
final FirebaseUser _user = await _auth.signInWithGoogle(
idToken: _gSAuth.idToken,
accessToken: _gSAuth.accessToken,
);
assert(_user != null);
assert(!_user.isAnonymous);
assert(_user.email != null);
assert(_user.displayName != null);
final FirebaseUser currentUser = await _auth.currentUser();
if (currentUser.email != null) {
//Navigator.pop(context);
Navigator.pushReplacement(context,
new MaterialPageRoute(builder: (BuildContext context) {
return new HomePage();
}));
name = currentUser.displayName;
email = currentUser.email;
userProfile = currentUser.photoUrl;
}
assert(_user.uid == currentUser.uid);
return _user;
}
@override
void initState() {
// TODO: implement initState
super.initState();
initRootRequest();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
textTheme:
TextTheme(title: TextStyle(fontSize: 20.0, fontFamily: 'Raleway')),
title: new Text('Login'),
centerTitle: true,
),
body: Center(
child: new FlatButton.icon(
onPressed: _signInWithGoogle,
icon: new Icon(Icons.email),
textColor: Colors.white,
color: Colors.blue,
label: Text("Sign in with Google"))),
);
}
}