Completed steem connect token refresh feature

This commit is contained in:
Mustafa Buyukcelebi 2019-01-03 00:53:13 +03:00
parent be4221a145
commit e84f2d7341
5 changed files with 19 additions and 46 deletions

View File

@ -196,10 +196,8 @@ export const uploadImage = (file) => {
export const getNodes = () => serverList.get().then(resp => resp.data.nodes);
export const getSCAccessToken = code => new Promise((resolve, reject) => {
console.log('code :', code);
export const getSCAccessToken = code => new Promise((resolve) => {
api
.post('/sc-token-refresh', { code })
.then(resp => resolve(resp.data))
.catch(err => console.log('err :', err, err.response, err.request));
.then(resp => resolve(resp.data));
});

View File

@ -8,6 +8,7 @@ import {
updateCurrentUsername,
getUserData,
setSCAccount,
getSCAccount,
} from '../../realm/realm';
import { encryptKey, decryptKey } from '../../utils/crypto';
import steemConnect from './steemConnectAPI';
@ -85,7 +86,6 @@ export const login = async (username, password) => {
export const loginWithSC2 = async (code) => {
const scTokens = await getSCAccessToken(code);
console.log('scTokens :', scTokens);
await setSCAccount(scTokens);
await steemConnect.setAccessToken(scTokens.access_token);
const account = await steemConnect.me();
@ -201,6 +201,18 @@ export const verifyPinCode = async (data) => {
} catch (error) {
return Promise.reject(new Error('Invalid pin code, please check and try again'));
}
const scAccount = await getSCAccount(userData.username);
const now = new Date();
const expireDate = new Date(scAccount.expireDate);
if (now >= expireDate) {
const newSCAccountData = await getSCAccessToken(scAccount.refreshToken);
await setSCAccount(newSCAccountData);
accessToken = newSCAccountData.access_token;
await updateUserData(
{ ...userData, accessToken: encryptKey(accessToken, data.pinCode) },
);
}
await steemConnect.setAccessToken(accessToken);
account = await steemConnect.me();
if (account) {

View File

@ -26,7 +26,6 @@ const scAccounts = {
name: SC_ACCOUNTS,
properties: {
username: { type: 'string', default: null },
accessToken: { type: 'string', default: null },
refreshToken: { type: 'string', default: null },
expireDate: { type: 'string', default: null },
},
@ -504,21 +503,13 @@ export const setExistUser = existUser => new Promise((resolve, reject) => {
export const setSCAccount = data => new Promise((resolve, reject) => {
try {
console.log('data :', data);
const scAccount = realm.objects(SC_ACCOUNTS).filtered('username = $0', data.username);
const date = new Date();
console.log('date :', date);
date.setSeconds(date.getSeconds() + data.expires_in);
console.log('date :', date);
realm.write(() => {
if (Array.from(scAccount).length > 0) {
console.log('scAccount :', scAccount[0]);
scAccount[0].accessToken = data.accessToken;
console.log('1');
scAccount[0].refreshToken = data.refreshToken;
console.log('2');
scAccount[0].expireDate = date;
console.log('scAccount :', scAccount);
scAccount[0].refreshToken = data.refresh_token;
scAccount[0].expireDate = date.toString();
resolve();
} else {
const account = {
@ -527,21 +518,18 @@ export const setSCAccount = data => new Promise((resolve, reject) => {
refreshToken: data.refresh_token,
expireDate: date.toString(),
};
console.log('account :', account);
realm.create(SC_ACCOUNTS, { ...account });
resolve();
}
});
} catch (error) {
console.log('error :', error);
reject(error);
}
});
export const getSCAccount = username => new Promise((resolve, reject) => {
try {
const scAccount = realm.objects(SC_ACCOUNTS);
console.log('scAccount :', Array.from(scAccount));
const scAccount = realm.objects(SC_ACCOUNTS).filtered('username = $0', username);
if (Array.from(scAccount).length > 0) {
resolve(scAccount[0]);
} else {

View File

@ -20,12 +20,9 @@ import {
removeUserData,
setPushTokenSaved,
getUserDataWithUsername,
getSCAccount,
setSCAccount,
updateUserData,
} from '../../../realm/realm';
import { getUser } from '../../../providers/steem/dsteem';
import { setPushToken, getSCAccessToken } from '../../../providers/esteem/esteem';
import { setPushToken } from '../../../providers/esteem/esteem';
import { switchAccount } from '../../../providers/steem/auth';
// Actions
@ -101,7 +98,6 @@ class ApplicationContainer extends Component {
await getAuthStatus().then((res) => {
({ currentUsername } = res);
if (res) {
console.log('res :', res);
getUserData().then(async (userData) => {
if (userData.length > 0) {
realmData = userData;
@ -109,24 +105,6 @@ class ApplicationContainer extends Component {
userData.forEach((accountData) => {
dispatch(addOtherAccount({ username: accountData.username }));
});
console.log('userData :', userData[0]);
if (userData[0].authType === 'steemConnect') {
const scAccount = await getSCAccount(userData[0].username);
console.log('scAccount :', scAccount);
const now = new Date();
const expireDate = new Date(scAccount.expireDate);
console.log('now :', now);
console.log('expireDate :', expireDate);
console.log('now >= expireDate :', now <= expireDate);
if (now >= expireDate) {
const newSCAccountData = await getSCAccessToken(scAccount.refreshToken);
console.log('newSCAccountData :', newSCAccountData);
await setSCAccount(newSCAccountData);
realmData = { ...userData[0], accessToken: scAccount.accessToken };
console.log('realmData :', realmData);
await updateUserData(realmData);
}
}
}
});
}

View File

@ -24,7 +24,6 @@ class SteemConnect extends PureComponent {
let code;
const { dispatch, setPinCodeState, handleOnModalClose } = this.props;
const { isLoading } = this.state;
console.log('event :', event);
if (event.url.indexOf('?code=') > -1) {
this.webview.stopLoading();
try {
@ -33,8 +32,6 @@ class SteemConnect extends PureComponent {
// TODO: return
}
console.log('code :', code[1]);
console.log('event.url :', event.url);
if (!isLoading) {
this.setState({ isLoading: true });
handleOnModalClose();