Updated verify pin code function

This commit is contained in:
mistikk 2018-10-22 01:42:12 +03:00
parent af61cef2e6
commit d705aa631c
6 changed files with 56 additions and 19 deletions

View File

@ -5,6 +5,8 @@ import {
setAuthStatus,
getUserDataWithUsername,
updateUserData,
setPinCode,
getPinCode,
} from '../../realm/realm';
import { encryptKey, decryptKey } from '../../utils/crypto';
import steemConnect from './steemConnectAPI';
@ -114,7 +116,14 @@ export const setUserDataWithPinCode = data => new Promise((resolve, reject) => {
setAuthStatus(authData)
.then(() => {
resolve();
const encriptedPinCode = encryptKey(data.pinCode, 'pin-code');
setPinCode(encriptedPinCode)
.then(() => {
resolve();
})
.catch((error) => {
reject(error);
});
})
.catch((error) => {
reject(error);
@ -129,22 +138,40 @@ export const setUserDataWithPinCode = data => new Promise((resolve, reject) => {
export const verifyPinCode = data => new Promise((resolve, reject) => {
const result = getUserDataWithUsername(data.username);
const userData = result[0];
const masterKey = decryptKey(userData.masterKey, data.pinCode);
if (masterKey === data.password) {
let loginFlag = false;
if (userData.masterKey || userData.accessToken) {
const masterKey = decryptKey(userData.masterKey, data.pinCode);
const accessToken = decryptKey(userData.accessToken, data.pinCode);
if (masterKey === data.password || accessToken === data.accessToken) {
loginFlag = true;
}
} else if (data.accessToken) {
getPinCode()
.then((encriptedPinCode) => {
const pinCode = decryptKey(encriptedPinCode, 'pin-code');
if (pinCode === data.pinCode) {
loginFlag = true;
}
})
.catch(() => {
reject(new Error('Unknown error, please contact to eSteem.'));
});
}
if (loginFlag) {
const authData = {
isLoggedIn: true,
};
setAuthStatus(authData)
.then(() => {
resolve();
})
.catch(() => {
reject(new Error('Invalid pin code, please check and try again'));
.catch((error) => {
// TODO: create function for throw error
reject(new Error('Unknown error, please contact to eSteem.'));
});
} else {
reject(new Error('Invalid pin code, please check and try again'));
reject();
}
});

View File

@ -21,7 +21,7 @@ const authSchema = {
name: AUTH_SCHEMA,
properties: {
isLoggedIn: { type: 'bool', default: false },
pinCode: { tyep: 'string' },
pinCode: { type: 'string' },
},
};
@ -116,9 +116,8 @@ export const setAuthStatus = authStatus => new Promise((resolve, reject) => {
try {
const auth = realm.objects(AUTH_SCHEMA);
realm.write(() => {
realm.delete(auth);
realm.create(AUTH_SCHEMA, authStatus);
resolve(authStatus);
auth[0].isLoggedIn = authStatus.isLoggedIn;
resolve(auth[0]);
});
} catch (error) {
reject(error);
@ -137,3 +136,14 @@ export const setPinCode = pinCode => new Promise((resolve, reject) => {
reject(error);
}
});
export const getPinCode = () => new Promise((resolve, reject) => {
try {
const auth = realm.objects(AUTH_SCHEMA);
if (auth[0]) {
resolve(auth[0].pinCode);
}
} catch (error) {
reject(error);
}
});

View File

@ -51,6 +51,7 @@ class PinCodeContainer extends Component {
const {
currentAccount: { password, name },
navigation,
accessToken,
} = this.props;
const { isExistUser, pinCode } = this.state;
if (isExistUser) {
@ -59,11 +60,12 @@ class PinCodeContainer extends Component {
pinCode: pin,
password,
username: name,
accessToken,
};
verifyPinCode(pinData)
.then(() => {
// TODO: make global route
navigation.navigate(ROUTES.DRAWER.MAIN);
resolve();
})
.catch((err) => {
alert(err);

View File

@ -1,7 +1,5 @@
import React, { Component } from 'react';
import {
Text, TouchableOpacity, Animated, View,
} from 'react-native';
import { Text, TouchableOpacity, View } from 'react-native';
import { Container } from 'native-base';
import { Logo, NumericKeyboard, PinAnimatedInput } from '../../../components';
@ -12,7 +10,6 @@ class PinCodeScreen extends Component {
constructor(props) {
super(props);
this.state = {
showPassword: false,
pin: '',
};
}
@ -37,6 +34,7 @@ class PinCodeScreen extends Component {
this.setState({ pin: newPin });
setPinCode(`${pin}${value}`)
.then(() => {
// TODO: fix unmounted component error
this.setState({ pin: '' });
})
.catch(() => {

View File

@ -40,7 +40,7 @@ class SteemConnect extends Component {
if (result) {
dispatch(addPassiveAccount(result));
dispatch(loginAction());
navigation.navigate(ROUTES.SCREENS.PINCODE);
navigation.navigate(ROUTES.SCREENS.PINCODE, { accessToken });
} else {
// TODO: Error alert (Toast Message)
console.log('loginWithSC2 error');

View File

@ -1,5 +1,5 @@
import CryptoJS from 'crypto-js';
export const encryptKey = (key, pinCode) => CryptoJS.AES.encrypt(key, pinCode).toString();
export const encryptKey = (key, data) => CryptoJS.AES.encrypt(key, data).toString();
export const decryptKey = (key, pinCode) => CryptoJS.AES.decrypt(key, pinCode).toString(CryptoJS.enc.Utf8);
export const decryptKey = (key, data) => CryptoJS.AES.decrypt(key, data).toString(CryptoJS.enc.Utf8);