Refresh user token on pageload

stable/1.0.x
Alex Gleason 2020-04-29 19:38:24 -05:00
rodzic bc3bb47829
commit f6212cf81b
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
2 zmienionych plików z 27 dodań i 6 usunięć

Wyświetl plik

@ -75,6 +75,27 @@ function createUserToken(username, password) {
grant_type: 'password', grant_type: 'password',
username: username, username: username,
password: password, password: password,
}).then(response => {
dispatch(authLoggedIn(response.data));
});
};
}
export function refreshUserToken() {
return (dispatch, getState) => {
const refreshToken = getState().getIn(['auth', 'user', 'refresh_token']);
const app = getState().getIn(['auth', 'app']);
if (!refreshToken) return dispatch(noOp());
return api(getState, 'app').post('/oauth/token', {
client_id: app.get('client_id'),
client_secret: app.get('client_secret'),
refresh_token: refreshToken,
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
grant_type: 'refresh_token',
}).then(response => {
dispatch(authLoggedIn(response.data));
}); });
}; };
} }
@ -83,8 +104,6 @@ export function logIn(username, password) {
return (dispatch, getState) => { return (dispatch, getState) => {
return dispatch(initAuthApp()).then(() => { return dispatch(initAuthApp()).then(() => {
return dispatch(createUserToken(username, password)); return dispatch(createUserToken(username, password));
}).then(response => {
return dispatch(authLoggedIn(response.data));
}).catch(error => { }).catch(error => {
dispatch(showAlert('Login failed.', 'Invalid username or password.')); dispatch(showAlert('Login failed.', 'Invalid username or password.'));
throw error; throw error;

Wyświetl plik

@ -1,5 +1,6 @@
import api from '../api'; import api from '../api';
import { importFetchedAccount } from './importer'; import { importFetchedAccount } from './importer';
import { refreshUserToken } from './auth';
export const ME_FETCH_REQUEST = 'ME_FETCH_REQUEST'; export const ME_FETCH_REQUEST = 'ME_FETCH_REQUEST';
export const ME_FETCH_SUCCESS = 'ME_FETCH_SUCCESS'; export const ME_FETCH_SUCCESS = 'ME_FETCH_SUCCESS';
@ -18,10 +19,11 @@ export function fetchMe() {
dispatch({ type: ME_FETCH_SKIP }); return; dispatch({ type: ME_FETCH_SKIP }); return;
}; };
dispatch(refreshUserToken()).then(() => {
dispatch(fetchMeRequest()); dispatch(fetchMeRequest());
return api(getState).get('/api/v1/accounts/verify_credentials').then(response => {
api(getState).get('/api/v1/accounts/verify_credentials').then(response => {
dispatch(fetchMeSuccess(response.data)); dispatch(fetchMeSuccess(response.data));
});
}).catch(error => { }).catch(error => {
dispatch(fetchMeFail(error)); dispatch(fetchMeFail(error));
}); });