Skip to content

Commit

Permalink
usuario
Browse files Browse the repository at this point in the history
  • Loading branch information
wjrcode committed Oct 17, 2022
1 parent d6e3250 commit e915485
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 52 deletions.
21 changes: 10 additions & 11 deletions lib/Controller/LoginController.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -69,12 +70,20 @@ class LoginController extends Base {
}
}

Future<UsuarioModel> 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;
Expand All @@ -92,20 +101,10 @@ class LoginController extends Base {

prefs.setString('usuario', '');
prefs.setString('senha', '');
prefs.setString('apelido', '');
prefs.setString('UsuarioApelido', '');
prefs.setString('token', '');
}

Future<String> getUsuarioLogado() async {
SharedPreferences prefs = await SharedPreferences.getInstance();

LoginModel? login = await getLogin();

String usuario = login!.usuario.toString();

return '$usuario';
}

Future<String> getSenhaUsuarioLogado() async {
SharedPreferences prefs = await SharedPreferences.getInstance();

Expand Down
17 changes: 14 additions & 3 deletions lib/Model/API/APIModel.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import 'package:shared_preferences/shared_preferences.dart';

Future<String> 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<String, String> headers = {

static final Future<String> _token = getToken();

static Map<String, String> headers = {
"Content-Type": "application/json",
"Authorization": "Bearer $_token"
"Authorization": "Bearer $_token"
};
}
24 changes: 14 additions & 10 deletions lib/Repository/LoginRepository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<bool> validarDispositivo(String id) async {
var _url = Uri.parse(ApiModel.ApiUrl + '/dispositivos/' + id + '/validar');
Future<UsuarioModel> 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;
}
}
2 changes: 0 additions & 2 deletions lib/components/dialogs/login/entrarDialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ Future<dynamic> entrar(BuildContext context) {
clicar: () async {
bool succes =
await loginController.autentica();
SharedPreferences prefs =
await SharedPreferences.getInstance();

if (succes) {
Navigator.of(context).push<void>(
Expand Down
12 changes: 5 additions & 7 deletions lib/components/dialogs/login/sairDialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import 'package:organizei/components/dialogs/tarefas/tarefaCadastroDialog.dart';
import 'package:organizei/start_page.dart';
import '../../../Controller/TarefaController.dart';

Future<dynamic> sair(BuildContext context, {Function? fecharDialog}) {
Future<dynamic> 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),
Expand Down Expand Up @@ -53,18 +56,13 @@ Future<dynamic> 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!();
// }
},
),
),
Expand Down
8 changes: 7 additions & 1 deletion lib/components/dialogs/login/usuarioCadastroDialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Future<dynamic> 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),
Expand Down Expand Up @@ -70,7 +76,7 @@ Future<dynamic> cadastrarUsuario(BuildContext context,
//child: Input(label: 'senha'),
),
Botao(
texto: 'Cadastrar',
texto: 'salvar',
cor: const Color(0xFF6BC8E4),
clicar: () async {
bool succes = await usuarioController.saveUsuario();
Expand Down
6 changes: 1 addition & 5 deletions lib/services/persistencia/login.configuracoes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> lerArquivo() async {
Expand Down
26 changes: 13 additions & 13 deletions lib/start_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ class _StartPageState extends State<StartPage> {
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) {
Expand Down

0 comments on commit e915485

Please sign in to comment.