Merge "Fix Logout and authentication loop"

This commit is contained in:
Jenkins 2017-07-19 21:25:16 +00:00 committed by Gerrit Code Review
commit 3cb948f160
7 changed files with 29 additions and 19 deletions

View File

@ -22,8 +22,8 @@ import store from '../../js/store';
describe('utility functions', () => {
const appState = {
login: new InitialLoginState({
tokenId: 123456,
token: Map({
id: 123456,
project: Map({
id: 778899
}),

View File

@ -30,10 +30,8 @@ export default {
KeystoneApiService.authenticateUserViaToken(keystoneAuthTokenId)
.then(result => {
const tokenId = result.request.getResponseHeader('X-Subject-Token');
let response = result.response;
response.token.id = tokenId;
cookie.save('keystoneAuthTokenId', tokenId, { path: '/' });
dispatch(this.userAuthSuccess(response.token));
dispatch(this.userAuthSuccess(tokenId, result.response.token));
})
.catch(error => {
logger.error(
@ -53,10 +51,8 @@ export default {
KeystoneApiService.authenticateUser(formData.username, formData.password)
.then(result => {
const tokenId = result.request.getResponseHeader('X-Subject-Token');
let response = result.response;
response.token.id = tokenId;
cookie.save('keystoneAuthTokenId', tokenId, { path: '/' });
dispatch(this.userAuthSuccess(response.token));
dispatch(this.userAuthSuccess(tokenId, result.response.token));
})
.catch(error => {
logger.error(
@ -91,10 +87,10 @@ export default {
};
},
userAuthSuccess(token) {
userAuthSuccess(tokenId, token) {
return {
type: LoginConstants.USER_AUTH_SUCCESS,
payload: fromJS(token)
payload: { token, tokenId }
};
},

View File

@ -15,7 +15,6 @@
*/
import { connect } from 'react-redux';
import cookie from 'react-cookie';
import { defineMessages, injectIntl } from 'react-intl';
import PropTypes from 'prop-types';
import React from 'react';
@ -48,16 +47,19 @@ class UserAuthenticator extends React.Component {
}
checkAuth(props) {
const { isAuthenticated, isAuthenticating } = props;
const keystoneAuthTokenId = cookie.load('keystoneAuthTokenId');
const { isAuthenticated, isAuthenticating, keystoneAuthTokenId } = props;
if (!isAuthenticated && !isAuthenticating && keystoneAuthTokenId) {
this.props.authenticateUserViaToken(keystoneAuthTokenId);
}
}
render() {
const { isAuthenticating, isAuthenticated, location } = this.props;
const keystoneAuthTokenId = cookie.load('keystoneAuthTokenId');
const {
isAuthenticating,
isAuthenticated,
keystoneAuthTokenId,
location
} = this.props;
if (isAuthenticated || isAuthenticating || keystoneAuthTokenId) {
return (
@ -84,13 +86,15 @@ UserAuthenticator.propTypes = {
intl: PropTypes.object,
isAuthenticated: PropTypes.bool.isRequired,
isAuthenticating: PropTypes.bool.isRequired,
keystoneAuthTokenId: PropTypes.string,
location: PropTypes.object.isRequired
};
const mapStateToProps = state => {
return {
isAuthenticated: state.login.isAuthenticated,
isAuthenticating: state.login.isAuthenticating
isAuthenticating: state.login.isAuthenticating,
keystoneAuthTokenId: state.login.tokenId
};
};

View File

@ -18,6 +18,7 @@ import { List, Map, Record } from 'immutable';
export const InitialLoginState = Record({
token: Map(),
tokenId: undefined,
loginForm: Map({
formErrors: List(),
formFieldErrors: Map()

View File

@ -13,6 +13,7 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
import { fromJS } from 'immutable';
import LoginConstants from '../constants/LoginConstants';
import { InitialLoginState } from '../immutableRecords/login';
@ -26,7 +27,8 @@ export default function loginReducer(state = initialState, action) {
case LoginConstants.USER_AUTH_SUCCESS:
return state
.set('token', action.payload)
.set('token', fromJS(action.payload.token))
.set('tokenId', action.payload.tokenId)
.set('isAuthenticating', false)
.set('isAuthenticated', true);
@ -34,7 +36,9 @@ export default function loginReducer(state = initialState, action) {
return state
.set('loginForm', action.payload)
.set('isAuthenticating', false)
.set('isAuthenticated', false);
.set('isAuthenticated', false)
.delete('tokenId')
.delete('token');
case LoginConstants.LOGOUT_USER_SUCCESS:
return initialState;

View File

@ -52,7 +52,7 @@ function getFromServiceCatalog(serviceName, urlType) {
* Returns Keystone Auth Token ID
*/
export function getAuthTokenId() {
return store.getState().login.getIn(['token', 'id']);
return store.getState().login.tokenId;
}
export function getProjectId() {

View File

@ -14,19 +14,24 @@
* under the License.
*/
import { createStore, applyMiddleware } from 'redux';
import { applyMiddleware, createStore } from 'redux';
import cookie from 'react-cookie';
import thunkMiddleware from 'redux-thunk';
import createLogger from 'redux-logger';
import logger from './services/logger';
import appReducer from './reducers/appReducer';
import { InitialPlanState } from './immutableRecords/plans';
import { InitialLoginState } from './immutableRecords/login';
import { getIntl } from './selectors/i18n';
const hydrateStore = () => {
return {
plans: new InitialPlanState({
currentPlanName: getStoredPlanName()
}),
login: new InitialLoginState({
tokenId: cookie.load('keystoneAuthTokenId')
})
};
};