OAuth
This commit is contained in:
parent
1df37028a9
commit
12ecab9e37
|
|
@ -54,6 +54,30 @@ class SettingsRepository {
|
|||
return userResponse.data;
|
||||
}
|
||||
|
||||
Future<UserEntity> connectOAuthUser(
|
||||
Credentials credentials,
|
||||
String password,
|
||||
String idToken,
|
||||
String serverAuthCode,
|
||||
) async {
|
||||
dynamic response;
|
||||
|
||||
final url = credentials.url + '/connected_account?include=company_user';
|
||||
response = await webClient.post(
|
||||
url,
|
||||
credentials.token,
|
||||
data: json
|
||||
.encode({'id_token': idToken, 'server_auth_code': serverAuthCode}),
|
||||
password: password,
|
||||
idToken: idToken,
|
||||
);
|
||||
|
||||
final UserItemResponse userResponse =
|
||||
serializers.deserializeWith(UserItemResponse.serializer, response);
|
||||
|
||||
return userResponse.data;
|
||||
}
|
||||
|
||||
Future<UserCompanyEntity> saveUserSettings(
|
||||
Credentials credentials, UserEntity user) async {
|
||||
final data = serializers.serializeWith(UserEntity.serializer, user);
|
||||
|
|
|
|||
|
|
@ -117,15 +117,17 @@ class SaveAuthUserFailure implements StopSaving {
|
|||
class ConnecOAuthUserRequest implements StartSaving {
|
||||
ConnecOAuthUserRequest({
|
||||
@required this.provider,
|
||||
@required this.idToken,
|
||||
@required this.serverAuthCode,
|
||||
this.completer,
|
||||
this.password,
|
||||
this.idToken,
|
||||
});
|
||||
|
||||
final Completer completer;
|
||||
final String provider;
|
||||
final String password;
|
||||
final String idToken;
|
||||
final String serverAuthCode;
|
||||
}
|
||||
|
||||
class ConnecOAuthUserSuccess implements StopSaving, PersistData, PersistUI {
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ List<Middleware<AppState>> createStoreSettingsMiddleware([
|
|||
final viewSettings = _viewSettings();
|
||||
final saveCompany = _saveCompany(repository);
|
||||
final saveAuthUser = _saveAuthUser(repository);
|
||||
final connectOAuthUser = _connectOAuthUser(repository);
|
||||
final saveSettings = _saveSettings(repository);
|
||||
final uploadLogo = _uploadLogo(repository);
|
||||
final saveDocument = _saveDocument(repository);
|
||||
|
|
@ -31,6 +32,7 @@ List<Middleware<AppState>> createStoreSettingsMiddleware([
|
|||
TypedMiddleware<AppState, ViewSettings>(viewSettings),
|
||||
TypedMiddleware<AppState, SaveCompanyRequest>(saveCompany),
|
||||
TypedMiddleware<AppState, SaveAuthUserRequest>(saveAuthUser),
|
||||
TypedMiddleware<AppState, ConnecOAuthUserRequest>(connectOAuthUser),
|
||||
TypedMiddleware<AppState, SaveUserSettingsRequest>(saveSettings),
|
||||
TypedMiddleware<AppState, UploadLogoRequest>(uploadLogo),
|
||||
TypedMiddleware<AppState, SaveCompanyDocumentRequest>(saveDocument),
|
||||
|
|
@ -124,6 +126,34 @@ Middleware<AppState> _saveAuthUser(SettingsRepository settingsRepository) {
|
|||
};
|
||||
}
|
||||
|
||||
Middleware<AppState> _connectOAuthUser(SettingsRepository settingsRepository) {
|
||||
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
|
||||
final action = dynamicAction as ConnecOAuthUserRequest;
|
||||
|
||||
settingsRepository
|
||||
.connectOAuthUser(store.state.credentials, action.password,
|
||||
action.idToken, action.serverAuthCode)
|
||||
.then((user) {
|
||||
store.dispatch(ConnecOAuthUserSuccess(user));
|
||||
store.dispatch(UserVerifiedPassword());
|
||||
if (action.completer != null) {
|
||||
action.completer.complete();
|
||||
}
|
||||
}).catchError((Object error) {
|
||||
print(error);
|
||||
store.dispatch(ConnecOAuthUserFailure(error));
|
||||
if ('$error'.contains('412')) {
|
||||
store.dispatch(UserUnverifiedPassword());
|
||||
}
|
||||
if (action.completer != null) {
|
||||
action.completer.completeError(error);
|
||||
}
|
||||
});
|
||||
|
||||
next(action);
|
||||
};
|
||||
}
|
||||
|
||||
Middleware<AppState> _saveSettings(SettingsRepository settingsRepository) {
|
||||
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
|
||||
final action = dynamicAction as SaveUserSettingsRequest;
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import 'package:invoiceninja_flutter/ui/settings/user_details.dart';
|
|||
import 'package:invoiceninja_flutter/utils/completers.dart';
|
||||
import 'package:invoiceninja_flutter/utils/dialogs.dart';
|
||||
import 'package:invoiceninja_flutter/utils/localization.dart';
|
||||
import 'package:invoiceninja_flutter/utils/oauth.dart';
|
||||
import 'package:redux/redux.dart';
|
||||
import 'package:invoiceninja_flutter/redux/app/app_state.dart';
|
||||
|
||||
|
|
@ -78,13 +79,16 @@ class UserDetailsVM {
|
|||
passwordCallback(
|
||||
context: context,
|
||||
callback: (password, idToken) {
|
||||
store.dispatch(
|
||||
ConnecOAuthUserRequest(
|
||||
provider: UserEntity.OAUTH_PROVIDER_GOOGLE,
|
||||
password: password,
|
||||
idToken: idToken,
|
||||
),
|
||||
);
|
||||
googleSignUp((idToken, accessToken, serverAuthCode) {
|
||||
store.dispatch(
|
||||
ConnecOAuthUserRequest(
|
||||
provider: UserEntity.OAUTH_PROVIDER_GOOGLE,
|
||||
password: password,
|
||||
idToken: idToken,
|
||||
serverAuthCode: serverAuthCode,
|
||||
),
|
||||
);
|
||||
});
|
||||
});
|
||||
},
|
||||
onSavePressed: (context) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue