Skip to content
Snippets Groups Projects
AuthenticationActions.js 3.08 KiB
Newer Older
Orlando Piñero's avatar
Orlando Piñero committed
export const SHOW_LOGIN_DIALOG = 'SHOW_LOGIN_DIALOG';
export const HIDE_LOGIN_DIALOG = 'HIDE_LOGIN_DIALOG';

Orlando Piñero's avatar
Orlando Piñero committed

Orlando Piñero's avatar
Orlando Piñero committed
export const LOGOUT = 'LOGOUT'
Orlando Piñero's avatar
Orlando Piñero committed
export const AUTHENTICATION_PENDING = 'AUTHENTICATION_PENDING'
export const AUTHENTICATION_SUCCESS = 'AUTHENTICATION_SUCCESS'
export const AUTHENTICATION_ERROR = 'AUTHENTICATION_ERROR'

Orlando Piñero's avatar
Orlando Piñero committed
const URL = process.env.REACT_APP_SERVER

Orlando Piñero's avatar
Orlando Piñero committed
export function getShowLoginDialogAction() {
    return {
        type: SHOW_LOGIN_DIALOG
    }
}

Orlando Piñero's avatar
Orlando Piñero committed

export function getHideLoginDialogAction() {
Orlando Piñero's avatar
Orlando Piñero committed
    return {
Orlando Piñero's avatar
Orlando Piñero committed
        type: HIDE_LOGIN_DIALOG
Orlando Piñero's avatar
Orlando Piñero committed
    }
}

Orlando Piñero's avatar
Orlando Piñero committed
export function getLogoutAction() {
Orlando Piñero's avatar
Orlando Piñero committed
    return {
Orlando Piñero's avatar
Orlando Piñero committed
        type: LOGOUT
Orlando Piñero's avatar
Orlando Piñero committed
    }
}

export function getAuthenticationPendingAction() {
    return {
        type: AUTHENTICATION_PENDING
    }
}

export function getAuthenticationSuccessAction(userSession) {
    return {
        type: AUTHENTICATION_SUCCESS,
        user: userSession.user,
Orlando Piñero's avatar
Orlando Piñero committed
        accessToken: userSession.accessToken,
        isAdmin: userSession.isAdmin
Orlando Piñero's avatar
Orlando Piñero committed
    }
}

export function getAuthenticationErrorAction(error) {
    return {
        type: AUTHENTICATION_ERROR,
        error: error
    }
}

Orlando Piñero's avatar
Orlando Piñero committed

Orlando Piñero's avatar
Orlando Piñero committed
export function authenticateUser(userID, password) {
    return dispatch => {
        dispatch(getAuthenticationPendingAction());
        login(userID, password)
            .then(
                userSession => {
                    const action = getAuthenticationSuccessAction(userSession);
                    dispatch(action);
                },
                error => {
                    dispatch(getAuthenticationErrorAction(error));
                }
            )
            .catch(error => {
                dispatch(getAuthenticationErrorAction(error))
            })
    }
}

function login(userID, password) {
    const requestOptions = {
        method: 'GET',
        headers: { 'Authorization': 'Basic ' + btoa(userID + ":" + password) }
    };

Orlando Piñero's avatar
Orlando Piñero committed
    return fetch(URL + '/authenticate', requestOptions)
Orlando Piñero's avatar
Orlando Piñero committed
        .then(handleResponse)
        .then(userSession => {
            return userSession
        });
}

function handleResponse(response) {
Orlando Piñero's avatar
Orlando Piñero committed
    const authorizationHeader = response.headers.get("Authorization");
Orlando Piñero's avatar
Orlando Piñero committed
    return response.text().then(text => {

        const data = text && JSON.parse(text);
Orlando Piñero's avatar
Orlando Piñero committed
        var splittedToken
Orlando Piñero's avatar
Orlando Piñero committed
        var token
Orlando Piñero's avatar
Orlando Piñero committed
        var adminRights
Orlando Piñero's avatar
Orlando Piñero committed
        var userID
Orlando Piñero's avatar
Orlando Piñero committed
        if (authorizationHeader) {
Orlando Piñero's avatar
Orlando Piñero committed
            token = authorizationHeader
Orlando Piñero's avatar
Orlando Piñero committed
            splittedToken = authorizationHeader.split(" ")[1];
Orlando Piñero's avatar
Orlando Piñero committed
            adminRights = (JSON.parse(atob(splittedToken.split(".")[1]))).isAdministrator
Orlando Piñero's avatar
Orlando Piñero committed
            userID = (JSON.parse(atob(splittedToken.split(".")[1]))).userID;
Orlando Piñero's avatar
Orlando Piñero committed
        }

        if (!response.ok) {
            if (response.status === 401) {
                logout();
            }
            const error = (data && data.message) || response.statusText;
            return Promise.reject(error);
        } else {
            let userSession = {
Orlando Piñero's avatar
Orlando Piñero committed
                user: userID,
Orlando Piñero's avatar
Orlando Piñero committed
                accessToken: token,
                isAdmin: adminRights
Orlando Piñero's avatar
Orlando Piñero committed
            }
            return userSession
        }
    });
}

function logout() {
Orlando Piñero's avatar
Orlando Piñero committed
    console.error('Should logout')