diff --git a/lib/di.dart b/lib/di.dart index 9b2d70b38..bd911335d 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -705,9 +705,9 @@ Future setup( getIt.registerFactoryParam((List args, _) { 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(), email, isSignIn); }); getIt.registerFactory(() => IoniaWelcomePage(getIt.get())); diff --git a/lib/ionia/ionia_api.dart b/lib/ionia/ionia_api.dart index 80280860e..6784e2ba1 100644 --- a/lib/ionia/ionia_api.dart +++ b/lib/ionia/ionia_api.dart @@ -13,6 +13,7 @@ class IoniaApi { static const pathPrefix = 'cake'; static final createUserUri = Uri.https(baseUri, '/$pathPrefix/CreateUser'); 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 getCardsUri = Uri.https(baseUri, '/$pathPrefix/GetCards'); static final getMerchantsUrl = Uri.https(baseUri, '/$pathPrefix/GetMerchants'); @@ -51,11 +52,13 @@ class IoniaApi { Future verifyEmail({ @required String username, + @required String email, @required String code, @required String clientId}) async { final headers = { 'clientId': clientId, - 'username': username}; + 'username': username, + 'EmailAddress': email}; final query = {'verificationCode': code}; final uri = verifyEmailUri.replace(queryParameters: query); final response = await put(uri, headers: headers); @@ -70,13 +73,38 @@ class IoniaApi { final isSuccessful = bodyJson['Successful'] as bool; if (!isSuccessful) { - throw Exception(data['ErrorMessage'] as String); + throw Exception(bodyJson['ErrorMessage'] as String); } final password = data['password'] as String; + username = data['username'] as String; return IoniaUserCredentials(username, password); } + // Sign In + + Future signIn(String email, {@required String clientId}) async { + final headers = {'clientId': clientId}; + final query = {'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; + final data = bodyJson['Data'] as Map; + final isSuccessful = bodyJson['Successful'] as bool; + + if (!isSuccessful) { + throw Exception(data['ErrorMessage'] as String); + } + + return data['username'] as String; + } + // Get virtual card Future getCards({ diff --git a/lib/ionia/ionia_create_state.dart b/lib/ionia/ionia_create_state.dart index 199d72c73..b0277be45 100644 --- a/lib/ionia/ionia_create_state.dart +++ b/lib/ionia/ionia_create_state.dart @@ -3,6 +3,8 @@ import 'package:flutter/material.dart'; abstract class IoniaCreateAccountState {} +class IoniaInitialCreateState extends IoniaCreateAccountState {} + class IoniaCreateStateSuccess extends IoniaCreateAccountState {} class IoniaCreateStateLoading extends IoniaCreateAccountState {} diff --git a/lib/ionia/ionia_service.dart b/lib/ionia/ionia_service.dart index b5ed5a495..d1a2052f0 100644 --- a/lib/ionia/ionia_service.dart +++ b/lib/ionia/ionia_service.dart @@ -32,8 +32,18 @@ class IoniaService { Future verifyEmail(String code) async { 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: ioniaUsernameStorageKey, value: credentials.username); + } + + // Sign In + + Future 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 getUserEmail() async { diff --git a/lib/src/screens/ionia/auth/ionia_create_account_page.dart b/lib/src/screens/ionia/auth/ionia_create_account_page.dart index 6f8cd8b13..5e2797768 100644 --- a/lib/src/screens/ionia/auth/ionia_create_account_page.dart +++ b/lib/src/screens/ionia/auth/ionia_create_account_page.dart @@ -149,6 +149,6 @@ class IoniaCreateAccountPage extends BasePage { void _onCreateSuccessful(BuildContext context, IoniaAuthViewModel authViewModel) => Navigator.pushNamed( context, Routes.ioniaVerifyIoniaOtpPage, - arguments: [authViewModel.email, authViewModel], + arguments: [authViewModel.email, false], ); } diff --git a/lib/src/screens/ionia/auth/ionia_login_page.dart b/lib/src/screens/ionia/auth/ionia_login_page.dart index 7aadce198..bcbc0fee3 100644 --- a/lib/src/screens/ionia/auth/ionia_login_page.dart +++ b/lib/src/screens/ionia/auth/ionia_login_page.dart @@ -43,7 +43,7 @@ class IoniaLoginPage extends BasePage { @override Widget body(BuildContext context) { - reaction((_) => _authViewModel.createUserState, (IoniaCreateAccountState state) { + reaction((_) => _authViewModel.signInState, (IoniaCreateAccountState state) { if (state is IoniaCreateStateFailure) { _onLoginUserFailure(context, state.error); } @@ -75,9 +75,9 @@ class IoniaLoginPage extends BasePage { if (!_formKey.currentState.validate()) { 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, textColor: Colors.white, ), @@ -107,6 +107,6 @@ class IoniaLoginPage extends BasePage { void _onLoginSuccessful(BuildContext context, IoniaAuthViewModel authViewModel) => Navigator.pushNamed( context, Routes.ioniaVerifyIoniaOtpPage, - arguments: [authViewModel.email, authViewModel], + arguments: [authViewModel.email, true], ); } diff --git a/lib/src/screens/ionia/auth/ionia_verify_otp_page.dart b/lib/src/screens/ionia/auth/ionia_verify_otp_page.dart index 95df9c25a..c219781cc 100644 --- a/lib/src/screens/ionia/auth/ionia_verify_otp_page.dart +++ b/lib/src/screens/ionia/auth/ionia_verify_otp_page.dart @@ -17,7 +17,7 @@ import 'package:keyboard_actions/keyboard_actions.dart'; import 'package:mobx/mobx.dart'; class IoniaVerifyIoniaOtp extends BasePage { - IoniaVerifyIoniaOtp(this._authViewModel, this._email) + IoniaVerifyIoniaOtp(this._authViewModel, this._email, this.isSignIn) : _codeController = TextEditingController(), _codeFocus = FocusNode() { _codeController.addListener(() { @@ -32,6 +32,7 @@ class IoniaVerifyIoniaOtp extends BasePage { } final IoniaAuthViewModel _authViewModel; + final bool isSignIn; final String _email; @@ -94,7 +95,9 @@ class IoniaVerifyIoniaOtp extends BasePage { Text(S.of(context).dont_get_code), SizedBox(width: 20), InkWell( - onTap: () => _authViewModel.createUser(_email), + onTap: () => isSignIn + ? _authViewModel.signIn(_email) + : _authViewModel.createUser(_email), child: Text( S.of(context).resend_code, style: textSmallSemiBold(color: Palette.blueCraiola), diff --git a/lib/view_model/ionia/ionia_auth_view_model.dart b/lib/view_model/ionia/ionia_auth_view_model.dart index f61016866..5e662b462 100644 --- a/lib/view_model/ionia/ionia_auth_view_model.dart +++ b/lib/view_model/ionia/ionia_auth_view_model.dart @@ -9,17 +9,18 @@ class IoniaAuthViewModel = IoniaAuthViewModelBase with _$IoniaAuthViewModel; abstract class IoniaAuthViewModelBase with Store { IoniaAuthViewModelBase({this.ioniaService}): - createUserState = IoniaCreateStateSuccess(), - otpState = IoniaOtpSendDisabled(){ - - - } + createUserState = IoniaInitialCreateState(), + signInState = IoniaInitialCreateState(), + otpState = IoniaOtpSendDisabled(); final IoniaService ioniaService; @observable IoniaCreateAccountState createUserState; + @observable + IoniaCreateAccountState signInState; + @observable IoniaOtpState otpState; @@ -42,14 +43,25 @@ abstract class IoniaAuthViewModelBase with Store { @action Future createUser(String email) async { - createUserState = IoniaCreateStateLoading(); try { + createUserState = IoniaCreateStateLoading(); await ioniaService.createUser(email); - createUserState = IoniaCreateStateSuccess(); - } on Exception catch (e) { + } catch (e) { createUserState = IoniaCreateStateFailure(error: e.toString()); } } + + @action + Future signIn(String email) async { + try { + signInState = IoniaCreateStateLoading(); + await ioniaService.signIn(email); + signInState = IoniaCreateStateSuccess(); + } catch (e) { + signInState = IoniaCreateStateFailure(error: e.toString()); + } + } + } \ No newline at end of file