-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate login and registration to GetX
- Loading branch information
Showing
10 changed files
with
106 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.