Skip to content

Commit

Permalink
migrate login and registration to GetX
Browse files Browse the repository at this point in the history
  • Loading branch information
mytja committed Oct 30, 2023
1 parent 2971cad commit 9464675
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 160 deletions.
6 changes: 3 additions & 3 deletions tarok/lib/game.dart → tarok/lib/game/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import 'package:flutter/services.dart';
import 'package:flutter_initicon/flutter_initicon.dart';
import 'package:get/get.dart';
import 'package:tarok/constants.dart';
import 'package:tarok/friends.dart';
import 'package:tarok/game_controller.dart';
import 'package:tarok/lobby/friends.dart';
import 'package:tarok/game/game_controller.dart';
import 'package:stockskis/stockskis.dart' as stockskis;

import 'stockskis_compatibility/compatibility.dart';
import '../stockskis_compatibility/compatibility.dart';

class Game extends StatelessWidget {
const Game({super.key});
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions tarok/lib/friends.dart → tarok/lib/lobby/friends.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import 'package:flutter/material.dart';
import 'package:flutter_initicon/flutter_initicon.dart';
import 'package:get/get.dart' hide FormData;
import 'package:tarok/constants.dart';
import 'package:tarok/game_controller.dart';
import 'package:tarok/lobby_controller.dart';
import 'package:tarok/game/game_controller.dart';
import 'package:tarok/lobby/lobby_controller.dart';
import 'package:tarok/ui/main_page.dart';

class Friends extends StatelessWidget {
Expand Down
2 changes: 1 addition & 1 deletion tarok/lib/lobby.dart → tarok/lib/lobby/lobby.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
import 'package:get/get.dart' hide FormData;
import 'package:stockskis/stockskis.dart' hide Card, debugPrint;
import 'package:tarok/constants.dart';
import 'package:tarok/lobby_controller.dart';
import 'package:tarok/lobby/lobby_controller.dart';
import 'package:tarok/replay.dart';
import 'package:tarok/ui/main_page.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,6 @@ class LobbyController extends GetxController {
});
}

void rerenderLogin() {
storage.read(key: "token").then((value) {
if (value == null) {
Get.toNamed("/login");
}
});
}

Future<List> getRegistrationCodes() async {
final response = await dio.get(
"$BACKEND_URL/admin/reg_code",
Expand Down Expand Up @@ -403,7 +395,6 @@ class LobbyController extends GetxController {
isAdmin.value = value == "admin";
if (!isAdmin.value) return;
});
rerenderLogin();

// ONLINE
try {
Expand Down
2 changes: 1 addition & 1 deletion tarok/lib/replays.dart → tarok/lib/lobby/replays.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart' hide FormData;
import 'package:tarok/lobby_controller.dart';
import 'package:tarok/lobby/lobby_controller.dart';
import 'package:tarok/replay.dart';
import 'package:tarok/ui/main_page.dart';

Expand Down
72 changes: 7 additions & 65 deletions tarok/lib/login.dart → tarok/lib/login/login.dart
Original file line number Diff line number Diff line change
@@ -1,64 +1,18 @@
import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:get/get.dart' hide FormData;
import 'package:tarok/constants.dart';
import 'package:tarok/login/login_controller.dart';
import 'package:url_launcher/url_launcher.dart';

class MyColorMapper implements ColorMapper {
const MyColorMapper({
required this.baseColor,
this.accentColor,
});

static const _rawBaseColor = Color(0xFF293540);
static const _rawAccentColor = Color(0xFFFFFFFF);

final Color baseColor;
final Color? accentColor;

@override
Color substitute(
String? id, String elementName, String attributeName, Color color) {
if (color == _rawBaseColor) return baseColor;

final accentColor = this.accentColor;
if (accentColor != null && color == _rawAccentColor) return accentColor;

return color;
}
}

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

@override
State<Login> createState() => _LoginState();
}

class _LoginState extends State<Login> {
late TextEditingController _username;
late TextEditingController _password;

@override
void initState() {
super.initState();
_username = TextEditingController();
_password = TextEditingController();
}

@override
void dispose() {
_username.dispose();
_password.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
LoginController controller = Get.put(LoginController());

return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
Expand All @@ -79,7 +33,7 @@ class _LoginState extends State<Login> {
SizedBox(
width: 350,
child: TextField(
controller: _username,
controller: controller.email.value,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Elektronski naslov',
Expand All @@ -92,7 +46,7 @@ class _LoginState extends State<Login> {
SizedBox(
width: 350,
child: TextField(
controller: _password,
controller: controller.password1.value,
obscureText: true,
decoration: const InputDecoration(
border: OutlineInputBorder(),
Expand All @@ -104,19 +58,7 @@ class _LoginState extends State<Login> {
height: 20,
),
ElevatedButton(
onPressed: () async {
final response = await dio.post(
"$BACKEND_URL/login",
data: FormData.fromMap(
{"email": _username.text, "pass": _password.text},
),
);
if (response.statusCode != 200) return;
final data = jsonDecode(response.data);
await storage.write(key: "token", value: data["token"]);
await storage.write(key: "role", value: data["role"]);
Get.toNamed("/");
},
onPressed: controller.login,
child: const Text("Prijava", style: TextStyle(fontSize: 20)),
),
const SizedBox(
Expand Down
68 changes: 68 additions & 0 deletions tarok/lib/login/login_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart' hide FormData;
import 'package:tarok/constants.dart';

class LoginController extends GetxController {
var email = TextEditingController().obs;
var name = TextEditingController().obs;
var password1 = TextEditingController().obs;
var password2 = TextEditingController().obs;
var emailController = TextEditingController().obs;

Future<void> login() async {
final response = await dio.post(
"$BACKEND_URL/login",
data: FormData.fromMap(
{"email": email.value.text, "pass": password1.value.text},
),
);
if (response.statusCode != 200) return;
final data = jsonDecode(response.data);
await storage.write(key: "token", value: data["token"]);
await storage.write(key: "role", value: data["role"]);
Get.toNamed("/");
}

Future<void> register() async {
if (password1.value.text != password2.value.text) {
Get.dialog(
AlertDialog(
title: const Text('Gesli se ne ujemata'),
content: const SizedBox(),
actions: <Widget>[
TextButton(
onPressed: () => Get.back(),
child: const Text('OK'),
),
],
),
);
return;
}
final response = await dio.post(
"$BACKEND_URL/register",
data: FormData.fromMap(
{
"email": email.value.text,
"pass": password1.value.text,
"name": name.value.text,
"regCode": "",
},
),
);
if (response.statusCode != 201) return;
Get.toNamed("/login");
}

@override
void onClose() {
email.value.dispose();
name.value.dispose();
password1.value.dispose();
password2.value.dispose();
super.onClose();
}
}
82 changes: 11 additions & 71 deletions tarok/lib/register.dart → tarok/lib/login/register.dart
Original file line number Diff line number Diff line change
@@ -1,43 +1,14 @@
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:tarok/constants.dart';
import 'package:get/get.dart';
import 'package:tarok/login/login_controller.dart';

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

@override
State<Register> createState() => _RegisterState();
}

class _RegisterState extends State<Register> {
late TextEditingController _email;
late TextEditingController _username;
late TextEditingController _password;
late TextEditingController _password2;
late TextEditingController _regCode;

@override
void initState() {
super.initState();
_email = TextEditingController();
_username = TextEditingController();
_password = TextEditingController();
_password2 = TextEditingController();
_regCode = TextEditingController();
}

@override
void dispose() {
_email.dispose();
_username.dispose();
_password.dispose();
_password2.dispose();
_regCode.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
LoginController controller = Get.put(LoginController());

return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
Expand All @@ -58,7 +29,7 @@ class _RegisterState extends State<Register> {
SizedBox(
width: 350,
child: TextField(
controller: _email,
controller: controller.email.value,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Elektronski naslov',
Expand All @@ -71,7 +42,7 @@ class _RegisterState extends State<Register> {
SizedBox(
width: 350,
child: TextField(
controller: _username,
controller: controller.name.value,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Ime profila',
Expand All @@ -84,7 +55,7 @@ class _RegisterState extends State<Register> {
SizedBox(
width: 350,
child: TextField(
controller: _password2,
controller: controller.password1.value,
obscureText: true,
decoration: const InputDecoration(
border: OutlineInputBorder(),
Expand All @@ -98,7 +69,7 @@ class _RegisterState extends State<Register> {
SizedBox(
width: 350,
child: TextField(
controller: _password,
controller: controller.password2.value,
obscureText: true,
decoration: const InputDecoration(
border: OutlineInputBorder(),
Expand All @@ -123,46 +94,15 @@ class _RegisterState extends State<Register> {
height: 20,
),
ElevatedButton(
onPressed: () async {
if (_password.text != _password2.text) {
showDialog<String>(
context: context,
builder: (BuildContext context) => AlertDialog(
title: const Text('Gesli se ne ujemata'),
content: const SizedBox(),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('OK'),
),
],
),
);
return;
}
final response = await dio.post(
"$BACKEND_URL/register",
data: FormData.fromMap(
{
"email": _email.text,
"pass": _password.text,
"name": _username.text,
"regCode": _regCode.text
},
),
);
if (response.statusCode != 201) return;
// ignore: use_build_context_synchronously
Navigator.pop(context);
},
onPressed: controller.register,
child: const Text("Registracija", style: TextStyle(fontSize: 20)),
),
const SizedBox(
height: 20,
),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
Get.toNamed("/login");
},
child: const Text("Prijava", style: TextStyle(fontSize: 20)),
),
Expand Down
Loading

0 comments on commit 9464675

Please sign in to comment.