EN · DE · RU · FR · ES

#2467 : authentication.js

projectforge-webapp/src/actions/authentication.js Type : JavaScript/React · Rôle : Composant · Source : projectforge-webapp/src/actions/authentication.js 85 lignes · 73 lignes de code · 3 commentaires · 9 lignes vides
Module d'authentification React gérant l'état de connexion, la gestion des jetons et la persistance de session.

Structure du code

Composants : USER_LOGIN_BEGIN, USER_LOGIN_SUCCESS, USER_LOGIN_FAILURE

Hooks utilisés : rLoginFailure, rLoginBegin, rLoginSuccess, rLoginBegin

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

Importations depuis : ../utilities/rest

Code source (abrégé)

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/ est un cas particulier où l'utilisateur n'est pas connecté.
                // Le formulaire de connexion est renvoyé par le transfert de données.
                && !pathname.startsWith('/react/public/datatransfer/')) {
                // définit l'URL de redirection après connexion :
                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));
};

Historique Git

e2061d7db UserPrefDao : Suppression du journal d'erreurs pour les anciennes données TimesheetPrefData. Gestion de la connexion améliorée : redirection vers l'URL d'origine après connexion.
cc0eb7b56 DataTransferPublicPagesRest : ne plus afficher le jeton d'accès dans le formulaire de connexion. Correction de la connexion.
7c88abd0f wip mise à jour des dépendances
bbd81edc3 es-lint, nouvelles versions js.
4f5a06d6f AppVersion supprimée. Informations Git ajoutées à ProjectForgeVersion.