Handle session expiration. Closes #11

This commit is contained in:
Lucas Verney 2016-08-06 17:20:02 +02:00
parent bb02473b11
commit 9d73eeba15
9 changed files with 68 additions and 35 deletions

View File

@ -87,6 +87,16 @@ export function loginUserFailure(error) {
};
}
export const LOGIN_USER_EXPIRED = "LOGIN_USER_EXPIRED";
export function loginUserExpired(error) {
return {
type: LOGIN_USER_EXPIRED,
payload: {
error: error
}
};
}
export const LOGIN_USER_REQUEST = "LOGIN_USER_REQUEST";
export function loginUserRequest() {
return {

View File

@ -7,8 +7,8 @@ export class RequireAuthentication extends Component {
this.checkAuth(this.props.isAuthenticated);
}
componentWillUpdate () {
this.checkAuth(this.props.isAuthenticated);
componentWillUpdate (newProps) {
this.checkAuth(newProps.isAuthenticated);
}
checkAuth (isAuthenticated) {

View File

@ -5,6 +5,8 @@ import X2JS from "x2js";
import { assembleURLAndParams } from "../utils";
import { i18nRecord } from "../models/i18n";
import { loginUserExpired } from "../actions/auth";
export const API_VERSION = 350001; /** API version to use. */
export const BASE_API_PATH = "/server/xml.server.php"; /** Base API path after endpoint. */
@ -35,7 +37,7 @@ function _parseToJSON (responseText) {
function _checkAPIErrors (jsonData) {
if (jsonData.error) {
return Promise.reject(jsonData.error.__cdata + " (" + jsonData.error.code + ")");
return Promise.reject(jsonData.error);
} else if (!jsonData) {
// No data returned
return Promise.reject(new i18nRecord({
@ -218,9 +220,24 @@ export default store => next => reduxAction => {
},
error => {
if (failureDispatch) {
if (error instanceof Error) {
const errorMessage = error.__cdata + " (" + error._code + ")";
// Error object from the API
if (error._code && error.__cdata) {
if (401 == error._code) {
// This is an error meaning no valid session was
// passed. We must perform a new handshake.
store.dispatch(loginUserExpired(errorMessage));
return;
}
// Else, form error message and continue
error = errorMessage;
}
// Else if exception was thrown
else if (error instanceof Error) {
// Form error message and continue
error = error.message;
}
// Dispatch a failure event
store.dispatch(failureDispatch(error));
}
}

View File

@ -1,6 +1,6 @@
import Cookies from "js-cookie";
import {LOGIN_USER_REQUEST, LOGIN_USER_SUCCESS, LOGIN_USER_FAILURE, LOGOUT_USER} from "../actions";
import { LOGIN_USER_REQUEST, LOGIN_USER_SUCCESS, LOGIN_USER_FAILURE, LOGIN_USER_EXPIRED, LOGOUT_USER } from "../actions";
import { createReducer } from "../utils";
import { i18nRecord } from "../models/i18n";
import { tokenRecord, stateRecord } from "../models/auth";
@ -72,6 +72,12 @@ export default createReducer(initialState, {
"error": payload.error
});
},
[LOGIN_USER_EXPIRED]: (state, payload) => {
return new stateRecord({
"isAuthenticated": false,
"error": payload.error
});
},
[LOGOUT_USER]: () => {
return new stateRecord({
info: new i18nRecord({

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long