Merge pull request #1767 from coralproject/token-fix

Token Handling
This commit is contained in:
Kim Gardner
2018-07-27 21:25:07 +01:00
committed by GitHub
3 changed files with 16 additions and 13 deletions
+3 -3
View File
@@ -17,19 +17,19 @@ export const checkLogin = () => (
if (!result.user) {
cleanAuthData(localStorage);
dispatch(checkLoginSuccess(null));
client.resetWebsocket();
return;
}
// Reset the websocket.
client.resetWebsocket();
dispatch(checkLoginSuccess(result.user));
pym.sendMessage('coral-auth-changed', JSON.stringify(result.user));
client.resetWebsocket();
})
.catch(error => {
if (error.status && error.status === 401 && localStorage) {
// Unauthorized.
cleanAuthData(localStorage);
client.resetWebsocket();
} else {
console.error(error);
}
+9 -2
View File
@@ -3,9 +3,11 @@ import {
ENABLE_PLUGINS_DEBUG,
DISABLE_PLUGINS_DEBUG,
} from '../constants/config';
import { LOGOUT } from '../constants/auth';
import { LOGOUT, SET_AUTH_TOKEN } from '../constants/auth';
const initialState = {};
const initialState = {
auth_token: null,
};
export default function config(state = initialState, action) {
switch (action.type) {
@@ -30,6 +32,11 @@ export default function config(state = initialState, action) {
...state,
auth_token: null,
};
case SET_AUTH_TOKEN:
return {
...state,
auth_token: action.token || null,
};
case MERGE_CONFIG:
return {
...state,
+4 -8
View File
@@ -42,17 +42,13 @@ const getAuthToken = (store, storage) => {
let state = store.getState();
if (state.config && state.config.auth_token) {
// if an auth_token exists in config, use it.
return state.config.auth_token;
// If the embed is called with `embed.login(token)`, and the browser is not
// capable of storing the token in localStorage, then we would have
// persisted it to the redux state.
return state.config.auth_token || state.auth.token;
} else if (!bowser.safari && !bowser.ios && storage) {
// Use local storage auth tokens where there's a stable api.
return storage.getItem('token');
} else if (state.auth && state.auth.token) {
// Use the redux token state if the remaining methods fall out. If the embed
// is called with `embed.login(token)`, and the browser is not capable of
// storing the token in localStorage, then we would have persisted it to the
// redux state.
return state.auth.token;
}
return null;