bcgov/vue-scaffold

View on GitHub
app/frontend/src/store/modules/auth.js

Summary

Maintainability
A
50 mins
Test Coverage
A
100%
Parsing error: 'import' and 'export' may appear only with 'sourceType: module'
import Vue from 'vue';
 
/**
* @function hasRoles
* Checks if all elements in `roles` array exists in `tokenRoles` array
* @param {string[]} tokenRoles An array of roles that exist in the token
* @param {string[]} roles An array of roles to check
* @returns {boolean} True if all `roles` exist in `tokenRoles`; false otherwise
*/
function hasRoles(tokenRoles, roles = []) {
return roles.map(r => tokenRoles.some(t => t === r)).every(x => x === true);
}
 
export default {
namespaced: true,
state: {
// In most cases, when this becomes populated, we end up doing a redirect flow,
// so when we return to the app, it is fresh again and undefined
redirectUri: undefined
},
getters: {
authenticated: () => Vue.prototype.$keycloak.authenticated,
createLoginUrl: () => options => Vue.prototype.$keycloak.createLoginUrl(options),
createLogoutUrl: () => options => Vue.prototype.$keycloak.createLogoutUrl(options),
email: () => Vue.prototype.$keycloak.tokenParsed ? Vue.prototype.$keycloak.tokenParsed.email : '',
fullName: () => Vue.prototype.$keycloak.fullName,
Function `hasResourceRoles` has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
hasResourceRoles: (_state, getters) => (resource, roles) => {
if (!getters.authenticated) return false;
if (!roles.length) return true; // No roles to check against
 
if (getters.resourceAccess[resource]) {
return hasRoles(getters.resourceAccess[resource].roles, roles);
}
return false; // There are roles to check, but nothing in token to check against
},
identityProvider: () => Vue.prototype.$keycloak.tokenParsed.identity_provider,
keycloakReady: () => Vue.prototype.$keycloak.ready,
keycloakSubject: () => Vue.prototype.$keycloak.subject,
moduleLoaded: () => !!Vue.prototype.$keycloak,
realmAccess: () => Vue.prototype.$keycloak.tokenParsed.realm_access,
redirectUri: state => state.redirectUri,
resourceAccess: () => Vue.prototype.$keycloak.tokenParsed.resource_access,
token: () => Vue.prototype.$keycloak.token,
tokenParsed: () => Vue.prototype.$keycloak.tokenParsed,
userName: () => Vue.prototype.$keycloak.userName
},
mutations: {
SET_REDIRECTURI(state, redirectUri) {
state.redirectUri = redirectUri;
}
},
actions: {
Function `login` has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
login({ commit, getters }, idpHint = undefined) {
if (getters.keycloakReady) {
// Use existing redirect uri if available
if (!getters.redirectUri) commit('SET_REDIRECTURI', location.toString());
 
const options = {
redirectUri: getters.redirectUri
};
 
// Determine idpHint based on input
if (idpHint && typeof idpHint === 'string') options.idpHint = idpHint;
 
// Redirect to Keycloak
window.location.replace(getters.createLoginUrl(options));
}
},
logout({ getters }) {
if (getters.keycloakReady) {
window.location.replace(
getters.createLogoutUrl({
redirectUri: `${location.origin}/${Vue.prototype.$config.basePath}`
})
);
}
}
}
};