mirror of
https://github.com/ecency/ecency-mobile.git
synced 2024-12-25 22:33:52 +03:00
Working on sc refresh token
This commit is contained in:
parent
f0b5550308
commit
be4221a145
@ -196,8 +196,10 @@ export const uploadImage = (file) => {
|
||||
|
||||
export const getNodes = () => serverList.get().then(resp => resp.data.nodes);
|
||||
|
||||
export const getSCAccessToken = code => api
|
||||
.post('/api/sc-token-refresh', {
|
||||
code,
|
||||
})
|
||||
.then(resp => resp.data);
|
||||
export const getSCAccessToken = code => new Promise((resolve, reject) => {
|
||||
console.log('code :', code);
|
||||
api
|
||||
.post('/sc-token-refresh', { code })
|
||||
.then(resp => resolve(resp.data))
|
||||
.catch(err => console.log('err :', err, err.response, err.request));
|
||||
});
|
||||
|
@ -85,6 +85,7 @@ 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();
|
||||
@ -118,7 +119,7 @@ export const loginWithSC2 = async (code) => {
|
||||
.then(() => {
|
||||
account.account.username = account.account.name;
|
||||
updateCurrentUsername(account.account.name);
|
||||
resolve({ ...account.account, accessToken });
|
||||
resolve({ ...account.account, accessToken: scTokens.access_token });
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
|
@ -504,29 +504,49 @@ 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();
|
||||
const test = new Date(data.expires_in);
|
||||
console.log('date :', date);
|
||||
console.log('test :', test);
|
||||
// TODO: expire date colculate
|
||||
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;
|
||||
scAccount[0].expireDate = data.expireDate;
|
||||
console.log('2');
|
||||
scAccount[0].expireDate = date;
|
||||
console.log('scAccount :', scAccount);
|
||||
resolve();
|
||||
} else {
|
||||
const account = {
|
||||
username: data.username,
|
||||
accessToken: data.access_token,
|
||||
refreshToken: data.refresh_token,
|
||||
expireDate: data.expires_in,
|
||||
expireDate: date.toString(),
|
||||
};
|
||||
realm.create(APPLICATION_SCHEMA, { ...account });
|
||||
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));
|
||||
if (Array.from(scAccount).length > 0) {
|
||||
resolve(scAccount[0]);
|
||||
} else {
|
||||
resolve(false);
|
||||
}
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
|
@ -20,9 +20,12 @@ import {
|
||||
removeUserData,
|
||||
setPushTokenSaved,
|
||||
getUserDataWithUsername,
|
||||
getSCAccount,
|
||||
setSCAccount,
|
||||
updateUserData,
|
||||
} from '../../../realm/realm';
|
||||
import { getUser } from '../../../providers/steem/dsteem';
|
||||
import { setPushToken } from '../../../providers/esteem/esteem';
|
||||
import { setPushToken, getSCAccessToken } from '../../../providers/esteem/esteem';
|
||||
import { switchAccount } from '../../../providers/steem/auth';
|
||||
|
||||
// Actions
|
||||
@ -98,13 +101,32 @@ class ApplicationContainer extends Component {
|
||||
await getAuthStatus().then((res) => {
|
||||
({ currentUsername } = res);
|
||||
if (res) {
|
||||
getUserData().then((userData) => {
|
||||
console.log('res :', res);
|
||||
getUserData().then(async (userData) => {
|
||||
if (userData.length > 0) {
|
||||
realmData = userData;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -209,8 +231,7 @@ class ApplicationContainer extends Component {
|
||||
dispatch(removeOtherAccount(currentAccountUsername));
|
||||
dispatch(logoutDone());
|
||||
})
|
||||
.catch(() => {
|
||||
});
|
||||
.catch(() => {});
|
||||
};
|
||||
|
||||
_switchAccount = (targetAccountUsername) => {
|
||||
|
@ -2,5 +2,5 @@ export const steemConnectOptions = {
|
||||
base_url: 'https://steemconnect.com/oauth2/authorize',
|
||||
client_id: 'esteem-app',
|
||||
redirect_uri: 'http://127.0.0.1:3415/', // http://127.0.0.1:3415
|
||||
scope: 'vote,comment,delete_comment,comment_options,custom_json,claim_reward_balance',
|
||||
scope: 'vote,comment,delete_comment,comment_options,custom_json,claim_reward_balance,offline',
|
||||
};
|
||||
|
@ -24,17 +24,21 @@ 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 {
|
||||
code = event.url.match(/\?(?:code)\=([\S\s]*?)\&/)[1];
|
||||
code = event.url.match(/code=([^&]*)/);
|
||||
} catch (error) {
|
||||
// TODO: return
|
||||
}
|
||||
|
||||
console.log('code :', code[1]);
|
||||
console.log('event.url :', event.url);
|
||||
if (!isLoading) {
|
||||
this.setState({ isLoading: true });
|
||||
handleOnModalClose();
|
||||
loginWithSC2(code, 'pinCode')
|
||||
loginWithSC2(code[1])
|
||||
.then((result) => {
|
||||
if (result) {
|
||||
dispatch(updateCurrentAccount({ ...result }));
|
||||
@ -64,7 +68,7 @@ class SteemConnect extends PureComponent {
|
||||
steemConnectOptions.client_id
|
||||
}&redirect_uri=${encodeURIComponent(
|
||||
steemConnectOptions.redirect_uri,
|
||||
)}&scope=${encodeURIComponent(steemConnectOptions.scope)}`,
|
||||
)}&response_type=code&scope=${encodeURIComponent(steemConnectOptions.scope)}`,
|
||||
}}
|
||||
onNavigationStateChange={this._onNavigationStateChange}
|
||||
ref={(ref) => {
|
||||
|
Loading…
Reference in New Issue
Block a user