Started logic for auth scren

This commit is contained in:
2024-01-09 17:40:39 +03:00
parent 288518f12a
commit 432c780564

View File

@@ -7,6 +7,9 @@ import 'package:validators/validators.dart';
import 'package:eatvio_app/widgets/next_step_button.dart';
import 'package:eatvio_app/widgets/custom_text_form_field.dart';
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:hive/hive.dart';
import 'package:http/http.dart' as http;
class AuthScreen extends StatelessWidget {
static Widget builder(BuildContext context) {
@@ -70,15 +73,49 @@ class AuthScreen extends StatelessWidget {
margin:
EdgeInsets.only(left: 16.h, right: 16.h, bottom: 50.v),
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
onPressed: () {
onPressed: () async {
List<String> errors = validateFields(
state.emailController!.text,
state.passwordController!.text);
if (errors.isEmpty) {
Navigator.pushNamed(
context,
AppRoutes.myProfile,
final credentials = {
'email': state.emailController!.text,
'password': state.passwordController!.text,
};
final response = await http.post(
Uri.parse('http://10.55.5.126:1092/api/login'),
headers: {'Content-Type': 'application/json'},
body: json.encode(credentials),
);
if (response.statusCode == 200) {
print(response.body);
final box = await Hive.openBox('token');
final token = json.decode(response.body)['token']
['plainTextToken'];
box.put('token', token);
print('token: ${box.get('token')}');
Navigator.pushNamed(
context,
AppRoutes.myProfile,
);
} else {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Ошибка'),
content:
Text(jsonDecode(response.body)['message']),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('OK'),
),
],
),
);
}
} else {
String errorMessage = '';