From 432c780564e1504d88fccdd77fe74208a309a976 Mon Sep 17 00:00:00 2001 From: maxxkim Date: Tue, 9 Jan 2024 17:40:39 +0300 Subject: [PATCH] Started logic for auth scren --- .../auth/auth_screen/auth_screen.dart | 45 +++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/lib/presentation/auth/auth_screen/auth_screen.dart b/lib/presentation/auth/auth_screen/auth_screen.dart index aafd1c79..d1159b4a 100644 --- a/lib/presentation/auth/auth_screen/auth_screen.dart +++ b/lib/presentation/auth/auth_screen/auth_screen.dart @@ -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 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 = '';