-
Notifications
You must be signed in to change notification settings - Fork 11
/
SignIn.dart
303 lines (273 loc) · 11.3 KB
/
SignIn.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_auth_buttons/flutter_auth_buttons.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'HomePage.dart';
class SignIn extends StatefulWidget {
@override
_SignInState createState() => _SignInState();
}
class _SignInState extends State<SignIn> {
/* Intialize FirebaseAuth & GoogleSignIn and give an instance */
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = GoogleSignIn();
/* Creating key to check FormState(status) */
final GlobalKey<FormState> _formkey = GlobalKey<FormState>();
String _email, _passwaord;
/* Checking whether the user is logged in or not when App starts */
checkAuthentication() async {
_auth.onAuthStateChanged.listen((user) async {
if (user != null) {
Navigator.pushReplacementNamed(context, '/home');
}
});
}
/* Method for Navigation to Sign Up page (optional) */
navigateToSignUpScreen() {
Navigator.pushReplacementNamed(context, '/signup');
}
/* Whenever App starts/restarts i.e. a lifecylce finishes
and starts again following methods are executed
*/
@override
void initState() {
super.initState();
this.checkAuthentication();
}
/* Method to check whether the user is signed in
after all the validation of form is done
*/
void signin() async {
if (_formkey.currentState.validate()) {
_formkey.currentState.save();
try {
AuthResult user = await _auth.signInWithEmailAndPassword(
email: _email,
password: _passwaord,
);
} catch (e) {
showError(e);
}
}
}
/* Google Sign In & Sign Out Method */
Future<FirebaseUser> _signInWithGoogle() async {
try {
final GoogleSignInAccount googleSignInAccount =
await _googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential authCredential = GoogleAuthProvider.getCredential(
idToken: googleSignInAuthentication.idToken,
accessToken: googleSignInAuthentication.accessToken);
final AuthResult authResult =
await _auth.signInWithCredential(authCredential);
final FirebaseUser user = authResult.user;
assert(user.email != null);
assert(user.displayName != null);
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final FirebaseUser currentuser = await _auth.currentUser();
assert(user.uid == currentuser.uid);
print(user.email);
return user;
} catch (e) {}
}
/* Showing the error message */
showError(String errormessage) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Error'),
content: Text(errormessage),
actions: <Widget>[
FlatButton(
child: Text('Ok'),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
// appBar: AppBar(
// title: Text('Sign In'),
// ),
body: Container(
padding: EdgeInsets.fromLTRB(30, 50, 30, 40),
child: Center(
child: ListView(
children: <Widget>[
Card(
child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(10.0, 50.0, 10.0, 50.0),
child: Image(
image: AssetImage('asset/index.png'),
height: 100,
width: 100,
),
),
Container(
padding: EdgeInsets.all(16),
child: Form(
key: _formkey,
child: Column(
children: <Widget>[
// E-mail TextField
Container(
child: TextFormField(
keyboardType: TextInputType.emailAddress,
cursorColor: Colors.white,
style: TextStyle(color: Colors.white),
validator: (input) {
if (input.isEmpty) {
return 'Provide an email';
}
},
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.blue),
borderRadius:
BorderRadius.circular(30)),
contentPadding: EdgeInsets.all(15),
suffixIcon: Icon(
Icons.account_circle,
color: Colors.white,
),
filled: true,
fillColor: Colors.blue,
focusColor: Colors.blue,
border: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.white),
borderRadius:
BorderRadius.circular(30)),
hintStyle: TextStyle(color: Colors.white),
hintText: 'E-mail'),
onSaved: (input) => _email = input,
),
),
Padding(
padding: EdgeInsets.all(10),
),
// Password TextField
Container(
child: TextFormField(
keyboardType: TextInputType.emailAddress,
cursorColor: Colors.white,
style: TextStyle(color: Colors.white),
obscureText: true,
validator: (input) {
if (input.length < 6) {
return 'Password must be atleast 6 char long';
}
},
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.blue),
borderRadius:
BorderRadius.circular(30)),
contentPadding: EdgeInsets.all(15),
suffixIcon: Icon(
Icons.lock,
color: Colors.white,
),
filled: true,
fillColor: Colors.blue,
focusColor: Colors.blue,
border: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.white),
borderRadius:
BorderRadius.circular(30)),
hintStyle: TextStyle(color: Colors.white),
hintText: 'Passwod'),
onSaved: (input) => _passwaord = input,
),
),
Padding(
padding: EdgeInsets.only(top: 60),
),
// Sign In button
RaisedButton(
padding: EdgeInsets.fromLTRB(80, 15, 80, 15),
color: Colors.blue,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadiusDirectional.circular(30),
),
onPressed: signin,
child: Text(
'Log In',
style: TextStyle(
color: Colors.white, fontSize: 20),
)),
Padding(
padding: EdgeInsets.only(top: 20),
),
// Text Button to Sign Up page
GestureDetector(
onTap: navigateToSignUpScreen,
child: Text(
'Create an account',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 16.0, color: Colors.blue),
),
),
Padding(
padding: EdgeInsets.only(top: 20),
),
Text(
"- OR -",
textAlign: TextAlign.center,
style:
TextStyle(fontSize: 20.0, color: Colors.blue),
),
Padding(
padding: EdgeInsets.only(top: 20),
),
GoogleSignInButton(
onPressed: () {
_signInWithGoogle()
.then((FirebaseUser user) => print(user))
.catchError((e) => print(e));
},
borderRadius: 20,
)
],
),
),
)
],
),
elevation: 20,
shape: BeveledRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(150),
)),
),
Padding(
padding: EdgeInsets.all(10),
),
Text(
'devloped by abhay1205',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 10, color: Colors.blue),
)
],
),
),
),
);
}
}