Skip to content

Commit

Permalink
login with device identifier (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
bejavu authored Mar 12, 2020
1 parent c749126 commit e39b586
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 7 deletions.
5 changes: 3 additions & 2 deletions lib/middlewares/auth_middleware.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ Middleware<AppState> _createVerifyPhoneNumberMiddleware() {
final FirebaseUser user = (await firebaseAuth.signInWithCredential(credential)).user;
final FirebaseUser currentUser = await firebaseAuth.currentUser();
assert(user.uid == currentUser.uid);
String accountAddress = store.state.userState.accountAddress;
final String accountAddress = store.state.userState.accountAddress;
final String identifier = store.state.userState.identifier;
IdTokenResult token = await user.getIdToken();
String jwtToken = await api.login(token.token, accountAddress);
String jwtToken = await api.login(token.token, accountAddress, identifier);
store.dispatch(new LoginVerifySuccess(jwtToken));
store.dispatch(SetIsVerifyRequest(isLoading: false));
store.dispatch(segmentTrackCall("Wallet: verified phone number"));
Expand Down
5 changes: 5 additions & 0 deletions lib/models/user_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class UserState {
final String displayName;
final String email;
final String verificationId;
final String identifier;
final bool loginRequestSuccess;
final bool loginVerifySuccess;
final bool isLoggedOut;
Expand Down Expand Up @@ -51,6 +52,7 @@ class UserState {
this.displayName,
this.email,
this.verificationId,
this.identifier,
this.loginRequestSuccess,
this.loginVerifySuccess,
this.isLoggedOut,
Expand All @@ -77,6 +79,7 @@ class UserState {
displayName: "Anom",
email: "",
verificationId: "",
identifier: "",
loginRequestSuccess: false,
loginVerifySuccess: false,
isLoggedOut: false,
Expand All @@ -103,6 +106,7 @@ class UserState {
String displayName,
String email,
String verificationId,
String identifier,
bool loginRequestSuccess,
bool loginVerifySuccess,
bool isLoggedOut,
Expand All @@ -127,6 +131,7 @@ class UserState {
displayName: displayName ?? this.displayName,
email: email ?? this.email,
verificationId: verificationId ?? this.verificationId,
identifier: identifier ?? this.identifier,
loginRequestSuccess: loginRequestSuccess ?? this.loginRequestSuccess,
loginVerifySuccess: loginVerifySuccess ?? this.loginVerifySuccess,
isLoggedOut: isLoggedOut ?? this.isLoggedOut,
Expand Down
7 changes: 6 additions & 1 deletion lib/models/views/splash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ class SplashViewModel extends Equatable {
final String jwtToken;
final bool isLoggedOut;
final Function() loginAgain;
final Function() setDeviceIdCall;
final Function(VoidCallback successCallback) createLocalAccount;

SplashViewModel(
{this.privateKey,
this.jwtToken,
this.isLoggedOut,
this.createLocalAccount,
this.loginAgain});
this.loginAgain,
this.setDeviceIdCall});

static SplashViewModel fromStore(Store<AppState> store) {
return SplashViewModel(
Expand All @@ -28,6 +30,9 @@ class SplashViewModel extends Equatable {
createLocalAccount: (VoidCallback successCallback) {
store.dispatch(createLocalAccountCall(successCallback));
},
setDeviceIdCall: () {
store.dispatch(setDeviceId());
},
loginAgain: () {
store.dispatch(getWalletAddressessCall());
store.dispatch(identifyCall());
Expand Down
34 changes: 32 additions & 2 deletions lib/redux/actions/user_actions.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:io';

import 'package:device_info/device_info.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -142,6 +145,10 @@ class SetIsVerifyRequest {
SetIsVerifyRequest({this.isLoading});
}

class DeviceIdSuccess {
final String identifier;
DeviceIdSuccess(this.identifier);
}

ThunkAction backupWalletCall() {
return (Store store) async {
Expand Down Expand Up @@ -213,6 +220,27 @@ ThunkAction restoreWalletCall(List<String> _mnemonic, VoidCallback successCallba
};
}

ThunkAction setDeviceId() {
return (Store store) async {
final logger = await AppFactory().getLogger('action');
String identifier;
final DeviceInfoPlugin deviceInfoPlugin = new DeviceInfoPlugin();
try {
if (Platform.isAndroid) {
var build = await deviceInfoPlugin.androidInfo;
identifier = build.androidId; //UUID for Android
} else if (Platform.isIOS) {
var data = await deviceInfoPlugin.iosInfo;
identifier = data.identifierForVendor; //UUID for iOS
}
} on Exception {
logger.severe('Failed to get platform version');
}
logger.info("device identifier: $identifier");
store.dispatch(new DeviceIdSuccess(identifier));
};
}

ThunkAction createLocalAccountCall(VoidCallback successCallback) {
return (Store store) async {
final logger = await AppFactory().getLogger('action');
Expand Down Expand Up @@ -306,7 +334,8 @@ ThunkAction identifyFirstTimeCall() {
"Phone Number": fullPhoneNumber,
"Wallet Address": store.state.cashWalletState.walletAddress,
"Account Address": store.state.userState.accountAddress,
"Display Name": store.state.userState.displayName
"Display Name": store.state.userState.displayName,
"Identifier": store.state.userState.identifier
})));
};
}
Expand All @@ -319,7 +348,8 @@ ThunkAction identifyCall() {
"Phone Number": fullPhoneNumber,
"Wallet Address": store.state.cashWalletState.walletAddress,
"Account Address": store.state.userState.accountAddress,
"Display Name": store.state.userState.displayName
"Display Name": store.state.userState.displayName,
"Identifier": store.state.userState.identifier
})));
};
}
Expand Down
6 changes: 6 additions & 0 deletions lib/redux/reducers/user_reducer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ final userReducers = combineReducers<UserState>([
TypedReducer<UserState, JustInstalled>(_justInstalled),
TypedReducer<UserState, SetIsLoginRequest>(_setIsLoginRequest),
TypedReducer<UserState, SetIsVerifyRequest>(_setIsVerifyRequest),
TypedReducer<UserState, DeviceIdSuccess>(_deviceIdSuccess),
]);

UserState _backupSuccess(UserState state, BackupSuccess action) {
Expand Down Expand Up @@ -119,3 +120,8 @@ UserState _setIsLoginRequest(UserState state, SetIsLoginRequest action) {
UserState _setIsVerifyRequest(UserState state, SetIsVerifyRequest action) {
return state.copyWith(isVerifyRequest: action.isLoading);
}

UserState _deviceIdSuccess(UserState state, DeviceIdSuccess action) {
return state.copyWith(identifier: action.identifier);
}

2 changes: 2 additions & 0 deletions lib/screens/splash/create_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class _CreateWalletState extends State<CreateWallet> {
if (viewModel.isLoggedOut) {
viewModel.loginAgain();
} else {
viewModel.setDeviceIdCall();
viewModel.createLocalAccount(() {
setState(() {
isPrimaryPreloading = false;
Expand Down Expand Up @@ -78,6 +79,7 @@ class _CreateWalletState extends State<CreateWallet> {
fontSize: 14,
label: I18n.of(context).create__wallet,
onPressed: () async {
viewModel.setDeviceIdCall();
viewModel.createLocalAccount(() {
setState(() {
isTransparentPreloading = false;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ packages:
name: wallet_core
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.1+67"
version: "0.1.0"
watcher:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies:
redux_persist: ^0.8.2
redux_persist_flutter: ^0.8.2
country_code_picker: ^1.2.4
wallet_core: ^0.0.1+67
wallet_core: ^0.1.0
# wallet_core:
# path: ../wallet_core
# alphabet_list_scroll_view: ^1.0.6
Expand Down

0 comments on commit e39b586

Please sign in to comment.