EN · DE · RU · FR · ES

#2467: authentication.js

projectforge-webapp/src/actions/authentication.js Type: JavaScript/React · Role: Component · Source: projectforge-webapp/src/actions/authentication.js 85 lines · 73 code · 3 comments · 9 blank
React authentication module handling login state, token management, and session persistence.

Code Structure

Components: USER_LOGIN_BEGIN, USER_LOGIN_SUCCESS, USER_LOGIN_FAILURE

Hooks used: rLoginFailure, rLoginBegin, rLoginSuccess, rLoginBegin

Exports: USER_LOGIN_BEGIN, USER_LOGIN_SUCCESS, USER_LOGIN_FAILURE, userLoginBegin, userLoginSuccess, userLoginFailure, loadUserStatus, login

Imports from: ../utilities/rest

Source Code (abridged)

import { getServiceURL, handleHTTPErrors } from '../utilities/rest';

export const USER_LOGIN_BEGIN = 'USER_LOGIN_BEGIN';
export const USER_LOGIN_SUCCESS = 'USER_LOGIN_SUCCESS';
export const USER_LOGIN_FAILURE = 'USER_LOGIN_FAILURE';

export const userLoginBegin = () => ({
    type: USER_LOGIN_BEGIN,
});

export const userLoginSuccess = (user, version, buildTimestamp, alertMessage) => ({
    type: USER_LOGIN_SUCCESS,
    payload: {
        user,
        version,
        buildTimestamp,
        alertMessage,
    },
});

export const userLoginFailure = (error) => ({
    type: USER_LOGIN_FAILURE,
    payload: {
        error,
    },
});

const catchError = (dispatch) => (error) => dispatch(userLoginFailure(error.message));

export const loadUserStatus = () => (dispatch) => {
    dispatch(userLoginBegin());

    return fetch(
        getServiceURL('userStatus'),
        {
            method: 'GET',
            credentials: 'include',
        },
    )
        .then(handleHTTPErrors)
        .then((response) => response.json())
        .then(({ userData, systemData, alertMessage }) => {
            dispatch(userLoginSuccess(
                userData,
                systemData.version,
                systemData.buildTimestamp,
                alertMessage,
            ));
        })
        .catch(() => {
            const { pathname, search } = window.location;
            const href = pathname + search;
            if (!pathname.startsWith('/react/public/login')
                // /react/public/datatransfer/ is a special case where the user is not logged in.
                // Login form is returned by data transfer.
                && !pathname.startsWith('/react/public/datatransfer/')) {
                // set the URL to redirect to after login:
                window.location.href = `/react/public/login?url=${encodeURIComponent(href)}`;
            }

            catchError(dispatch)({ message: undefined });
        });
};

export const login = (username, password, keepSignedIn) => (dispatch) => {
    dispatch(userLoginBegin());
    return fetch(
        getServiceURL('/rsPublic/login'),
        {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                username,
                password,
                stayLoggedIn: keepSignedIn,
            }),
            credentials: 'include',
        },
    )
        .then(handleHTTPErrors)
        .then(() => loadUserStatus()(dispatch))
        .catch(catchError(dispatch));
};

Git History

e2061d7db UserPrefDao: Suppress error log on old TimesheetPrefData. Login handling improved: redirect to origin url after login.
cc0eb7b56 DataTransferPublicPagesRest: don't show access token in login form. Login fixed.
7c88abd0f wip lift dependencies
bbd81edc3 es-lint, new js versions.
4f5a06d6f AppVersion removed. Git information added to ProjectForgeVersion.