chore: Pass sso_account_id to select the account during SSO Login (#4103)

This commit is contained in:
Pranav Raj S 2022-03-03 20:49:51 +05:30 committed by GitHub
parent 11adfd2384
commit 9583a2dbad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 61 additions and 9 deletions

View file

@ -13,7 +13,7 @@ export default {
.post('auth/sign_in', creds)
.then(response => {
setAuthCredentials(response);
resolve();
resolve(response.data);
})
.catch(error => {
reject(error.response);

View file

@ -1,10 +1,22 @@
import queryString from 'query-string';
import { DEFAULT_REDIRECT_URL } from '../constants';
export const frontendURL = (path, params) => {
const stringifiedParams = params ? `?${queryString.stringify(params)}` : '';
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 = ({
accountId,
activeInbox,

View file

@ -3,6 +3,7 @@ import {
conversationUrl,
accountIdFromPathname,
isValidURL,
getLoginRedirectURL,
} from '../URLHelper';
describe('#URL Helpers', () => {
@ -58,4 +59,24 @@ describe('#URL Helpers', () => {
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/');
});
});
});

View file

@ -5,6 +5,7 @@ import login from './login/login.routes';
import dashboard from './dashboard/dashboard.routes';
import authRoute from './auth/auth.routes';
import { frontendURL } from '../helper/URLHelper';
import { clearBrowserSessionCookies } from '../store/utils/api';
const routes = [
...login.routes,
@ -101,6 +102,13 @@ export const validateAuthenticateRoutePermission = (to, from, 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) => {
if (
window.chatwootConfig.signupEnabled !== 'true' &&
@ -111,6 +119,11 @@ const validateRouteAccess = (to, from, next) => {
next(frontendURL(`accounts/${user.account_id}/dashboard`));
}
if (validateSSOLoginParams(to)) {
clearBrowserSessionCookies();
return next();
}
if (authIgnoreRoutes.includes(to.name)) {
return next();
}

View file

@ -80,6 +80,7 @@ export default {
mixins: [globalConfigMixin],
props: {
ssoAuthToken: { type: String, default: '' },
ssoAccountId: { type: String, default: '' },
redirectUrl: { type: String, default: '' },
config: { type: String, default: '' },
email: { type: String, default: '' },
@ -138,6 +139,7 @@ export default {
: this.credentials.email,
password: this.credentials.password,
sso_auth_token: this.ssoAuthToken,
ssoAccountId: this.ssoAccountId,
};
this.$store
.dispatch('login', credentials)

View file

@ -12,6 +12,7 @@ export default {
email: route.query.email,
ssoAuthToken: route.query.sso_auth_token,
redirectUrl: route.query.route_url,
ssoAccountId: route.query.sso_account_id,
}),
},
],

View file

@ -6,7 +6,7 @@ import authAPI from '../../api/auth';
import createAxios from '../../helper/APIHelper';
import actionCable from '../../helper/actionCable';
import { setUser, getHeaderExpiry, clearCookiesOnLogout } from '../utils/api';
import { DEFAULT_REDIRECT_URL } from '../../constants';
import { getLoginRedirectURL } from '../../helper/URLHelper';
const state = {
currentUser: {
@ -88,15 +88,16 @@ export const getters = {
// actions
export const actions = {
login({ commit }, credentials) {
login({ commit }, { ssoAccountId, ...credentials }) {
return new Promise((resolve, reject) => {
authAPI
.login(credentials)
.then(() => {
.then(response => {
commit(types.default.SET_CURRENT_USER);
window.axios = createAxios(axios);
actionCable.init(Vue);
window.location = DEFAULT_REDIRECT_URL;
window.location = getLoginRedirectURL(ssoAccountId, response.data);
resolve();
})
.catch(error => {

View file

@ -38,13 +38,15 @@ export const setAuthCredentials = response => {
setUser(response.data.data, expiryDate);
};
export const clearBrowserSessionCookies = () => {
Cookies.remove('auth_data');
Cookies.remove('user');
};
export const clearCookiesOnLogout = () => {
window.bus.$emit(CHATWOOT_RESET);
window.bus.$emit(ANALYTICS_RESET);
Cookies.remove('auth_data');
Cookies.remove('user');
clearBrowserSessionCookies();
const globalConfig = window.globalConfig || {};
const logoutRedirectLink =
globalConfig.LOGOUT_REDIRECT_LINK || frontendURL('login');