Skip to content

Commit

Permalink
feat(#58) adiciona serviço de login com email e senha
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielCostaDeOliveira committed Dec 9, 2024
1 parent 50e970b commit c898afd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 37 deletions.
3 changes: 2 additions & 1 deletion lib/core/network/AppInterceptors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class AppInterceptors extends Interceptor {
}

handler.next(options);



} catch (e) {
Log.e(e);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/core/network/BaseApi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class BaseApi {

BaseApi._internal()
: _dio = Dio() {
_dio.options.baseUrl = 'http://192.168.1.107:3000';
_dio.options.baseUrl = 'https://arandu-user-service.onrender.com/';
_dio.options.connectTimeout = const Duration(seconds: 5);
_dio.options.receiveTimeout = const Duration(seconds: 5);

Expand Down
8 changes: 4 additions & 4 deletions lib/ui/login/model/LoginResponse.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import 'dart:convert';

class LoginResponse {

String authToken;
String refreshToken;
String? authToken;
String? refreshToken;

LoginResponse(this.authToken, this.refreshToken);


factory LoginResponse.fromJsonString(String jsonString) {
Map<String, dynamic> json = jsonDecode(jsonString);
return LoginResponse(
json['auth_token'] as String,
json['refresh_token'] as String
json['accessToken'] as String?,
json['refreshToken'] as String?
);
}

Expand Down
13 changes: 9 additions & 4 deletions lib/ui/login/service/LoginService.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:aranduapp/core/data/local/StorageValue.dart';
import 'package:aranduapp/core/log/Log.dart';
import 'package:aranduapp/core/network/BaseApi.dart';
import 'package:aranduapp/ui/login/model/LoginRequest.dart';
import 'package:aranduapp/ui/login/model/LoginResponse.dart';
Expand All @@ -8,18 +9,22 @@ class LoginService {

static Future<Response> login(LoginRequest loginRequest) async {

Log.d('${loginRequest.email} ${loginRequest.password}');

Response response = await BaseApi.getInstance().post(
path: '/asdfsdfsdf',
path: '/auth/login',
data: <String, dynamic> {
'email' : loginRequest.email,
'passwoed': loginRequest.password
'password': loginRequest.password
}
);

LoginResponse loginResponse = LoginResponse.fromJsonString(response.toString());

await StorageValue.getInstance().setAuthToken(loginResponse.authToken);
await StorageValue.getInstance().setAuthToken(loginResponse.authToken);
assert(loginResponse.authToken != null && loginResponse.refreshToken != null);

await StorageValue.getInstance().setAuthToken(loginResponse.authToken ?? '');
await StorageValue.getInstance().setAuthToken(loginResponse.refreshToken??'');


return response;
Expand Down
36 changes: 21 additions & 15 deletions lib/ui/login/view/LoginView.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:aranduapp/core/log/Log.dart';
import 'package:aranduapp/ui/home/view/HomeView.dart';
import 'package:aranduapp/ui/shared/PhraseLink.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -126,7 +127,10 @@ class _LoginState extends State<_Login> {
_loginButtonSection(context, viewModel),
const OrDivider(),
_loggingInWithOther(),
const TextAndLink(text: 'É novo pro aqui?', link: 'Cria a sua conta', page: RegisterAccount()),
const TextAndLink(
text: 'É novo pro aqui?',
link: 'Cria a sua conta',
page: RegisterAccount()),
],
)),
],
Expand Down Expand Up @@ -184,13 +188,19 @@ class _LoginState extends State<_Login> {
height: 64,
child: ElevatedButton(
onPressed: () {
viewModel
.loginWithEmailAndPassword()
.catchError((e) => showDialog<Object>(
context: context,
builder: (BuildContext context) =>
ErrorPopUp(content: Text('$e')),
));
viewModel.loginWithEmailAndPassword()
.then((_) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => const HomeView(),
),
);
})
.catchError((e) => showDialog<Object>(
context: context,
builder: (BuildContext context) =>
ErrorPopUp(content: Text('$e')),
));
},
child: Consumer<LoginViewModel>(
builder: (context, value, child) => value.isLoading
Expand All @@ -200,25 +210,21 @@ class _LoginState extends State<_Login> {
);
}




Widget _loggingInWithOther(){

Widget _loggingInWithOther() {
return GestureDetector(
onTap: () => Log.d(""),
child: Container(
width: 50,
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Theme.of(context).colorScheme.outline),
color: Colors.transparent,
),
child: Icon(
FontAwesomeIcons.google,
size: 20,
color: Theme.of(context).colorScheme.primary,
color: Theme.of(context).colorScheme.primary,
),
),
);
Expand Down
12 changes: 0 additions & 12 deletions lib/ui/login/viewModel/LoginViewModel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,8 @@ class LoginViewModel extends ChangeNotifier {
throw Exception('Valores inválidos');
}


await LoginService.login(LoginRequest(emailController.text, passwordController.text));

_moveToHome();

} catch (e) {
rethrow;
} finally {
Expand All @@ -65,18 +62,9 @@ class LoginViewModel extends ChangeNotifier {
if (!value)
throw Exception();

_moveToHome();
}


void _moveToHome(){

Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => const HomeView(),
),
);
}

}

0 comments on commit c898afd

Please sign in to comment.