mirror of
https://github.com/ecency/ecency-mobile.git
synced 2025-01-04 20:15:51 +03:00
Completed steem connect token refresh feature
This commit is contained in:
parent
be4221a145
commit
e84f2d7341
@ -196,10 +196,8 @@ export const uploadImage = (file) => {
|
|||||||
|
|
||||||
export const getNodes = () => serverList.get().then(resp => resp.data.nodes);
|
export const getNodes = () => serverList.get().then(resp => resp.data.nodes);
|
||||||
|
|
||||||
export const getSCAccessToken = code => new Promise((resolve, reject) => {
|
export const getSCAccessToken = code => new Promise((resolve) => {
|
||||||
console.log('code :', code);
|
|
||||||
api
|
api
|
||||||
.post('/sc-token-refresh', { code })
|
.post('/sc-token-refresh', { code })
|
||||||
.then(resp => resolve(resp.data))
|
.then(resp => resolve(resp.data));
|
||||||
.catch(err => console.log('err :', err, err.response, err.request));
|
|
||||||
});
|
});
|
||||||
|
@ -8,6 +8,7 @@ import {
|
|||||||
updateCurrentUsername,
|
updateCurrentUsername,
|
||||||
getUserData,
|
getUserData,
|
||||||
setSCAccount,
|
setSCAccount,
|
||||||
|
getSCAccount,
|
||||||
} from '../../realm/realm';
|
} from '../../realm/realm';
|
||||||
import { encryptKey, decryptKey } from '../../utils/crypto';
|
import { encryptKey, decryptKey } from '../../utils/crypto';
|
||||||
import steemConnect from './steemConnectAPI';
|
import steemConnect from './steemConnectAPI';
|
||||||
@ -85,7 +86,6 @@ export const login = async (username, password) => {
|
|||||||
|
|
||||||
export const loginWithSC2 = async (code) => {
|
export const loginWithSC2 = async (code) => {
|
||||||
const scTokens = await getSCAccessToken(code);
|
const scTokens = await getSCAccessToken(code);
|
||||||
console.log('scTokens :', scTokens);
|
|
||||||
await setSCAccount(scTokens);
|
await setSCAccount(scTokens);
|
||||||
await steemConnect.setAccessToken(scTokens.access_token);
|
await steemConnect.setAccessToken(scTokens.access_token);
|
||||||
const account = await steemConnect.me();
|
const account = await steemConnect.me();
|
||||||
@ -201,6 +201,18 @@ export const verifyPinCode = async (data) => {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
return Promise.reject(new Error('Invalid pin code, please check and try again'));
|
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);
|
await steemConnect.setAccessToken(accessToken);
|
||||||
account = await steemConnect.me();
|
account = await steemConnect.me();
|
||||||
if (account) {
|
if (account) {
|
||||||
|
@ -26,7 +26,6 @@ const scAccounts = {
|
|||||||
name: SC_ACCOUNTS,
|
name: SC_ACCOUNTS,
|
||||||
properties: {
|
properties: {
|
||||||
username: { type: 'string', default: null },
|
username: { type: 'string', default: null },
|
||||||
accessToken: { type: 'string', default: null },
|
|
||||||
refreshToken: { type: 'string', default: null },
|
refreshToken: { type: 'string', default: null },
|
||||||
expireDate: { 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) => {
|
export const setSCAccount = data => new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
console.log('data :', data);
|
|
||||||
const scAccount = realm.objects(SC_ACCOUNTS).filtered('username = $0', data.username);
|
const scAccount = realm.objects(SC_ACCOUNTS).filtered('username = $0', data.username);
|
||||||
const date = new Date();
|
const date = new Date();
|
||||||
console.log('date :', date);
|
|
||||||
date.setSeconds(date.getSeconds() + data.expires_in);
|
date.setSeconds(date.getSeconds() + data.expires_in);
|
||||||
console.log('date :', date);
|
|
||||||
realm.write(() => {
|
realm.write(() => {
|
||||||
if (Array.from(scAccount).length > 0) {
|
if (Array.from(scAccount).length > 0) {
|
||||||
console.log('scAccount :', scAccount[0]);
|
scAccount[0].refreshToken = data.refresh_token;
|
||||||
scAccount[0].accessToken = data.accessToken;
|
scAccount[0].expireDate = date.toString();
|
||||||
console.log('1');
|
|
||||||
scAccount[0].refreshToken = data.refreshToken;
|
|
||||||
console.log('2');
|
|
||||||
scAccount[0].expireDate = date;
|
|
||||||
console.log('scAccount :', scAccount);
|
|
||||||
resolve();
|
resolve();
|
||||||
} else {
|
} else {
|
||||||
const account = {
|
const account = {
|
||||||
@ -527,21 +518,18 @@ export const setSCAccount = data => new Promise((resolve, reject) => {
|
|||||||
refreshToken: data.refresh_token,
|
refreshToken: data.refresh_token,
|
||||||
expireDate: date.toString(),
|
expireDate: date.toString(),
|
||||||
};
|
};
|
||||||
console.log('account :', account);
|
|
||||||
realm.create(SC_ACCOUNTS, { ...account });
|
realm.create(SC_ACCOUNTS, { ...account });
|
||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('error :', error);
|
|
||||||
reject(error);
|
reject(error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export const getSCAccount = username => new Promise((resolve, reject) => {
|
export const getSCAccount = username => new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
const scAccount = realm.objects(SC_ACCOUNTS);
|
const scAccount = realm.objects(SC_ACCOUNTS).filtered('username = $0', username);
|
||||||
console.log('scAccount :', Array.from(scAccount));
|
|
||||||
if (Array.from(scAccount).length > 0) {
|
if (Array.from(scAccount).length > 0) {
|
||||||
resolve(scAccount[0]);
|
resolve(scAccount[0]);
|
||||||
} else {
|
} else {
|
||||||
|
@ -20,12 +20,9 @@ import {
|
|||||||
removeUserData,
|
removeUserData,
|
||||||
setPushTokenSaved,
|
setPushTokenSaved,
|
||||||
getUserDataWithUsername,
|
getUserDataWithUsername,
|
||||||
getSCAccount,
|
|
||||||
setSCAccount,
|
|
||||||
updateUserData,
|
|
||||||
} from '../../../realm/realm';
|
} from '../../../realm/realm';
|
||||||
import { getUser } from '../../../providers/steem/dsteem';
|
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';
|
import { switchAccount } from '../../../providers/steem/auth';
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
@ -101,7 +98,6 @@ class ApplicationContainer extends Component {
|
|||||||
await getAuthStatus().then((res) => {
|
await getAuthStatus().then((res) => {
|
||||||
({ currentUsername } = res);
|
({ currentUsername } = res);
|
||||||
if (res) {
|
if (res) {
|
||||||
console.log('res :', res);
|
|
||||||
getUserData().then(async (userData) => {
|
getUserData().then(async (userData) => {
|
||||||
if (userData.length > 0) {
|
if (userData.length > 0) {
|
||||||
realmData = userData;
|
realmData = userData;
|
||||||
@ -109,24 +105,6 @@ class ApplicationContainer extends Component {
|
|||||||
userData.forEach((accountData) => {
|
userData.forEach((accountData) => {
|
||||||
dispatch(addOtherAccount({ username: accountData.username }));
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,6 @@ class SteemConnect extends PureComponent {
|
|||||||
let code;
|
let code;
|
||||||
const { dispatch, setPinCodeState, handleOnModalClose } = this.props;
|
const { dispatch, setPinCodeState, handleOnModalClose } = this.props;
|
||||||
const { isLoading } = this.state;
|
const { isLoading } = this.state;
|
||||||
console.log('event :', event);
|
|
||||||
if (event.url.indexOf('?code=') > -1) {
|
if (event.url.indexOf('?code=') > -1) {
|
||||||
this.webview.stopLoading();
|
this.webview.stopLoading();
|
||||||
try {
|
try {
|
||||||
@ -33,8 +32,6 @@ class SteemConnect extends PureComponent {
|
|||||||
// TODO: return
|
// TODO: return
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('code :', code[1]);
|
|
||||||
console.log('event.url :', event.url);
|
|
||||||
if (!isLoading) {
|
if (!isLoading) {
|
||||||
this.setState({ isLoading: true });
|
this.setState({ isLoading: true });
|
||||||
handleOnModalClose();
|
handleOnModalClose();
|
||||||
|
Loading…
Reference in New Issue
Block a user