Add ionia sign in.

This commit is contained in:
M 2022-07-27 13:16:42 +01:00
parent 2bc5b7055b
commit 0c8caf8847
8 changed files with 75 additions and 20 deletions

View file

@ -705,9 +705,9 @@ Future setup(
getIt.registerFactoryParam<IoniaVerifyIoniaOtp, List, void>((List args, _) { getIt.registerFactoryParam<IoniaVerifyIoniaOtp, List, void>((List args, _) {
final email = args.first as String; final email = args.first as String;
final ioniaAuthViewModel = args[1] as IoniaAuthViewModel; final isSignIn = args[1] as bool;
return IoniaVerifyIoniaOtp(ioniaAuthViewModel, email); return IoniaVerifyIoniaOtp(getIt.get<IoniaAuthViewModel>(), email, isSignIn);
}); });
getIt.registerFactory(() => IoniaWelcomePage(getIt.get<IoniaGiftCardsListViewModel>())); getIt.registerFactory(() => IoniaWelcomePage(getIt.get<IoniaGiftCardsListViewModel>()));

View file

@ -13,6 +13,7 @@ class IoniaApi {
static const pathPrefix = 'cake'; static const pathPrefix = 'cake';
static final createUserUri = Uri.https(baseUri, '/$pathPrefix/CreateUser'); static final createUserUri = Uri.https(baseUri, '/$pathPrefix/CreateUser');
static final verifyEmailUri = Uri.https(baseUri, '/$pathPrefix/VerifyEmail'); static final verifyEmailUri = Uri.https(baseUri, '/$pathPrefix/VerifyEmail');
static final signInUri = Uri.https(baseUri, '/$pathPrefix/SignIn');
static final createCardUri = Uri.https(baseUri, '/$pathPrefix/CreateCard'); static final createCardUri = Uri.https(baseUri, '/$pathPrefix/CreateCard');
static final getCardsUri = Uri.https(baseUri, '/$pathPrefix/GetCards'); static final getCardsUri = Uri.https(baseUri, '/$pathPrefix/GetCards');
static final getMerchantsUrl = Uri.https(baseUri, '/$pathPrefix/GetMerchants'); static final getMerchantsUrl = Uri.https(baseUri, '/$pathPrefix/GetMerchants');
@ -51,11 +52,13 @@ class IoniaApi {
Future<IoniaUserCredentials> verifyEmail({ Future<IoniaUserCredentials> verifyEmail({
@required String username, @required String username,
@required String email,
@required String code, @required String code,
@required String clientId}) async { @required String clientId}) async {
final headers = <String, String>{ final headers = <String, String>{
'clientId': clientId, 'clientId': clientId,
'username': username}; 'username': username,
'EmailAddress': email};
final query = <String, String>{'verificationCode': code}; final query = <String, String>{'verificationCode': code};
final uri = verifyEmailUri.replace(queryParameters: query); final uri = verifyEmailUri.replace(queryParameters: query);
final response = await put(uri, headers: headers); final response = await put(uri, headers: headers);
@ -69,12 +72,37 @@ class IoniaApi {
final data = bodyJson['Data'] as Map<String, Object>; final data = bodyJson['Data'] as Map<String, Object>;
final isSuccessful = bodyJson['Successful'] as bool; final isSuccessful = bodyJson['Successful'] as bool;
if (!isSuccessful) {
throw Exception(bodyJson['ErrorMessage'] as String);
}
final password = data['password'] as String;
username = data['username'] as String;
return IoniaUserCredentials(username, password);
}
// Sign In
Future<String> signIn(String email, {@required String clientId}) async {
final headers = <String, String>{'clientId': clientId};
final query = <String, String>{'emailAddress': email};
final uri = signInUri.replace(queryParameters: query);
final response = await put(uri, headers: headers);
if (response.statusCode != 200) {
// throw exception
return null;
}
final bodyJson = json.decode(response.body) as Map<String, Object>;
final data = bodyJson['Data'] as Map<String, Object>;
final isSuccessful = bodyJson['Successful'] as bool;
if (!isSuccessful) { if (!isSuccessful) {
throw Exception(data['ErrorMessage'] as String); throw Exception(data['ErrorMessage'] as String);
} }
final password = data['password'] as String; return data['username'] as String;
return IoniaUserCredentials(username, password);
} }
// Get virtual card // Get virtual card

View file

@ -3,6 +3,8 @@ import 'package:flutter/material.dart';
abstract class IoniaCreateAccountState {} abstract class IoniaCreateAccountState {}
class IoniaInitialCreateState extends IoniaCreateAccountState {}
class IoniaCreateStateSuccess extends IoniaCreateAccountState {} class IoniaCreateStateSuccess extends IoniaCreateAccountState {}
class IoniaCreateStateLoading extends IoniaCreateAccountState {} class IoniaCreateStateLoading extends IoniaCreateAccountState {}

View file

@ -32,8 +32,18 @@ class IoniaService {
Future<void> verifyEmail(String code) async { Future<void> verifyEmail(String code) async {
final username = await secureStorage.read(key: ioniaUsernameStorageKey); final username = await secureStorage.read(key: ioniaUsernameStorageKey);
final credentials = await ioniaApi.verifyEmail(username: username, code: code, clientId: clientId); final email = await secureStorage.read(key: ioniaEmailStorageKey);
final credentials = await ioniaApi.verifyEmail(email: email, username: username, code: code, clientId: clientId);
await secureStorage.write(key: ioniaPasswordStorageKey, value: credentials.password); await secureStorage.write(key: ioniaPasswordStorageKey, value: credentials.password);
await secureStorage.write(key: ioniaUsernameStorageKey, value: credentials.username);
}
// Sign In
Future<void> signIn(String email) async {
final username = await ioniaApi.signIn(email, clientId: clientId);
await secureStorage.write(key: ioniaEmailStorageKey, value: email);
await secureStorage.write(key: ioniaUsernameStorageKey, value: username);
} }
Future<String> getUserEmail() async { Future<String> getUserEmail() async {

View file

@ -149,6 +149,6 @@ class IoniaCreateAccountPage extends BasePage {
void _onCreateSuccessful(BuildContext context, IoniaAuthViewModel authViewModel) => Navigator.pushNamed( void _onCreateSuccessful(BuildContext context, IoniaAuthViewModel authViewModel) => Navigator.pushNamed(
context, context,
Routes.ioniaVerifyIoniaOtpPage, Routes.ioniaVerifyIoniaOtpPage,
arguments: [authViewModel.email, authViewModel], arguments: [authViewModel.email, false],
); );
} }

View file

@ -43,7 +43,7 @@ class IoniaLoginPage extends BasePage {
@override @override
Widget body(BuildContext context) { Widget body(BuildContext context) {
reaction((_) => _authViewModel.createUserState, (IoniaCreateAccountState state) { reaction((_) => _authViewModel.signInState, (IoniaCreateAccountState state) {
if (state is IoniaCreateStateFailure) { if (state is IoniaCreateStateFailure) {
_onLoginUserFailure(context, state.error); _onLoginUserFailure(context, state.error);
} }
@ -75,9 +75,9 @@ class IoniaLoginPage extends BasePage {
if (!_formKey.currentState.validate()) { if (!_formKey.currentState.validate()) {
return; return;
} }
await _authViewModel.createUser(_emailController.text); await _authViewModel.signIn(_emailController.text);
}, },
isLoading: _authViewModel.createUserState is IoniaCreateStateLoading, isLoading: _authViewModel.signInState is IoniaCreateStateLoading,
color: Theme.of(context).accentTextTheme.body2.color, color: Theme.of(context).accentTextTheme.body2.color,
textColor: Colors.white, textColor: Colors.white,
), ),
@ -107,6 +107,6 @@ class IoniaLoginPage extends BasePage {
void _onLoginSuccessful(BuildContext context, IoniaAuthViewModel authViewModel) => Navigator.pushNamed( void _onLoginSuccessful(BuildContext context, IoniaAuthViewModel authViewModel) => Navigator.pushNamed(
context, context,
Routes.ioniaVerifyIoniaOtpPage, Routes.ioniaVerifyIoniaOtpPage,
arguments: [authViewModel.email, authViewModel], arguments: [authViewModel.email, true],
); );
} }

View file

@ -17,7 +17,7 @@ import 'package:keyboard_actions/keyboard_actions.dart';
import 'package:mobx/mobx.dart'; import 'package:mobx/mobx.dart';
class IoniaVerifyIoniaOtp extends BasePage { class IoniaVerifyIoniaOtp extends BasePage {
IoniaVerifyIoniaOtp(this._authViewModel, this._email) IoniaVerifyIoniaOtp(this._authViewModel, this._email, this.isSignIn)
: _codeController = TextEditingController(), : _codeController = TextEditingController(),
_codeFocus = FocusNode() { _codeFocus = FocusNode() {
_codeController.addListener(() { _codeController.addListener(() {
@ -32,6 +32,7 @@ class IoniaVerifyIoniaOtp extends BasePage {
} }
final IoniaAuthViewModel _authViewModel; final IoniaAuthViewModel _authViewModel;
final bool isSignIn;
final String _email; final String _email;
@ -94,7 +95,9 @@ class IoniaVerifyIoniaOtp extends BasePage {
Text(S.of(context).dont_get_code), Text(S.of(context).dont_get_code),
SizedBox(width: 20), SizedBox(width: 20),
InkWell( InkWell(
onTap: () => _authViewModel.createUser(_email), onTap: () => isSignIn
? _authViewModel.signIn(_email)
: _authViewModel.createUser(_email),
child: Text( child: Text(
S.of(context).resend_code, S.of(context).resend_code,
style: textSmallSemiBold(color: Palette.blueCraiola), style: textSmallSemiBold(color: Palette.blueCraiola),

View file

@ -9,17 +9,18 @@ class IoniaAuthViewModel = IoniaAuthViewModelBase with _$IoniaAuthViewModel;
abstract class IoniaAuthViewModelBase with Store { abstract class IoniaAuthViewModelBase with Store {
IoniaAuthViewModelBase({this.ioniaService}): IoniaAuthViewModelBase({this.ioniaService}):
createUserState = IoniaCreateStateSuccess(), createUserState = IoniaInitialCreateState(),
otpState = IoniaOtpSendDisabled(){ signInState = IoniaInitialCreateState(),
otpState = IoniaOtpSendDisabled();
}
final IoniaService ioniaService; final IoniaService ioniaService;
@observable @observable
IoniaCreateAccountState createUserState; IoniaCreateAccountState createUserState;
@observable
IoniaCreateAccountState signInState;
@observable @observable
IoniaOtpState otpState; IoniaOtpState otpState;
@ -42,14 +43,25 @@ abstract class IoniaAuthViewModelBase with Store {
@action @action
Future<void> createUser(String email) async { Future<void> createUser(String email) async {
createUserState = IoniaCreateStateLoading();
try { try {
createUserState = IoniaCreateStateLoading();
await ioniaService.createUser(email); await ioniaService.createUser(email);
createUserState = IoniaCreateStateSuccess(); createUserState = IoniaCreateStateSuccess();
} on Exception catch (e) { } catch (e) {
createUserState = IoniaCreateStateFailure(error: e.toString()); createUserState = IoniaCreateStateFailure(error: e.toString());
} }
} }
@action
Future<void> signIn(String email) async {
try {
signInState = IoniaCreateStateLoading();
await ioniaService.signIn(email);
signInState = IoniaCreateStateSuccess();
} catch (e) {
signInState = IoniaCreateStateFailure(error: e.toString());
}
}
} }