From 42c95776cd3eb09951db8cc4dd4e650cbfae50b1 Mon Sep 17 00:00:00 2001 From: Scott Bishel Date: Wed, 19 Apr 2023 10:45:23 -0600 Subject: [PATCH] MM-51876 - use redirect if user already logged in (#22997) * use redirect if user already logged in * lint fixes --------- Co-authored-by: Mattermost Build --- .../channels/src/components/login/login.test.tsx | 14 ++++++++++++++ webapp/channels/src/components/login/login.tsx | 10 +++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/webapp/channels/src/components/login/login.test.tsx b/webapp/channels/src/components/login/login.test.tsx index b1116f4d2c..512c98856f 100644 --- a/webapp/channels/src/components/login/login.test.tsx +++ b/webapp/channels/src/components/login/login.test.tsx @@ -288,4 +288,18 @@ describe('components/login/Login', () => { expect(externalLoginButton.props().label).toEqual('OpenID 2'); expect(externalLoginButton.props().style).toEqual({color: '#00ff00', borderColor: '#00ff00'}); }); + + it('should redirect on login', () => { + mockState.entities.users.currentUserId = 'user1'; + LocalStorageStore.setWasLoggedIn(true); + mockConfig.EnableSignInWithEmail = 'true'; + const redirectPath = '/boards/team/teamID/boardID'; + mockLocation.search = '?redirect_to=' + redirectPath; + mount( + + + , + ); + expect(mockHistoryPush).toHaveBeenCalledWith(redirectPath); + }); }); diff --git a/webapp/channels/src/components/login/login.tsx b/webapp/channels/src/components/login/login.tsx index c0e154e561..edde751e63 100644 --- a/webapp/channels/src/components/login/login.tsx +++ b/webapp/channels/src/components/login/login.tsx @@ -141,6 +141,9 @@ const Login = ({onCustomizeHeader}: LoginProps) => { const enableExternalSignup = enableSignUpWithGitLab || enableSignUpWithOffice365 || enableSignUpWithGoogle || enableSignUpWithOpenId || enableSignUpWithSaml; const showSignup = enableOpenServer && (enableExternalSignup || enableSignUpWithEmail || enableLdap); + const query = new URLSearchParams(search); + const redirectTo = query.get('redirect_to'); + const getExternalLoginOptions = () => { const externalLoginOptions: ExternalLoginButtonType[] = []; @@ -372,6 +375,10 @@ const Login = ({onCustomizeHeader}: LoginProps) => { useEffect(() => { if (currentUser) { + if (redirectTo && redirectTo.match(/^\/([^/]|$)/)) { + history.push(redirectTo); + return; + } redirectUserToDefaultTeam(); return; } @@ -615,9 +622,6 @@ const Login = ({onCustomizeHeader}: LoginProps) => { dispatch(setNeedsLoggedInLimitReachedCheck(true)); } - const query = new URLSearchParams(search); - const redirectTo = query.get('redirect_to'); - setCSRFFromCookie(); // Record a successful login to local storage. If an unintentional logout occurs, e.g.