mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-11-03 20:48:07 +00:00 
			
		
		
		
	chore: Add support for conversation_id in SSO params (#5228)
This commit is contained in:
		@@ -5,16 +5,31 @@ export const frontendURL = (path, params) => {
 | 
				
			|||||||
  return `/app/${path}${stringifiedParams}`;
 | 
					  return `/app/${path}${stringifiedParams}`;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const getLoginRedirectURL = (ssoAccountId, user) => {
 | 
					const getSSOAccountPath = ({ ssoAccountId, user }) => {
 | 
				
			||||||
  const { accounts = [] } = user || {};
 | 
					  const { accounts = [] } = user || {};
 | 
				
			||||||
  const ssoAccount = accounts.find(
 | 
					  const ssoAccount = accounts.find(
 | 
				
			||||||
    account => account.id === Number(ssoAccountId)
 | 
					    account => account.id === Number(ssoAccountId)
 | 
				
			||||||
  );
 | 
					  );
 | 
				
			||||||
 | 
					  let accountPath = '';
 | 
				
			||||||
  if (ssoAccount) {
 | 
					  if (ssoAccount) {
 | 
				
			||||||
    return frontendURL(`accounts/${ssoAccountId}/dashboard`);
 | 
					    accountPath = `accounts/${ssoAccountId}`;
 | 
				
			||||||
 | 
					  } else if (accounts.length) {
 | 
				
			||||||
 | 
					    accountPath = `accounts/${accounts[0].id}`;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  if (accounts.length) {
 | 
					  return accountPath;
 | 
				
			||||||
    return frontendURL(`accounts/${accounts[0].id}/dashboard`);
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const getLoginRedirectURL = ({
 | 
				
			||||||
 | 
					  ssoAccountId,
 | 
				
			||||||
 | 
					  ssoConversationId,
 | 
				
			||||||
 | 
					  user,
 | 
				
			||||||
 | 
					}) => {
 | 
				
			||||||
 | 
					  const accountPath = getSSOAccountPath({ ssoAccountId, user });
 | 
				
			||||||
 | 
					  if (accountPath) {
 | 
				
			||||||
 | 
					    if (ssoConversationId) {
 | 
				
			||||||
 | 
					      return frontendURL(`${accountPath}/conversations/${ssoConversationId}`);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return frontendURL(`${accountPath}/dashboard`);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  return DEFAULT_REDIRECT_URL;
 | 
					  return DEFAULT_REDIRECT_URL;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -74,17 +74,37 @@ describe('#URL Helpers', () => {
 | 
				
			|||||||
  describe('getLoginRedirectURL', () => {
 | 
					  describe('getLoginRedirectURL', () => {
 | 
				
			||||||
    it('should return correct Account URL if account id is present', () => {
 | 
					    it('should return correct Account URL if account id is present', () => {
 | 
				
			||||||
      expect(
 | 
					      expect(
 | 
				
			||||||
        getLoginRedirectURL('7500', {
 | 
					        getLoginRedirectURL({
 | 
				
			||||||
 | 
					          ssoAccountId: '7500',
 | 
				
			||||||
 | 
					          user: {
 | 
				
			||||||
            accounts: [{ id: 7500, name: 'Test Account 7500' }],
 | 
					            accounts: [{ id: 7500, name: 'Test Account 7500' }],
 | 
				
			||||||
 | 
					          },
 | 
				
			||||||
        })
 | 
					        })
 | 
				
			||||||
      ).toBe('/app/accounts/7500/dashboard');
 | 
					      ).toBe('/app/accounts/7500/dashboard');
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    it('should return default URL if account id is not present', () => {
 | 
					    it('should return correct conversation URL if account id and conversationId is present', () => {
 | 
				
			||||||
      expect(getLoginRedirectURL('7500', {})).toBe('/app/');
 | 
					 | 
				
			||||||
      expect(
 | 
					      expect(
 | 
				
			||||||
        getLoginRedirectURL('7500', {
 | 
					        getLoginRedirectURL({
 | 
				
			||||||
 | 
					          ssoAccountId: '7500',
 | 
				
			||||||
 | 
					          ssoConversationId: '752',
 | 
				
			||||||
 | 
					          user: {
 | 
				
			||||||
 | 
					            accounts: [{ id: 7500, name: 'Test Account 7500' }],
 | 
				
			||||||
 | 
					          },
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					      ).toBe('/app/accounts/7500/conversations/752');
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it('should return default URL if account id is not present', () => {
 | 
				
			||||||
 | 
					      expect(getLoginRedirectURL({ ssoAccountId: '7500', user: {} })).toBe(
 | 
				
			||||||
 | 
					        '/app/'
 | 
				
			||||||
 | 
					      );
 | 
				
			||||||
 | 
					      expect(
 | 
				
			||||||
 | 
					        getLoginRedirectURL({
 | 
				
			||||||
 | 
					          ssoAccountId: '7500',
 | 
				
			||||||
 | 
					          user: {
 | 
				
			||||||
            accounts: [{ id: '7501', name: 'Test Account 7501' }],
 | 
					            accounts: [{ id: '7501', name: 'Test Account 7501' }],
 | 
				
			||||||
 | 
					          },
 | 
				
			||||||
        })
 | 
					        })
 | 
				
			||||||
      ).toBe('/app/accounts/7501/dashboard');
 | 
					      ).toBe('/app/accounts/7501/dashboard');
 | 
				
			||||||
      expect(getLoginRedirectURL('7500', null)).toBe('/app/');
 | 
					      expect(getLoginRedirectURL('7500', null)).toBe('/app/');
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -80,7 +80,7 @@ export default {
 | 
				
			|||||||
  props: {
 | 
					  props: {
 | 
				
			||||||
    ssoAuthToken: { type: String, default: '' },
 | 
					    ssoAuthToken: { type: String, default: '' },
 | 
				
			||||||
    ssoAccountId: { type: String, default: '' },
 | 
					    ssoAccountId: { type: String, default: '' },
 | 
				
			||||||
    redirectUrl: { type: String, default: '' },
 | 
					    ssoConversationId: { type: String, default: '' },
 | 
				
			||||||
    config: { type: String, default: '' },
 | 
					    config: { type: String, default: '' },
 | 
				
			||||||
    email: { type: String, default: '' },
 | 
					    email: { type: String, default: '' },
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
@@ -139,6 +139,7 @@ export default {
 | 
				
			|||||||
        password: this.credentials.password,
 | 
					        password: this.credentials.password,
 | 
				
			||||||
        sso_auth_token: this.ssoAuthToken,
 | 
					        sso_auth_token: this.ssoAuthToken,
 | 
				
			||||||
        ssoAccountId: this.ssoAccountId,
 | 
					        ssoAccountId: this.ssoAccountId,
 | 
				
			||||||
 | 
					        ssoConversationId: this.ssoConversationId,
 | 
				
			||||||
      };
 | 
					      };
 | 
				
			||||||
      this.$store
 | 
					      this.$store
 | 
				
			||||||
        .dispatch('login', credentials)
 | 
					        .dispatch('login', credentials)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -11,8 +11,8 @@ export default {
 | 
				
			|||||||
        config: route.query.config,
 | 
					        config: route.query.config,
 | 
				
			||||||
        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,
 | 
					 | 
				
			||||||
        ssoAccountId: route.query.sso_account_id,
 | 
					        ssoAccountId: route.query.sso_account_id,
 | 
				
			||||||
 | 
					        ssoConversationId: route.query.sso_conversation_id,
 | 
				
			||||||
      }),
 | 
					      }),
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
  ],
 | 
					  ],
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -84,12 +84,16 @@ export const getters = {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// actions
 | 
					// actions
 | 
				
			||||||
export const actions = {
 | 
					export const actions = {
 | 
				
			||||||
  login(_, { ssoAccountId, ...credentials }) {
 | 
					  login(_, { ssoAccountId, ssoConversationId, ...credentials }) {
 | 
				
			||||||
    return new Promise((resolve, reject) => {
 | 
					    return new Promise((resolve, reject) => {
 | 
				
			||||||
      authAPI
 | 
					      authAPI
 | 
				
			||||||
        .login(credentials)
 | 
					        .login(credentials)
 | 
				
			||||||
        .then(response => {
 | 
					        .then(response => {
 | 
				
			||||||
          window.location = getLoginRedirectURL(ssoAccountId, response.data);
 | 
					          window.location = getLoginRedirectURL({
 | 
				
			||||||
 | 
					            ssoAccountId,
 | 
				
			||||||
 | 
					            ssoConversationId,
 | 
				
			||||||
 | 
					            user: response.data,
 | 
				
			||||||
 | 
					          });
 | 
				
			||||||
          resolve();
 | 
					          resolve();
 | 
				
			||||||
        })
 | 
					        })
 | 
				
			||||||
        .catch(error => {
 | 
					        .catch(error => {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user