mirror of
https://github.com/ecency/ecency-mobile.git
synced 2024-12-26 23:05:00 +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 getNodes = () => serverList.get().then(resp => resp.data.nodes);
|
||||||
|
|
||||||
export const getSCAccessToken = code => api
|
export const getSCAccessToken = code => new Promise((resolve, reject) => {
|
||||||
.post('/api/sc-token-refresh', {
|
console.log('code :', code);
|
||||||
code,
|
api
|
||||||
})
|
.post('/sc-token-refresh', { code })
|
||||||
.then(resp => resp.data);
|
.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) => {
|
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();
|
||||||
@ -118,7 +119,7 @@ export const loginWithSC2 = async (code) => {
|
|||||||
.then(() => {
|
.then(() => {
|
||||||
account.account.username = account.account.name;
|
account.account.username = account.account.name;
|
||||||
updateCurrentUsername(account.account.name);
|
updateCurrentUsername(account.account.name);
|
||||||
resolve({ ...account.account, accessToken });
|
resolve({ ...account.account, accessToken: scTokens.access_token });
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
reject(error);
|
reject(error);
|
||||||
|
@ -504,29 +504,49 @@ 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();
|
||||||
const test = new Date(data.expires_in);
|
|
||||||
console.log('date :', date);
|
console.log('date :', date);
|
||||||
console.log('test :', test);
|
date.setSeconds(date.getSeconds() + data.expires_in);
|
||||||
// TODO: expire date colculate
|
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].accessToken = data.accessToken;
|
scAccount[0].accessToken = data.accessToken;
|
||||||
|
console.log('1');
|
||||||
scAccount[0].refreshToken = data.refreshToken;
|
scAccount[0].refreshToken = data.refreshToken;
|
||||||
scAccount[0].expireDate = data.expireDate;
|
console.log('2');
|
||||||
|
scAccount[0].expireDate = date;
|
||||||
|
console.log('scAccount :', scAccount);
|
||||||
resolve();
|
resolve();
|
||||||
} else {
|
} else {
|
||||||
const account = {
|
const account = {
|
||||||
username: data.username,
|
username: data.username,
|
||||||
accessToken: data.access_token,
|
accessToken: data.access_token,
|
||||||
refreshToken: data.refresh_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();
|
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) {
|
} catch (error) {
|
||||||
reject(error);
|
reject(error);
|
||||||
}
|
}
|
||||||
|
@ -20,9 +20,12 @@ 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 } from '../../../providers/esteem/esteem';
|
import { setPushToken, getSCAccessToken } from '../../../providers/esteem/esteem';
|
||||||
import { switchAccount } from '../../../providers/steem/auth';
|
import { switchAccount } from '../../../providers/steem/auth';
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
@ -98,13 +101,32 @@ class ApplicationContainer extends Component {
|
|||||||
await getAuthStatus().then((res) => {
|
await getAuthStatus().then((res) => {
|
||||||
({ currentUsername } = res);
|
({ currentUsername } = res);
|
||||||
if (res) {
|
if (res) {
|
||||||
getUserData().then((userData) => {
|
console.log('res :', res);
|
||||||
|
getUserData().then(async (userData) => {
|
||||||
if (userData.length > 0) {
|
if (userData.length > 0) {
|
||||||
realmData = userData;
|
realmData = userData;
|
||||||
|
|
||||||
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -209,8 +231,7 @@ class ApplicationContainer extends Component {
|
|||||||
dispatch(removeOtherAccount(currentAccountUsername));
|
dispatch(removeOtherAccount(currentAccountUsername));
|
||||||
dispatch(logoutDone());
|
dispatch(logoutDone());
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {});
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
_switchAccount = (targetAccountUsername) => {
|
_switchAccount = (targetAccountUsername) => {
|
||||||
|
@ -2,5 +2,5 @@ export const steemConnectOptions = {
|
|||||||
base_url: 'https://steemconnect.com/oauth2/authorize',
|
base_url: 'https://steemconnect.com/oauth2/authorize',
|
||||||
client_id: 'esteem-app',
|
client_id: 'esteem-app',
|
||||||
redirect_uri: 'http://127.0.0.1:3415/', // http://127.0.0.1:3415
|
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;
|
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 {
|
||||||
code = event.url.match(/\?(?:code)\=([\S\s]*?)\&/)[1];
|
code = event.url.match(/code=([^&]*)/);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// 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();
|
||||||
loginWithSC2(code, 'pinCode')
|
loginWithSC2(code[1])
|
||||||
.then((result) => {
|
.then((result) => {
|
||||||
if (result) {
|
if (result) {
|
||||||
dispatch(updateCurrentAccount({ ...result }));
|
dispatch(updateCurrentAccount({ ...result }));
|
||||||
@ -64,7 +68,7 @@ class SteemConnect extends PureComponent {
|
|||||||
steemConnectOptions.client_id
|
steemConnectOptions.client_id
|
||||||
}&redirect_uri=${encodeURIComponent(
|
}&redirect_uri=${encodeURIComponent(
|
||||||
steemConnectOptions.redirect_uri,
|
steemConnectOptions.redirect_uri,
|
||||||
)}&scope=${encodeURIComponent(steemConnectOptions.scope)}`,
|
)}&response_type=code&scope=${encodeURIComponent(steemConnectOptions.scope)}`,
|
||||||
}}
|
}}
|
||||||
onNavigationStateChange={this._onNavigationStateChange}
|
onNavigationStateChange={this._onNavigationStateChange}
|
||||||
ref={(ref) => {
|
ref={(ref) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user