chore: Pass sso_account_id to select the account during SSO Login (#4103)
This commit is contained in:
parent
11adfd2384
commit
9583a2dbad
8 changed files with 61 additions and 9 deletions
|
@ -13,7 +13,7 @@ export default {
|
||||||
.post('auth/sign_in', creds)
|
.post('auth/sign_in', creds)
|
||||||
.then(response => {
|
.then(response => {
|
||||||
setAuthCredentials(response);
|
setAuthCredentials(response);
|
||||||
resolve();
|
resolve(response.data);
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
reject(error.response);
|
reject(error.response);
|
||||||
|
|
|
@ -1,10 +1,22 @@
|
||||||
import queryString from 'query-string';
|
import queryString from 'query-string';
|
||||||
|
import { DEFAULT_REDIRECT_URL } from '../constants';
|
||||||
|
|
||||||
export const frontendURL = (path, params) => {
|
export const frontendURL = (path, params) => {
|
||||||
const stringifiedParams = params ? `?${queryString.stringify(params)}` : '';
|
const stringifiedParams = params ? `?${queryString.stringify(params)}` : '';
|
||||||
return `/app/${path}${stringifiedParams}`;
|
return `/app/${path}${stringifiedParams}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getLoginRedirectURL = (ssoAccountId, user) => {
|
||||||
|
const { accounts = [] } = user || {};
|
||||||
|
const ssoAccount = accounts.find(
|
||||||
|
account => account.id === Number(ssoAccountId)
|
||||||
|
);
|
||||||
|
if (ssoAccount) {
|
||||||
|
return frontendURL(`accounts/${ssoAccountId}/dashboard`);
|
||||||
|
}
|
||||||
|
return DEFAULT_REDIRECT_URL;
|
||||||
|
};
|
||||||
|
|
||||||
export const conversationUrl = ({
|
export const conversationUrl = ({
|
||||||
accountId,
|
accountId,
|
||||||
activeInbox,
|
activeInbox,
|
||||||
|
|
|
@ -3,6 +3,7 @@ import {
|
||||||
conversationUrl,
|
conversationUrl,
|
||||||
accountIdFromPathname,
|
accountIdFromPathname,
|
||||||
isValidURL,
|
isValidURL,
|
||||||
|
getLoginRedirectURL,
|
||||||
} from '../URLHelper';
|
} from '../URLHelper';
|
||||||
|
|
||||||
describe('#URL Helpers', () => {
|
describe('#URL Helpers', () => {
|
||||||
|
@ -58,4 +59,24 @@ describe('#URL Helpers', () => {
|
||||||
expect(isValidURL('alert.window')).toBe(false);
|
expect(isValidURL('alert.window')).toBe(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('getLoginRedirectURL', () => {
|
||||||
|
it('should return correct Account URL if account id is present', () => {
|
||||||
|
expect(
|
||||||
|
getLoginRedirectURL('7500', {
|
||||||
|
accounts: [{ id: 7500, name: 'Test Account 7500' }],
|
||||||
|
})
|
||||||
|
).toBe('/app/accounts/7500/dashboard');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return default URL if account id is not present', () => {
|
||||||
|
expect(getLoginRedirectURL('7500', {})).toBe('/app/');
|
||||||
|
expect(
|
||||||
|
getLoginRedirectURL('7500', {
|
||||||
|
accounts: [{ id: '7501', name: 'Test Account 7501' }],
|
||||||
|
})
|
||||||
|
).toBe('/app/');
|
||||||
|
expect(getLoginRedirectURL('7500', null)).toBe('/app/');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -5,6 +5,7 @@ import login from './login/login.routes';
|
||||||
import dashboard from './dashboard/dashboard.routes';
|
import dashboard from './dashboard/dashboard.routes';
|
||||||
import authRoute from './auth/auth.routes';
|
import authRoute from './auth/auth.routes';
|
||||||
import { frontendURL } from '../helper/URLHelper';
|
import { frontendURL } from '../helper/URLHelper';
|
||||||
|
import { clearBrowserSessionCookies } from '../store/utils/api';
|
||||||
|
|
||||||
const routes = [
|
const routes = [
|
||||||
...login.routes,
|
...login.routes,
|
||||||
|
@ -101,6 +102,13 @@ export const validateAuthenticateRoutePermission = (to, from, next) => {
|
||||||
return nextRoute ? next(frontendURL(nextRoute)) : next();
|
return nextRoute ? next(frontendURL(nextRoute)) : next();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const validateSSOLoginParams = to => {
|
||||||
|
const isLoginRoute = to.name === 'login';
|
||||||
|
const { email, sso_auth_token: ssoAuthToken } = to.query || {};
|
||||||
|
const hasValidSSOParams = email && ssoAuthToken;
|
||||||
|
return isLoginRoute && hasValidSSOParams;
|
||||||
|
};
|
||||||
|
|
||||||
const validateRouteAccess = (to, from, next) => {
|
const validateRouteAccess = (to, from, next) => {
|
||||||
if (
|
if (
|
||||||
window.chatwootConfig.signupEnabled !== 'true' &&
|
window.chatwootConfig.signupEnabled !== 'true' &&
|
||||||
|
@ -111,6 +119,11 @@ const validateRouteAccess = (to, from, next) => {
|
||||||
next(frontendURL(`accounts/${user.account_id}/dashboard`));
|
next(frontendURL(`accounts/${user.account_id}/dashboard`));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (validateSSOLoginParams(to)) {
|
||||||
|
clearBrowserSessionCookies();
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
if (authIgnoreRoutes.includes(to.name)) {
|
if (authIgnoreRoutes.includes(to.name)) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,6 +80,7 @@ export default {
|
||||||
mixins: [globalConfigMixin],
|
mixins: [globalConfigMixin],
|
||||||
props: {
|
props: {
|
||||||
ssoAuthToken: { type: String, default: '' },
|
ssoAuthToken: { type: String, default: '' },
|
||||||
|
ssoAccountId: { type: String, default: '' },
|
||||||
redirectUrl: { type: String, default: '' },
|
redirectUrl: { type: String, default: '' },
|
||||||
config: { type: String, default: '' },
|
config: { type: String, default: '' },
|
||||||
email: { type: String, default: '' },
|
email: { type: String, default: '' },
|
||||||
|
@ -138,6 +139,7 @@ export default {
|
||||||
: this.credentials.email,
|
: this.credentials.email,
|
||||||
password: this.credentials.password,
|
password: this.credentials.password,
|
||||||
sso_auth_token: this.ssoAuthToken,
|
sso_auth_token: this.ssoAuthToken,
|
||||||
|
ssoAccountId: this.ssoAccountId,
|
||||||
};
|
};
|
||||||
this.$store
|
this.$store
|
||||||
.dispatch('login', credentials)
|
.dispatch('login', credentials)
|
||||||
|
|
|
@ -12,6 +12,7 @@ export default {
|
||||||
email: route.query.email,
|
email: route.query.email,
|
||||||
ssoAuthToken: route.query.sso_auth_token,
|
ssoAuthToken: route.query.sso_auth_token,
|
||||||
redirectUrl: route.query.route_url,
|
redirectUrl: route.query.route_url,
|
||||||
|
ssoAccountId: route.query.sso_account_id,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|
|
@ -6,7 +6,7 @@ import authAPI from '../../api/auth';
|
||||||
import createAxios from '../../helper/APIHelper';
|
import createAxios from '../../helper/APIHelper';
|
||||||
import actionCable from '../../helper/actionCable';
|
import actionCable from '../../helper/actionCable';
|
||||||
import { setUser, getHeaderExpiry, clearCookiesOnLogout } from '../utils/api';
|
import { setUser, getHeaderExpiry, clearCookiesOnLogout } from '../utils/api';
|
||||||
import { DEFAULT_REDIRECT_URL } from '../../constants';
|
import { getLoginRedirectURL } from '../../helper/URLHelper';
|
||||||
|
|
||||||
const state = {
|
const state = {
|
||||||
currentUser: {
|
currentUser: {
|
||||||
|
@ -88,15 +88,16 @@ export const getters = {
|
||||||
|
|
||||||
// actions
|
// actions
|
||||||
export const actions = {
|
export const actions = {
|
||||||
login({ commit }, credentials) {
|
login({ commit }, { ssoAccountId, ...credentials }) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
authAPI
|
authAPI
|
||||||
.login(credentials)
|
.login(credentials)
|
||||||
.then(() => {
|
.then(response => {
|
||||||
commit(types.default.SET_CURRENT_USER);
|
commit(types.default.SET_CURRENT_USER);
|
||||||
window.axios = createAxios(axios);
|
window.axios = createAxios(axios);
|
||||||
actionCable.init(Vue);
|
actionCable.init(Vue);
|
||||||
window.location = DEFAULT_REDIRECT_URL;
|
|
||||||
|
window.location = getLoginRedirectURL(ssoAccountId, response.data);
|
||||||
resolve();
|
resolve();
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
|
|
|
@ -38,13 +38,15 @@ export const setAuthCredentials = response => {
|
||||||
setUser(response.data.data, expiryDate);
|
setUser(response.data.data, expiryDate);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const clearBrowserSessionCookies = () => {
|
||||||
|
Cookies.remove('auth_data');
|
||||||
|
Cookies.remove('user');
|
||||||
|
};
|
||||||
|
|
||||||
export const clearCookiesOnLogout = () => {
|
export const clearCookiesOnLogout = () => {
|
||||||
window.bus.$emit(CHATWOOT_RESET);
|
window.bus.$emit(CHATWOOT_RESET);
|
||||||
window.bus.$emit(ANALYTICS_RESET);
|
window.bus.$emit(ANALYTICS_RESET);
|
||||||
|
clearBrowserSessionCookies();
|
||||||
Cookies.remove('auth_data');
|
|
||||||
Cookies.remove('user');
|
|
||||||
|
|
||||||
const globalConfig = window.globalConfig || {};
|
const globalConfig = window.globalConfig || {};
|
||||||
const logoutRedirectLink =
|
const logoutRedirectLink =
|
||||||
globalConfig.LOGOUT_REDIRECT_LINK || frontendURL('login');
|
globalConfig.LOGOUT_REDIRECT_LINK || frontendURL('login');
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue