MM-51876 - use redirect if user already logged in (#22997)

* use redirect if user already logged in

* lint fixes

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
This commit is contained in:
Scott Bishel 2023-04-19 10:45:23 -06:00 committed by GitHub
parent d1c9469d06
commit 96ad240a78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View File

@ -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(
<MemoryRouter>
<Login/>
</MemoryRouter>,
);
expect(mockHistoryPush).toHaveBeenCalledWith(redirectPath);
});
});

View File

@ -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.