2016-08-10 21:36:11 +02:00
|
|
|
/**
|
|
|
|
* This implements the auth reducer, storing and updating authentication state.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// NPM imports
|
2016-07-07 23:23:18 +02:00
|
|
|
import Cookies from "js-cookie";
|
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
// Local imports
|
2016-07-07 23:23:18 +02:00
|
|
|
import { createReducer } from "../utils";
|
2016-08-10 21:36:11 +02:00
|
|
|
|
|
|
|
// Models
|
2016-08-01 00:26:52 +02:00
|
|
|
import { i18nRecord } from "../models/i18n";
|
|
|
|
import { tokenRecord, stateRecord } from "../models/auth";
|
2016-07-07 23:23:18 +02:00
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
// Actions
|
|
|
|
import {
|
|
|
|
LOGIN_USER_REQUEST,
|
|
|
|
LOGIN_USER_SUCCESS,
|
|
|
|
LOGIN_USER_FAILURE,
|
|
|
|
LOGIN_USER_EXPIRED,
|
|
|
|
LOGOUT_USER } from "../actions";
|
|
|
|
|
|
|
|
|
2016-08-01 00:26:52 +02:00
|
|
|
/**
|
2016-08-10 21:36:11 +02:00
|
|
|
* Initial state, load data from cookies if set
|
2016-08-01 00:26:52 +02:00
|
|
|
*/
|
|
|
|
var initialState = new stateRecord();
|
2016-08-10 21:36:11 +02:00
|
|
|
// Get token
|
2016-08-01 00:26:52 +02:00
|
|
|
const initialToken = Cookies.getJSON("token");
|
2016-07-07 23:23:18 +02:00
|
|
|
if (initialToken) {
|
|
|
|
initialToken.expires = new Date(initialToken.expires);
|
2016-08-01 00:26:52 +02:00
|
|
|
initialState = initialState.set(
|
|
|
|
"token",
|
|
|
|
new tokenRecord({ token: initialToken.token, expires: new Date(initialToken.expires) })
|
|
|
|
);
|
|
|
|
}
|
2016-08-10 21:36:11 +02:00
|
|
|
// Get username
|
2016-08-01 00:26:52 +02:00
|
|
|
const initialUsername = Cookies.get("username");
|
|
|
|
if (initialUsername) {
|
|
|
|
initialState = initialState.set(
|
|
|
|
"username",
|
|
|
|
initialUsername
|
|
|
|
);
|
|
|
|
}
|
2016-08-10 21:36:11 +02:00
|
|
|
// Get endpoint
|
2016-08-01 00:26:52 +02:00
|
|
|
const initialEndpoint = Cookies.get("endpoint");
|
|
|
|
if (initialEndpoint) {
|
|
|
|
initialState = initialState.set(
|
|
|
|
"endpoint",
|
|
|
|
initialEndpoint
|
|
|
|
);
|
|
|
|
}
|
2016-08-10 21:36:11 +02:00
|
|
|
// Set remember me
|
2016-08-01 00:26:52 +02:00
|
|
|
if (initialUsername && initialEndpoint) {
|
|
|
|
initialState = initialState.set(
|
|
|
|
"rememberMe",
|
|
|
|
true
|
|
|
|
);
|
2016-07-07 23:23:18 +02:00
|
|
|
}
|
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
|
2016-08-01 00:26:52 +02:00
|
|
|
/**
|
|
|
|
* Reducers
|
|
|
|
*/
|
2016-07-07 23:23:18 +02:00
|
|
|
export default createReducer(initialState, {
|
2016-08-01 00:26:52 +02:00
|
|
|
[LOGIN_USER_REQUEST]: () => {
|
|
|
|
return new stateRecord({
|
2016-07-07 23:23:18 +02:00
|
|
|
isAuthenticating: true,
|
2016-08-01 00:26:52 +02:00
|
|
|
info: new i18nRecord({
|
2016-07-31 01:05:08 +02:00
|
|
|
id: "app.login.connecting",
|
2016-08-10 23:50:23 +02:00
|
|
|
values: {},
|
|
|
|
}),
|
2016-07-07 23:23:18 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
[LOGIN_USER_SUCCESS]: (state, payload) => {
|
2016-08-01 00:26:52 +02:00
|
|
|
return new stateRecord({
|
|
|
|
"isAuthenticated": true,
|
|
|
|
"token": new tokenRecord(payload.token),
|
|
|
|
"username": payload.username,
|
|
|
|
"endpoint": payload.endpoint,
|
|
|
|
"rememberMe": payload.rememberMe,
|
|
|
|
"info": new i18nRecord({
|
2016-07-31 01:05:08 +02:00
|
|
|
id: "app.login.success",
|
2016-08-10 23:50:23 +02:00
|
|
|
values: {username: payload.username},
|
2016-08-01 00:26:52 +02:00
|
|
|
}),
|
2016-08-10 23:50:23 +02:00
|
|
|
"timerID": payload.timerID,
|
2016-07-07 23:23:18 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
[LOGIN_USER_FAILURE]: (state, payload) => {
|
2016-08-01 00:26:52 +02:00
|
|
|
return new stateRecord({
|
2016-08-10 23:50:23 +02:00
|
|
|
"error": payload.error,
|
2016-07-07 23:23:18 +02:00
|
|
|
});
|
|
|
|
},
|
2016-08-06 17:20:02 +02:00
|
|
|
[LOGIN_USER_EXPIRED]: (state, payload) => {
|
|
|
|
return new stateRecord({
|
|
|
|
"isAuthenticated": false,
|
2016-08-10 23:50:23 +02:00
|
|
|
"error": payload.error,
|
2016-08-06 17:20:02 +02:00
|
|
|
});
|
|
|
|
},
|
2016-08-01 00:26:52 +02:00
|
|
|
[LOGOUT_USER]: () => {
|
|
|
|
return new stateRecord({
|
|
|
|
info: new i18nRecord({
|
2016-07-31 01:05:08 +02:00
|
|
|
id: "app.login.byebye",
|
2016-08-10 23:50:23 +02:00
|
|
|
values: {},
|
|
|
|
}),
|
2016-07-07 23:23:18 +02:00
|
|
|
});
|
2016-08-10 23:50:23 +02:00
|
|
|
},
|
2016-07-07 23:23:18 +02:00
|
|
|
});
|