diff --git a/lib/Controller/LoginController.dart b/lib/Controller/LoginController.dart index 3f619cd..8c61597 100644 --- a/lib/Controller/LoginController.dart +++ b/lib/Controller/LoginController.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:http/retry.dart'; +import 'package:organizei/Model/Usuario/UsuarioModel.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:organizei/Controller/Base/Base.dart'; import 'package:organizei/Controller/DeviceDetailsController.dart'; @@ -69,12 +70,20 @@ class LoginController extends Base { } } + Future getUsuarioLogado() async { + return repository.getUsuarioLogado(); + } + salvarLogin(LoginModel login) async { //LoginModel? login = await loginConfiguracoes.getLogin(); await loginConfiguracoes.salvarArquivo(login); } + sair() async { + await loginConfiguracoes.deleteLogin(); + } + bool existeLogin() { loginConfiguracoes.existeArquivo().then((value) { return value; @@ -92,20 +101,10 @@ class LoginController extends Base { prefs.setString('usuario', ''); prefs.setString('senha', ''); - prefs.setString('apelido', ''); + prefs.setString('UsuarioApelido', ''); prefs.setString('token', ''); } - Future getUsuarioLogado() async { - SharedPreferences prefs = await SharedPreferences.getInstance(); - - LoginModel? login = await getLogin(); - - String usuario = login!.usuario.toString(); - - return '$usuario'; - } - Future getSenhaUsuarioLogado() async { SharedPreferences prefs = await SharedPreferences.getInstance(); diff --git a/lib/Model/API/APIModel.dart b/lib/Model/API/APIModel.dart index 7e2fc06..38d0ce3 100644 --- a/lib/Model/API/APIModel.dart +++ b/lib/Model/API/APIModel.dart @@ -1,10 +1,21 @@ +import 'package:shared_preferences/shared_preferences.dart'; + +Future getToken() async { + SharedPreferences prefs = await SharedPreferences.getInstance(); + + String? token = prefs.getString('UsuarioToken'); + return token ?? ''; +} + class ApiModel { static const ApiUrl = "http://10.0.0.91:4444"; //static const ApiUrl = "http://192.168.100.74:4444"; //static const ApiUrl = "http://192.168.100.82:4444"; - static const String _token = "fd7bc88b7c0149adbc134d5d0a919814"; - static const Map headers = { + + static final Future _token = getToken(); + + static Map headers = { "Content-Type": "application/json", - "Authorization": "Bearer $_token" + "Authorization": "Bearer $_token" }; } diff --git a/lib/Repository/LoginRepository.dart b/lib/Repository/LoginRepository.dart index f7ce278..0291e61 100644 --- a/lib/Repository/LoginRepository.dart +++ b/lib/Repository/LoginRepository.dart @@ -2,8 +2,8 @@ import 'dart:convert'; import 'package:organizei/Model/API/APIModel.dart'; import 'package:http/http.dart' as http; -import 'package:organizei/Model/API/ResponseAPIModel.dart'; import 'package:organizei/Model/Login/LoginModel.dart'; +import 'package:organizei/Model/Usuario/UsuarioModel.dart'; import 'package:shared_preferences/shared_preferences.dart'; class LoginRepository { @@ -23,24 +23,28 @@ class LoginRepository { if (response.statusCode == 200) { SharedPreferences prefs = await SharedPreferences.getInstance(); - final dadosLogin = ResponseAPIModel.fromJson(jsonDecode(response.body)); + final dadosLogin = jsonDecode(response.body); - prefs.setString('UsuarioApelido', dadosLogin.msg.toString()); + prefs.setString('UsuarioApelido', dadosLogin['apelido'].toString()); + prefs.setString('UsuarioToken', dadosLogin['token'].toString()); return true; } return false; } - Future validarDispositivo(String id) async { - var _url = Uri.parse(ApiModel.ApiUrl + '/dispositivos/' + id + '/validar'); + Future getUsuarioLogado() async { + SharedPreferences prefs = await SharedPreferences.getInstance(); - final response = await http.post(_url, headers: ApiModel.headers); + String? token = prefs.getString('UsuarioToken'); + print(token); - if (response.statusCode == 200) { - return true; - } + var _url = Uri.parse(ApiModel.ApiUrl + '/usuario/' + token!); - return false; + final response = await http.get(_url, headers: ApiModel.headers); + + UsuarioModel usuario = UsuarioModel.fromJson(jsonDecode(response.body)); + + return usuario; } } diff --git a/lib/components/dialogs/login/entrarDialog.dart b/lib/components/dialogs/login/entrarDialog.dart index b47e5b0..b9f559d 100644 --- a/lib/components/dialogs/login/entrarDialog.dart +++ b/lib/components/dialogs/login/entrarDialog.dart @@ -63,8 +63,6 @@ Future entrar(BuildContext context) { clicar: () async { bool succes = await loginController.autentica(); - SharedPreferences prefs = - await SharedPreferences.getInstance(); if (succes) { Navigator.of(context).push( diff --git a/lib/components/dialogs/login/sairDialog.dart b/lib/components/dialogs/login/sairDialog.dart index f451868..7f7ed0a 100644 --- a/lib/components/dialogs/login/sairDialog.dart +++ b/lib/components/dialogs/login/sairDialog.dart @@ -11,12 +11,15 @@ import 'package:organizei/components/dialogs/tarefas/tarefaCadastroDialog.dart'; import 'package:organizei/start_page.dart'; import '../../../Controller/TarefaController.dart'; -Future sair(BuildContext context, {Function? fecharDialog}) { +Future sair(BuildContext context, {Function? fecharDialog}) async { UsuarioModel? usuario; late LoginController loginController; loginController = LoginController(LoginRepository(), context); + + usuario = await loginController.getUsuarioLogado(); + return showDialog( barrierDismissible: false, barrierColor: Colors.white.withOpacity(0), @@ -53,18 +56,13 @@ Future sair(BuildContext context, {Function? fecharDialog}) { texto: 'Sair', cor: const Color(0xFFEF7E69), clicar: () async { - await loginController.limpaPreferencesLogin(); + await loginController.sair(); Navigator.pushAndRemoveUntil( context, MaterialPageRoute( builder: (_) => const StartPage()), (route) => false); - - // if (succes == true) { - // Navigator.pop(context); - // fecharDialog!(); - // } }, ), ), diff --git a/lib/components/dialogs/login/usuarioCadastroDialog.dart b/lib/components/dialogs/login/usuarioCadastroDialog.dart index d3ebf6f..c3c0959 100644 --- a/lib/components/dialogs/login/usuarioCadastroDialog.dart +++ b/lib/components/dialogs/login/usuarioCadastroDialog.dart @@ -12,6 +12,12 @@ Future cadastrarUsuario(BuildContext context, late UsuarioController usuarioController; usuarioController = UsuarioController(UsuarioRepository(), context); + if (usuario != null) { + usuarioController.controllerNome.text = usuario.nome ?? ''; + usuarioController.controllerApelido.text = usuario.nome ?? ''; + usuarioController.controllerSenha.text = usuario.apelido ?? ''; + } + return showDialog( barrierDismissible: false, barrierColor: Colors.white.withOpacity(0), @@ -70,7 +76,7 @@ Future cadastrarUsuario(BuildContext context, //child: Input(label: 'senha'), ), Botao( - texto: 'Cadastrar', + texto: 'salvar', cor: const Color(0xFF6BC8E4), clicar: () async { bool succes = await usuarioController.saveUsuario(); diff --git a/lib/services/persistencia/login.configuracoes.dart b/lib/services/persistencia/login.configuracoes.dart index 5dc7361..3528a51 100644 --- a/lib/services/persistencia/login.configuracoes.dart +++ b/lib/services/persistencia/login.configuracoes.dart @@ -23,12 +23,8 @@ class LoginConfiguracoes { return ret; } - deleteLogin(LoginModel login) async { - var logins = await getLogin(); - //logins.removeWhere((e) => e.chaveCentralizador == login.chaveCentralizador); - + deleteLogin() async { excluirArquivo(); - // salvarArquivo(logins); } Future lerArquivo() async { diff --git a/lib/start_page.dart b/lib/start_page.dart index 95f469b..405c3d6 100644 --- a/lib/start_page.dart +++ b/lib/start_page.dart @@ -27,25 +27,25 @@ class _StartPageState extends State { initState() { controller = LoginController(LoginRepository(), context); usuarioController = UsuarioController(UsuarioRepository(), context); - _logar(); + // _logar(); super.initState(); } - _logar() async { - await controller.getLogin().then((value) async { - if (value != null) { - String teste = await controller.getUsuarioLogado(); - String senha = await controller.getSenhaUsuarioLogado(); + // _logar() async { + // await controller.getLogin().then((value) async { + // if (value != null) { + // String teste = await controller.getUsuarioLogado(); + // String senha = await controller.getSenhaUsuarioLogado(); - SharedPreferences prefs = await SharedPreferences.getInstance(); + // SharedPreferences prefs = await SharedPreferences.getInstance(); - apelido = prefs.getString('UsuarioApelido'); + // apelido = prefs.getString('UsuarioApelido'); - controller.controllerUsuario.text = teste.toString(); - controller.controllerSenha.text = senha.toString(); - } - }); - } + // controller.controllerUsuario.text = teste.toString(); + // controller.controllerSenha.text = senha.toString(); + // } + // }); + // } @override Widget build(BuildContext context) {