Connected Realm and Redux to pin code screen

This commit is contained in:
mistikk 2018-09-15 02:04:44 +03:00
parent 596510bfe7
commit 0e3674fadf
5 changed files with 74 additions and 47 deletions

View File

@ -62,7 +62,7 @@ export const Login = (username, password) => {
// Save user data to Realm DB
setUserData(userData)
.then(() => {
resolve(loginFlag);
resolve({ ...account, password });
})
.catch(err => {
reject(err);
@ -98,6 +98,7 @@ export const setUserDataWithPinCode = (pinCode, password) =>
memoKey: encryptKey(privateKeys.memo.toString(), pinCode),
};
//TODO update user data
setUserData(updatedUserData)
.then(() => {
resolve();
@ -113,8 +114,12 @@ export const verifyPinCode = (pinCode, password) =>
new Promise((resolve, reject) => {
getUserData().then(result => {
const userData = Array.from(result)[0];
const masterKey = decryptKey(userData.masterKey, pinCode);
console.log("====userData====", Array.from(result));
console.log("====userData.masterKey====", userData.masterKey);
console.log("====pinCode====", pinCode, password);
const masterKey = decryptKey(userData.masterKey, pinCode);
console.log("====masterKey====", masterKey);
if (masterKey === password) {
resolve();
} else {

View File

@ -8,7 +8,10 @@ import {
const initialState = {
isFetching: null,
data: [],
data: {
accounts: [],
currentAccountId: null,
},
hasError: false,
errorMessage: null,
};
@ -33,7 +36,10 @@ export default function(state = initialState, action) {
return {
...state,
isFetching: false,
data: [...state.data, action.payload],
data: {
accounts: [...state.data.accounts, action.payload],
currentAccountId: action.payload.id,
},
hasError: false,
errorMessage: null,
};

View File

@ -3,5 +3,5 @@ import userReducer from "./userReducer";
import accountReducer from "./accountReducer";
export default combineReducers({
accounts: accountReducer,
account: accountReducer,
});

View File

@ -26,7 +26,7 @@ import { connect } from "react-redux";
import { Login } from "../../providers/steem/auth";
import { fetchAccountFromSteem } from "../../redux/actions/accountAction";
import { addNewAccount } from "../../redux/actions/accountAction";
import { default as INITIAL } from "../../constants/initial";
@ -52,41 +52,18 @@ class LoginPage extends Component {
Login(username, password)
.then(result => {
if (result === true) {
AsyncStorage.setItem(
INITIAL.IS_EXIST_USER,
JSON.stringify(false)
);
this.props.dispatch(
fetchAccountFromSteem(username, password)
);
AsyncStorage.getItem(
INITIAL.IS_EXIST_USER,
(err, value) => {
if (value === "true") {
Navigation.setStackRoot(componentId, {
component: {
name: "navigation.eSteem.Splash",
},
});
} else {
Navigation.setStackRoot(componentId, {
component: {
name: "navigation.eSteem.PinCode",
passProps: {
test: "test",
},
options: {
topBar: {
visible: false,
},
},
},
});
}
}
);
if (result) {
this.props.dispatch(addNewAccount(result));
Navigation.setStackRoot(componentId, {
component: {
name: "navigation.eSteem.PinCode",
options: {
topBar: {
visible: false,
},
},
},
});
}
})
.catch(err => {

View File

@ -1,5 +1,7 @@
import React from "react";
import { AsyncStorage } from "react-native";
import { connect } from "react-redux";
import { Navigation } from "react-native-navigation";
import {
setUserDataWithPinCode,
@ -24,6 +26,7 @@ class PinCodeContainer extends React.Component {
componentDidMount() {
this._getDataFromStorage().then(() => {
const { isExistUser } = this.state;
console.log("============isExistUser===========", isExistUser);
if (isExistUser) {
this.setState({
informationText: "verify screen",
@ -34,11 +37,17 @@ class PinCodeContainer extends React.Component {
});
}
});
console.log(
"==============password==========",
this.props.currentAccount.password
);
}
_getDataFromStorage = () =>
new Promise(resolve => {
AsyncStorage.getItem(INITIAL.IS_EXIST_USER, (err, result) => {
console.log("============IS_EXIST_USER===========", result);
this.setState(
{
isExistUser: JSON.parse(result),
@ -49,12 +58,25 @@ class PinCodeContainer extends React.Component {
});
_setPinCode = pin => {
const {
currentAccount: { password },
componentId,
} = this.props;
const { isExistUser, pinCode } = this.state;
console.log(password);
if (isExistUser) {
// If the user is exist, we are just checking to pin and navigating to home screen
verifyPinCode(pinCode, "").then(() => {});
// TODO navigate to home
verifyPinCode(pin, password)
.then(() => {
Navigation.setStackRoot(componentId, {
component: {
name: "navigation.eSteem.Home",
},
});
})
.catch(err => {
alert(err);
});
} else {
// If the user is logging in for the first time, the user should set to pin
if (!pinCode) {
@ -64,8 +86,19 @@ class PinCodeContainer extends React.Component {
});
} else {
if (pinCode === pin) {
setUserDataWithPinCode(pinCode, "").then(() => {});
// TODO navigate to home
setUserDataWithPinCode(pinCode, password).then(() => {
AsyncStorage.setItem(
INITIAL.IS_EXIST_USER,
JSON.stringify(true),
() => {
Navigation.setStackRoot(componentId, {
component: {
name: "navigation.eSteem.Home",
},
});
}
);
});
} else {
this.setState({
informationText: "wrongggg!!!",
@ -92,4 +125,10 @@ class PinCodeContainer extends React.Component {
}
}
export default PinCodeContainer;
const mapStateToProps = state => ({
currentAccount: state.account.data.accounts.find(
item => item.id === state.account.data.currentAccountId
),
});
export default connect(mapStateToProps)(PinCodeContainer);