Universal UI Login Flow

Login Action

async userSignIn({ commit, rootState, dispatch }, payload) {
    try {
        let response = await axios.post(
            `${
                rootState.config.authEndpoint
            }/oauth/token?grant_type=password`,
            {},
            {
                headers: {
                    Accept: 'application/json',
                    Authorization:
                        'Basic ' +
                        btoa(payload.name + ':' + payload.password),
                    'Content-Type': 'application/json'
                }
            }
        );
        let token = response.data.access_token;
        await dispatch('checkToken', token);
    }
    catch(e) {
        commit('setUserName', null);
        commit('setAuthenticated', false);
        commit(
            'setError',
            'Invalid username or password or combination. Please try again.'
        );
        return e;
    }
}

Validate Token

async checkToken({ commit, rootState, dispatch }, token) {
    try {
        let response = axios.post(
            `${
                rootState.config.authEndpoint
            }/oauth/check_token?token=${token}`,
            {},
            {
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                }
            }
        )
        dispatch('setAccessToken', response);
        return response;
    }
    catch(e) {
        commit('setUserName', null);
        commit('setAuthenticated', false);
        commit(
            'setError',
            'Invalid username or password or combination. Please try again.'
        );
        return e;
    }
}

Commit Token to Store

setAccessToken({ commit }, payload) {
    commit('accessToken', payload);
    window.$cookies.set('app-access-token', payload);
    commit('setServiceHeaders', {
        headers: {
            'Accept': 'application/json',
            'Authorization': 'Bearer ' + payload,
            'Content-Type': 'application/json',
        }
    });
    return;
}