-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAuthenticationPage.dart
More file actions
222 lines (207 loc) · 6.82 KB
/
Copy pathAuthenticationPage.dart
File metadata and controls
222 lines (207 loc) · 6.82 KB
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
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
class AuthenticationPage extends StatefulWidget {
AuthenticationPage({Key key}) : super(key: key);
@override
AuthenticationPageState createState() {
return new AuthenticationPageState();
}
}
class AuthenticationPageState extends State<AuthenticationPage> {
AuthenticationPageState(): super() {
this.monitorAuthenticationState();
}
// Authentication
FirebaseAuth auth = FirebaseAuth.instance;
String errorMessage = "";
String email;
String password;
User user;
monitorAuthenticationState() {
auth.authStateChanges().listen((User user) {
if (user != null) {
print("Authentication: User logged in");
} else {
print("Authentication: User logged out");
}
setState(() {
this.user = user;
});
});
}
registerAccountUsingEmail(email, password) async {
try {
UserCredential userCredential = await auth.createUserWithEmailAndPassword(
email: email,
password: password
);
} on FirebaseAuthException catch (err) {
if (err.code == "weak-password") {
setState(() {
errorMessage = "Please enter a stronger password";
});
} else if (err.code == "email-already-in-use") {
setState(() {
errorMessage = "This email has already been registered to another account";
});
}
} catch (err) { // other errors
print(err);
}
}
loginUsingEmail(email, password) async {
try {
UserCredential userCredential = await auth.signInWithEmailAndPassword(
email: email,
password: password
);
} on FirebaseAuthException catch (err) {
if (err.code == "user-not-found") {
setState(() {
errorMessage = "There is no account connected to this email address";
});
} else if (err.code == "wrong-password") {
setState(() {
errorMessage = "Incorrect password";
});
}
} catch (err) { // other errors
print(err);
}
}
logOut() async {
await auth.signOut();
}
// Form
final formKey = GlobalKey<FormState>();
String emailAddressValidator(String email) {
if (!email.contains("@")) {
return "Please enter a valid email";
}
return null;
}
String passwordValidator(String password) {
if (password.length < 5) {
return "Password must be at least 5 characters";
} else if (password.length > 25) {
return "Password must be at most 25 characters";
}
return null;
}
onRegisterFormSubmitted() {
if (formKey.currentState.validate()) {
formKey.currentState.save();
this.registerAccountUsingEmail(email, password);
}
}
onLoginFormSubmitted() {
if (formKey.currentState.validate()) {
formKey.currentState.save();
this.loginUsingEmail(email, password);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Authentication"),
backgroundColor: Colors.orange,
),
body: Container(
child: Form(
key: formKey,
child:
user == null ? Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(vertical: 4.0, horizontal: 32.0),
child: TextFormField(
decoration: const InputDecoration(
icon: Icon(Icons.email),
labelText: "Email"
),
validator: this.emailAddressValidator,
onSaved: (value) {
email = value;
}
)
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 4.0, horizontal: 32.0),
child: TextFormField(
obscureText: true,
decoration: const InputDecoration(
icon: Icon(Icons.lock),
labelText: "Password"
),
validator: this.passwordValidator,
onSaved: (value) {
password = value;
},
)
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 4.0, horizontal: 32.0),
child: Center(
child: Text(
"$errorMessage",
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.red
)
)
)
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 4.0, horizontal: 32.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
onPressed: onRegisterFormSubmitted,
child: Text('Register'),
),
ElevatedButton(
onPressed: onLoginFormSubmitted,
child: Text('Login'),
),
]
)
)
]
)
: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(vertical: 4.0, horizontal: 32.0),
child: Center(
child: Text(
"Welcome, ${user.email}",
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.green,
fontSize: 36
)
)
)
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 4.0, horizontal: 32.0),
child: Center(
child: ElevatedButton(
onPressed: logOut,
child: Text('Logout'),
)
)
),
],
)
)
)
);
}
}