Skip to content

Commit

Permalink
feat: Remove AuthModel to only use OAuthModel (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-martinez authored Oct 17, 2023
1 parent 0bfdc61 commit c3f4271
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 98 deletions.
83 changes: 0 additions & 83 deletions lib/models/auth_model.dart

This file was deleted.

7 changes: 2 additions & 5 deletions lib/navigator/guard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:async';

import 'package:client_common/api/response_models/app_response.dart';
import 'package:client_common/api/response_models/user.dart';
import 'package:client_common/models/auth_model.dart';
import 'package:client_common/models/user_application_model.dart';
import 'package:client_common/navigator/common_navigator.dart';
import 'package:client_common/oauth/oauth_model.dart';
Expand All @@ -25,13 +24,11 @@ class Guard {
static final Guard checkNotHaveApp = Guard(isValid: haveApp(false), onInvalid: toHome);

static Future<bool> isDev(BuildContext context) async {
AuthModel authModel = context.read<AuthModel>();
return authModel.isOneOfRole(_devOrMore);
return context.read<OAuthModel>().isOneOfRole(_devOrMore);
}

static Future<bool> isNotDev(BuildContext context) async {
AuthModel authModel = context.read<AuthModel>();
return authModel.isOneOfRole(UserRole.values.where((ur) => !_devOrMore.contains(ur)).toList());
return context.read<OAuthModel>().isOneOfRole(UserRole.values.where((ur) => !_devOrMore.contains(ur)).toList());
}

static IsValid haveApp(bool mustHaveApp) {
Expand Down
70 changes: 64 additions & 6 deletions lib/oauth/oauth_model.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import 'dart:async';

import 'package:client_common/api/request_models/ask_code_lost_password_request.dart';
import 'package:client_common/api/request_models/change_password_request.dart';
import 'package:client_common/api/request_models/send_code_lost_password_request.dart';
import 'package:client_common/api/request_models/validate_dev_request.dart';
import 'package:client_common/api/response_models/empty_response.dart';
import 'package:client_common/api/response_models/user.dart';
import 'package:client_common/api/response_models/user_response.dart';
import 'package:client_common/api/user_api.dart';
import 'package:client_common/models/status.dart';
import 'package:client_common/navigator/common_navigator.dart';
import 'package:client_common/oauth/oauth.dart';
import 'package:client_common/utils/connexion_utils_stub.dart'
if (dart.library.io) 'package:client_common/utils/connexion_utils_io.dart'
Expand All @@ -9,14 +19,29 @@ import 'package:oauth2_client/access_token_response.dart';
import 'package:oauth2_client/oauth2_helper.dart';

class OAuthModel extends ChangeNotifier {
AccessTokenResponse? accessToken;
late OAuth2Helper helper;
String beforeRedirectPath = "/";

String clientId;
String redirectUrl;
List<String> scopes;

User? user;

final Status<UserResponse> refreshStatus = Status();
final Status<UserResponse> validateUserStatus = Status();
final Status<EmptyResponse> resendRegistrationTokenStatus = Status();
final Status<UserResponse> validateDevStatus = Status();

final Status<EmptyResponse> logoutStatus = Status();

final Status<EmptyResponse> askCodeLostPasswordStatus = Status();
final Status<EmptyResponse> sendCodeLostPasswordStatus = Status();
final Status<EmptyResponse> changePasswordStatus = Status();

/// The route to redirect to after the user has logged in or out.
String? redirectToRoute;

OAuthModel(this.clientId, this.redirectUrl, {this.scopes = const []}) {
helper = OAuth2Helper(
LenraOAuth2Client(
Expand All @@ -30,11 +55,7 @@ class OAuthModel extends ChangeNotifier {
}

Future<AccessTokenResponse?> authenticate() async {
accessToken = await helper.getToken();

notifyListeners();

return accessToken;
return await helper.getToken();
}

Future<void> logout() async {
Expand Down Expand Up @@ -64,4 +85,41 @@ class OAuthModel extends ChangeNotifier {
return "http";
}
}

bool isOneOfRole(List<UserRole> roles) {
if (user == null) return false;
return roles.contains(user?.role);
}

Future<UserResponse> validateDev() async {
var res = await validateDevStatus.handle(() => UserApi.validateDev(ValidateDevRequest()), notifyListeners);
user = res.user;
return res;
}

Future<EmptyResponse> askCodeLostPassword(String email) async {
var res = await askCodeLostPasswordStatus.handle(
() => UserApi.askCodeLostPassword(AskCodeLostPasswordRequest(email)), notifyListeners);
notifyListeners();
return res;
}

Future<EmptyResponse> sendCodeLostPassword(String code, String email, String password, String confirmation) async {
var res = await sendCodeLostPasswordStatus.handle(
() => UserApi.sendCodeLostPassword(SendCodeLostPasswordRequest(code, email, password, confirmation)),
notifyListeners);
notifyListeners();
return res;
}

Future<EmptyResponse> changePassword(String old, String password, String confirmation) async {
var res = await changePasswordStatus.handle(
() => UserApi.changePassword(ChangePasswordRequest(old, password, confirmation)), notifyListeners);
notifyListeners();
return res;
}

String getRedirectionRouteAfterAuthentication() {
return redirectToRoute ?? CommonNavigator.homeRoute;
}
}
8 changes: 4 additions & 4 deletions lib/views/profile/change_password_form.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:client_common/api/response_models/api_error.dart';
import 'package:client_common/models/auth_model.dart';
import 'package:client_common/oauth/oauth_model.dart';
import 'package:client_common/views/error.dart';
import 'package:client_common/views/loading_button.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -34,8 +34,8 @@ class _ChangePasswordState extends State<ChangePasswordForm> {

@override
Widget build(BuildContext context) {
bool isChangingPassword = context.select<AuthModel, bool>((m) => m.changePasswordStatus.isFetching());
ApiError? changePasswordError = context.select<AuthModel, ApiError?>((m) => m.changePasswordStatus.error);
bool isChangingPassword = context.select<OAuthModel, bool>((m) => m.changePasswordStatus.isFetching());
ApiError? changePasswordError = context.select<OAuthModel, ApiError?>((m) => m.changePasswordStatus.error);

return Form(
key: _formKey,
Expand Down Expand Up @@ -112,7 +112,7 @@ class _ChangePasswordState extends State<ChangePasswordForm> {
child: LoadingButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
context.read<AuthModel>().changePassword(
context.read<OAuthModel>().changePassword(
oldPassword,
newPassword,
newPasswordConfirmation,
Expand Down

0 comments on commit c3f4271

Please sign in to comment.