Refactoring for steem connect login

This commit is contained in:
mistikk 2018-10-19 00:34:53 +03:00
parent 690745b925
commit 3e78211616
9 changed files with 67 additions and 45 deletions

View File

@ -3,7 +3,9 @@ import { BaseNavigator } from '../navigation';
import { default as ROUTES } from '../constants/routeNames';
// Screens
import { Splash, Login, PinCode } from '../screens';
import {
Splash, Login, PinCode, SteemConnect,
} from '../screens';
// Components
import { SideMenu } from '../components';
@ -26,5 +28,6 @@ export default SwitchNavigator({
[ROUTES.SCREENS.SPLASH]: { screen: Splash },
[ROUTES.SCREENS.LOGIN]: { screen: Login },
[ROUTES.SCREENS.PINCODE]: { screen: PinCode },
[ROUTES.SCREENS.STEEM_CONNECT]: { screen: SteemConnect },
[ROUTES.DRAWER.MAIN]: mainNavigation,
});

View File

@ -7,6 +7,7 @@ export default {
LOGIN: `Login${SCREEN_SUFFIX}`,
PINCODE: `PinCode${SCREEN_SUFFIX}`,
HOME: `Home${SCREEN_SUFFIX}`,
STEEM_CONNECT: `SteemConnect${SCREEN_SUFFIX}`,
},
DRAWER: {
MAIN: `Main${DRAWER_SUFFIX}`,

View File

@ -36,6 +36,11 @@ const authMenuItems = [
route: 'Settings',
icon: 'gear',
},
{
name: 'LoginTest',
route: ROUTES.SCREENS.LOGIN,
icon: 'user-o',
},
];
const noAuthMenuItems = [

View File

@ -13,7 +13,10 @@ export const Login = (username, password) => {
let publicKeys;
let privateKeys;
const resultKeys = {
active: null, memo: null, owner: null, posting: null,
active: null,
memo: null,
owner: null,
posting: null,
};
let loginFlag = false;
@ -76,19 +79,32 @@ export const Login = (username, password) => {
};
export const setUserDataWithPinCode = data => new Promise((resolve, reject) => {
let updatedUserData;
const result = getUserDataWithUsername(data.username);
const userData = result[0];
const privateKeys = getPrivateKeys(userData.username, data.password);
const updatedUserData = {
username: userData.username,
authType: 'masterKey',
masterKey: encryptKey(data.password, data.pinCode),
postingKey: encryptKey(privateKeys.posting.toString(), data.pinCode),
activeKey: encryptKey(privateKeys.active.toString(), data.pinCode),
memoKey: encryptKey(privateKeys.memo.toString(), data.pinCode),
};
if (userData.authType === 'masterKey') {
updatedUserData = {
username: userData.username,
authType: 'masterKey',
masterKey: encryptKey(data.password, data.pinCode),
postingKey: encryptKey(privateKeys.posting.toString(), data.pinCode),
activeKey: encryptKey(privateKeys.active.toString(), data.pinCode),
memoKey: encryptKey(privateKeys.memo.toString(), data.pinCode),
};
} else if (userData.authType === 'steemConnect') {
updatedUserData = {
username: userData.name,
authType: 'steemConnect',
accessToken: encryptKey(data.accessToken, data.pinCode),
postingKey: '',
masterKey: '',
activeKey: '',
memoKey: '',
};
}
updateUserData(updatedUserData)
.then(() => {
@ -123,7 +139,7 @@ export const verifyPinCode = data => new Promise((resolve, reject) => {
.then(() => {
resolve();
})
.catch((error) => {
.catch(() => {
reject(new Error('Invalid pin code, please check and try again'));
});
} else {
@ -138,17 +154,15 @@ const getPrivateKeys = (username, password) => ({
owner: dsteem.PrivateKey.fromLogin(username, password, 'owner'),
posting: dsteem.PrivateKey.fromLogin(username, password, 'posting'),
});
export const loginWithSC2 = async (access_token, pinCode) => {
let account;
await steemConnect.setAccessToken(access_token);
account = await steemConnect.me();
export const loginWithSC2 = async (accessToken) => {
await steemConnect.setAccessToken(accessToken);
const account = await steemConnect.me();
return new Promise((resolve, reject) => {
const userData = {
username: account.name,
authType: 'steemConnect',
accessToken: encryptKey(access_token, pinCode),
postingKey: '',
masterKey: '',
activeKey: '',
@ -163,7 +177,7 @@ export const loginWithSC2 = async (access_token, pinCode) => {
.then(() => {
setUserData(userData)
.then(() => {
resolve(true);
resolve({ ...account, accessToken });
})
.catch((error) => {
reject(error);

View File

@ -37,3 +37,8 @@ export const removeAccountData = data => ({
type: REMOVE_ACCOUNT_DATA,
payload: data,
});
export const failedAccount = data => ({
type: FETCH_ACCOUNT_FAIL,
payload: data,
});

View File

@ -30,7 +30,7 @@ export default function (state = initialState, action) {
...state,
isFetching: false,
hasError: true,
errorMessage: action.err,
errorMessage: action.payload,
};
case ADD_NEW_ACCOUNT:
return {

View File

@ -4,6 +4,7 @@ import { Home } from './home';
import { Login } from './login';
import { Profile } from './profile';
import { Notification } from './notification';
import SteemConnect from './steem-connect/steemConnect';
// import Author from './authorProfile';
// import SideMenu from './sideMenuScreen';
@ -24,6 +25,7 @@ export {
Splash,
Profile,
Notification,
SteemConnect,
// Author,
// SideMenu,
// Hot,

View File

@ -1,15 +1,14 @@
import React, { Component } from 'react';
import {
View, Linking, StatusBar, Platform,
View, Linking, StatusBar, Platform, Alert,
} from 'react-native';
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
import ScrollableTabView from '@esteemapp/react-native-scrollable-tab-view';
// Actions
import { addPassiveAccount } from '../../../redux/actions/accountAction';
import { addPassiveAccount, failedAccount } from '../../../redux/actions/accountAction';
import {
login as loginAction,
logout as logoutAction,
} from '../../../redux/actions/applicationActions';
// Internal Components
@ -56,8 +55,10 @@ class LoginScreen extends Component {
navigation.navigate(ROUTES.SCREENS.PINCODE);
}
})
.catch(() => {
dispatch(logoutAction());
.catch((err) => {
// TODO: Change with global error handling
Alert.alert('Error', err.message);
dispatch(failedAccount(err.message));
this.setState({ isLoading: false });
});
};
@ -79,19 +80,8 @@ class LoginScreen extends Component {
};
_loginwithSc2 = () => {
// Navigation.push(this.props.componentId, {
// component: {
// name: 'navigation.eSteem.SteemConnect',
// passProps: {},
// options: {
// topBar: {
// title: {
// text: 'Login via SC2',
// },
// },
// },
// },
// });
const { navigation } = this.props;
navigation.navigate(ROUTES.SCREENS.STEEM_CONNECT);
};
render() {

View File

@ -1,10 +1,10 @@
import React, { Component } from 'react';
import { View, WebView } from 'react-native';
import RNRestart from 'react-native-restart';
import { Navigation } from 'react-native-navigation';
import { loginWithSC2 } from '../../providers/steem/auth';
import { steemConnectOptions } from './config';
import { goToAuthScreens } from '../../navigation';
// Constants
import { default as ROUTES } from '../../constants/routeNames';
export default class SteemConnect extends Component {
constructor(props) {
@ -13,20 +13,22 @@ export default class SteemConnect extends Component {
}
onNavigationStateChange(event) {
let access_token;
let accessToken;
const { navigation } = this.props;
if (event.url.indexOf('?access_token=') > -1) {
this.webview.stopLoading();
try {
access_token = event.url.match(/\?(?:access_token)\=([\S\s]*?)\&/)[1];
accessToken = event.url.match(/\?(?:access_token)\=([\S\s]*?)\&/)[1];
} catch (error) {
console.log(error);
}
loginWithSC2(access_token, 'pinCode')
console.log(accessToken);
loginWithSC2(accessToken, 'pinCode')
.then((result) => {
console.log(result);
if (result) {
// TODO: Handle pinCode and navigate to home page
goToAuthScreens();
navigation.navigate(ROUTES.SCREENS.PINCODE);
} else {
// TODO: Error alert (Toast Message)
console.log('loginWithSC2 error');