aboutsummaryrefslogtreecommitdiff
path: root/lib/screens/login.dart
blob: 1a0db55650191e38ebde44a98f9bdfa001045718 (plain)
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
import "package:flutter/material.dart";
import "package:flutter_secure_storage/flutter_secure_storage.dart";

import "package:openbills/models/login.dart";
import "package:openbills/widgets/input_box.dart";
import "package:openbills/widgets/snackbar.dart";
import "package:openbills/screens/signup.dart";

class LoginScreen extends StatelessWidget {
	const LoginScreen({super.key});

	@override
	Widget build(BuildContext context) {
		return Scaffold(
			appBar: AppBar(
				centerTitle: true,
				title: const Image(
					height: 50,
					image: AssetImage("assets/images/logo.png"),
				),
			),
			body: const Center(
					child: SizedBox(
						width: 500,
						child: Body(),
					)
			),
		);
	}
}

class Body extends StatefulWidget {
	const Body({super.key});

	@override
	State<Body> createState() => _BodyState();
}

class _BodyState extends State<Body> {
	final accountNameController = TextEditingController();
	final passwordController = TextEditingController();
	LoginMethod? _method = LoginMethod.username;

	final _storage = const FlutterSecureStorage();

	@override
	void dispose() {
		accountNameController.dispose();
		passwordController.dispose();
		super.dispose();
	}

	@override
	Widget build(BuildContext context) {
		Size size = MediaQuery.sizeOf(context);
		MainAxisAlignment alignment = MainAxisAlignment.center;

		if (size.width < 700) {
			alignment = MainAxisAlignment.spaceBetween;
		}

		if (size.height < 700) {
			alignment = MainAxisAlignment.center;
		}

		return Column (
			crossAxisAlignment: CrossAxisAlignment.center,
			mainAxisAlignment: alignment,
			children: <Widget> [
				const Padding(
					padding: EdgeInsets.symmetric(horizontal: 8, vertical: 16),
					child: Text(
						"OpenBills Log In",
						textAlign: TextAlign.center,
						style: TextStyle(
							fontWeight: FontWeight.bold,
							fontSize: 30,
						),
					),
				),

				Column(
					children: <Widget>[
						FormInputBox(
							controller: accountNameController,
							hintText: _method == LoginMethod.email ? "E-Mail" : "Username",
						),

						Padding(
							padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 16),
							child: Column(
								children: <Widget>[
									const Text(
										"Log in with:",
										textAlign: TextAlign.left,
										style: TextStyle(
											fontSize: 18,
										),
									),

									Row(
										children: <Widget>[
											Flexible(
												child: RadioListTile<LoginMethod>(
													title: const Text("username"),
													value: LoginMethod.username,
													groupValue: _method,
													onChanged: (LoginMethod? value) {
														setState(() {
															_method = value;
														});
													},
												),
											),

											Flexible(
												child: RadioListTile<LoginMethod>(
													title: const Text("email"),
													value: LoginMethod.email,
													groupValue: _method,
													onChanged: (LoginMethod? value) {
														setState(() {
															_method = value;
														});
													},
												),
											),
										],
									),
								],
							)
						),

						FormInputBox(
							controller: passwordController,
							hintText: "Password",
							obscureText: true,
						),
					],
				),

				Padding(
					padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 16),
					child: Row(
						mainAxisAlignment: MainAxisAlignment.spaceBetween,
						children: <Widget> [
							TextButton(
								onPressed: () {
									Navigator.push(
											context,
											MaterialPageRoute(builder: (context) => const SignupScreen())
									);
								},
								child: const Text("Don't have an account? Sign up.")
							),

							ElevatedButton(
								onPressed: () async {
									String name = accountNameController.text;
									String? method = _method?.value;
									String password = passwordController.text;

									try {
										LoginRes res = await userSignIn(name, method, password);
										String? username = res.user.username;

										await _storage.write(
											key: "auth_token",
											value: res.authToken,
											aOptions: const AndroidOptions(
												encryptedSharedPreferences: true,
											),
										);

										await _storage.write(
											key: "refresh_token",
											value: res.refreshToken,
											aOptions: const AndroidOptions(
												encryptedSharedPreferences: true,
											),
										);

										if (context.mounted) {
											MySnackBar.show(context, "Hello, $username! Please restart the app to see the homepage.");
										}
									} on String catch(e) {
										if (context.mounted) {
											MySnackBar.show(context, "Error: $e");
										}
									}
								},
								child: const Text("Log In")
							),
						],
					),
				),
			],
		);
	}
}