ampache_react/app/reducers/auth.js

75 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-07-07 23:23:18 +02:00
import Cookies from "js-cookie";
import {LOGIN_USER_REQUEST, LOGIN_USER_SUCCESS, LOGIN_USER_FAILURE, LOGOUT_USER} from "../actions";
import { createReducer } from "../utils";
var initialToken = Cookies.getJSON("token");
if (initialToken) {
initialToken.expires = new Date(initialToken.expires);
}
const initialState = {
token: initialToken || {
token: "",
expires: null
},
username: Cookies.get("username"),
endpoint: Cookies.get("endpoint"),
rememberMe: Boolean(Cookies.get("username") && Cookies.get("endpoint")),
isAuthenticated: false,
isAuthenticating: false,
error: "",
info: "",
timerID: null
};
export default createReducer(initialState, {
[LOGIN_USER_REQUEST]: (state) => {
return Object.assign({}, state, {
isAuthenticating: true,
info: "Connecting…",
error: "",
timerID: null
});
},
[LOGIN_USER_SUCCESS]: (state, payload) => {
return Object.assign({}, state, {
isAuthenticating: false,
isAuthenticated: true,
token: payload.token,
username: payload.username,
endpoint: payload.endpoint,
rememberMe: payload.rememberMe,
info: "Successfully logged in as " + payload.username + "!",
error: "",
timerID: payload.timerID
});
},
[LOGIN_USER_FAILURE]: (state, payload) => {
return Object.assign({}, state, {
isAuthenticating: false,
isAuthenticated: false,
token: initialState.token,
username: "",
endpoint: "",
rememberMe: false,
info: "",
error: payload.error,
timerID: 0
});
},
[LOGOUT_USER]: (state) => {
return Object.assign({}, state, {
isAuthenticated: false,
token: initialState.token,
username: "",
endpoint: "",
rememberMe: false,
info: "See you soon!",
error: "",
timerID: 0
});
}
});