-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.dart
237 lines (225 loc) · 7.27 KB
/
auth.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
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'styles.dart';
import 'main.dart';
import 'experience.dart';
import 'package:shared_preferences/shared_preferences.dart';
// Login widget
class Login extends StatefulWidget {
Login({Key key}) : super(key: key);
@override
_LoginState createState() => _LoginState();
}
class _LoginState extends State<Login> {
String mail;
String password;
String error = "";
String storedMail;
bool saveData = false;
// get mail if one was saved in the shared preferences
getStoredMail() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String _res = prefs.getString("email");
setState(() {
storedMail = _res;
storedMail != null ? mail = storedMail : mail = mail;
});
}
@override
Widget build(BuildContext context) {
getStoredMail();
return Scaffold(
appBar: AppBar(title: const Text('Login')),
body: ListView(
padding: const EdgeInsets.symmetric(vertical: 18, horizontal: 12),
children: [
const SizedBox(height: 18),
TextFormField(
decoration: const InputDecoration(labelText: 'Email'),
initialValue: storedMail,
key: Key("mailinput"),
onChanged: (String value) {
mail = value;
},
),
TextFormField(
obscureText: true,
enableSuggestions: false,
autocorrect: false,
key: Key("pwinput"),
decoration: const InputDecoration(labelText: 'Password'),
onChanged: (String value) {
password = value;
},
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Checkbox(
checkColor: Colors.white,
value: saveData,
onChanged: (bool value) {
setState(() {
saveData = value;
});
},
),
Text("Save email for later"),
]),
),
errorWidget(this.error),
const SizedBox(height: 18),
ElevatedButton(
key: Key("loginbtn"),
child: Text('Login'),
onPressed: () {
if (mail != null &&
mail.isNotEmpty &&
password != null &&
password.isNotEmpty &&
// check if mail is valid
RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+")
.hasMatch(mail)) {
supabase.auth
.signIn(email: mail, password: password)
.then((response) {
if ((response.error) != null) {
setState(() {
error = (response.error.message);
});
} else {
if (saveData) {
setStoredMail(mail);
}
// return the user to the experiences tab
Navigator.pop(
context,
MaterialPageRoute(
builder: (context) => experience()));
}
});
} else {
error = (mail == null || mail.isEmpty)
? "Please enter a mail adress"
: (password == null || password.isEmpty)
? "Please enter a password"
: "Please enter valid credentials";
}
}),
],
),
);
}
}
// Signup widget
class Signup extends StatefulWidget {
Signup({Key key}) : super(key: key);
@override
_SignupState createState() => _SignupState();
}
class _SignupState extends State<Signup> {
String mail;
String password;
String error;
bool saveData = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Sign Up')),
body: ListView(
padding: const EdgeInsets.symmetric(vertical: 18, horizontal: 12),
children: [
const SizedBox(height: 18),
TextFormField(
decoration: const InputDecoration(labelText: 'Email'),
onChanged: (String value) {
mail = value;
},
),
TextFormField(
obscureText: true,
enableSuggestions: false,
autocorrect: false,
decoration: const InputDecoration(labelText: 'Password'),
onChanged: (String value) {
password = value;
},
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Checkbox(
checkColor: Colors.white,
value: saveData,
onChanged: (bool value) {
setState(() {
saveData = value;
});
},
),
Text("Save email for later"),
]),
),
errorWidget(this.error),
const SizedBox(height: 18),
ElevatedButton(
child: Text('Sign Up'),
onPressed: () {
if (mail != null &&
mail.isNotEmpty &&
password != null &&
password.isNotEmpty) {
supabase.auth.signUp(mail, password).then((response) {
if ((response.error) != null) {
setState(() {
error = (response.error.message);
});
} else {
if (saveData) {
setStoredMail(mail);
}
Navigator.pop(
context,
MaterialPageRoute(
builder: (context) => experience()));
}
});
} else {
error = (mail == null || mail.isEmpty)
? "Please enter a mail adress"
: (password == null || password.isEmpty)
? "Please enter a password"
: "Please enter valid credentials";
}
}),
],
),
);
}
}
Widget errorWidget(String error) {
if (error != null && error != "") {
return (Center(
child: Text(
error,
style: errorstyle,
),
));
} else {
return Container();
}
}
String getUserId() {
return supabase.auth.currentUser.id;
}
// save the mail provided into the preferences
void setStoredMail(String mail) async {
final prefs = await SharedPreferences.getInstance();
prefs.setString("email", mail);
}