Skip to content

Commit

Permalink
Added redirect query param
Browse files Browse the repository at this point in the history
  • Loading branch information
ActiveChooN committed Dec 3, 2020
1 parent 5735efe commit 8dd2ef4
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 16 deletions.
9 changes: 6 additions & 3 deletions cvat-ui/src/actions/auth-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export const authActions = {
authorizeSuccess: (user: any) => createAction(AuthActionTypes.AUTHORIZED_SUCCESS, { user }),
authorizeFailed: (error: any) => createAction(AuthActionTypes.AUTHORIZED_FAILED, { error }),
login: () => createAction(AuthActionTypes.LOGIN),
loginSuccess: (user: any) => createAction(AuthActionTypes.LOGIN_SUCCESS, { user }),
loginSuccess: (user: any, redirect: string | null) =>
createAction(AuthActionTypes.LOGIN_SUCCESS, { user, redirect }),
loginFailed: (error: any) => createAction(AuthActionTypes.LOGIN_FAILED, { error }),
register: () => createAction(AuthActionTypes.REGISTER),
registerSuccess: (user: any) => createAction(AuthActionTypes.REGISTER_SUCCESS, { user }),
Expand Down Expand Up @@ -98,14 +99,16 @@ export const registerAsync = (
}
};

export const loginAsync = (username: string, password: string): ThunkAction => async (dispatch) => {
export const loginAsync = (username: string, password: string, redirect: string | null): ThunkAction => async (
dispatch,
) => {
dispatch(authActions.login());

try {
await cvat.server.login(username, password);
const users = await cvat.users.get({ self: true });

dispatch(authActions.loginSuccess(users[0]));
dispatch(authActions.loginSuccess(users[0], redirect));
} catch (error) {
dispatch(authActions.loginFailed(error));
}
Expand Down
9 changes: 8 additions & 1 deletion cvat-ui/src/components/cvat-app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ interface CVATAppProps {
authActionsInitialized: boolean;
notifications: NotificationsState;
user: any;
redirect: string | null;
isModelPluginActive: boolean;
}

Expand All @@ -88,7 +89,7 @@ class CVATApplication extends React.PureComponent<CVATAppProps & RouteComponentP
verifyAuthorized();
}

public componentDidUpdate(): void {
public componentDidUpdate(prevProps: CVATAppProps & RouteComponentProps): void {
const {
verifyAuthorized,
loadFormats,
Expand All @@ -113,6 +114,8 @@ class CVATApplication extends React.PureComponent<CVATAppProps & RouteComponentP
authActionsFetching,
authActionsInitialized,
isModelPluginActive,
redirect,
history,
} = this.props;

this.showErrors();
Expand Down Expand Up @@ -151,6 +154,10 @@ class CVATApplication extends React.PureComponent<CVATAppProps & RouteComponentP
if (!pluginsInitialized && !pluginsFetching) {
initPlugins();
}

if (redirect && redirect !== prevProps.redirect) {
history.push(redirect);
}
}

private showMessages(): void {
Expand Down
14 changes: 9 additions & 5 deletions cvat-ui/src/components/login-page/login-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import React from 'react';
import { RouteComponentProps } from 'react-router';
import { Link, withRouter } from 'react-router-dom';
import { Link } from 'react-router-dom';
import Title from 'antd/lib/typography/Title';
import Text from 'antd/lib/typography/Text';
import { Row, Col } from 'antd/lib/grid';
Expand All @@ -15,7 +15,7 @@ import CookieDrawer from './cookie-policy-drawer';
interface LoginPageComponentProps {
fetching: boolean;
renderResetPassword: boolean;
onLogin: (username: string, password: string) => void;
onLogin: (username: string, password: string, redirect: string | null) => void;
}

function LoginPageComponent(props: LoginPageComponentProps & RouteComponentProps): JSX.Element {
Expand All @@ -27,7 +27,11 @@ function LoginPageComponent(props: LoginPageComponentProps & RouteComponentProps
xl: { span: 4 },
};

const { fetching, onLogin, renderResetPassword } = props;
const {
fetching, onLogin, renderResetPassword, location,
} = props;

const redirectParam = new URLSearchParams(location.search).get('redirect');

return (
<>
Expand All @@ -37,7 +41,7 @@ function LoginPageComponent(props: LoginPageComponentProps & RouteComponentProps
<LoginForm
fetching={fetching}
onSubmit={(loginData: LoginData): void => {
onLogin(loginData.username, loginData.password);
onLogin(loginData.username, loginData.password, redirectParam);
}}
/>
<Row type='flex' justify='start' align='top'>
Expand All @@ -64,4 +68,4 @@ function LoginPageComponent(props: LoginPageComponentProps & RouteComponentProps
);
}

export default withRouter(LoginPageComponent);
export default LoginPageComponent;
8 changes: 5 additions & 3 deletions cvat-ui/src/components/login-with-token/login-with-token.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
// SPDX-License-Identifier: MIT

import React, { useEffect } from 'react';
import { Redirect, useParams } from 'react-router';
import { Redirect, useParams, useLocation } from 'react-router';
import { useCookies } from 'react-cookie';

export default function LoginWithTokenComponent(): JSX.Element {
const { sessionId, token } = useParams();
const { sessionId, token } = useParams<{ sessionId: string; token: string }>();
const [cookies, setCookie] = useCookies(['sessionid', 'csrftoken']);
const location = useLocation();
const redirectParam = new URLSearchParams(location.search).get('redirect');

const expires1y = new Date(new Date().setFullYear(new Date().getFullYear() + 1));
const expires2w = new Date(new Date().setDate(new Date().getDate() + 13));
Expand All @@ -24,7 +26,7 @@ export default function LoginWithTokenComponent(): JSX.Element {
);

if (cookies.sessionid && cookies.csrftoken) {
return <Redirect to='/tasks' />;
return <Redirect to={redirectParam || '/tasks'} />;
}
return <></>;
}
3 changes: 2 additions & 1 deletion cvat-ui/src/containers/login-page/login-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: MIT

import { connect } from 'react-redux';
import { withRouter } from 'react-router';
import LoginPageComponent from 'components/login-page/login-page';
import { CombinedState } from 'reducers/interfaces';
import { loginAsync } from 'actions/auth-actions';
Expand All @@ -27,4 +28,4 @@ const mapDispatchToProps: DispatchToProps = {
onLogin: loginAsync,
};

export default connect(mapStateToProps, mapDispatchToProps)(LoginPageComponent);
export default connect(mapStateToProps, mapDispatchToProps)(withRouter(LoginPageComponent));
2 changes: 2 additions & 0 deletions cvat-ui/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ interface StateToProps {
allowResetPassword: boolean;
notifications: NotificationsState;
user: any;
redirect: string | null;
keyMap: Record<string, ExtendedKeyMapOptions>;
isModelPluginActive: boolean;
}
Expand Down Expand Up @@ -91,6 +92,7 @@ function mapStateToProps(state: CombinedState): StateToProps {
allowResetPassword: auth.allowResetPassword,
notifications: state.notifications,
user: auth.user,
redirect: auth.redirect,
keyMap: shortcuts.keyMap,
isModelPluginActive: plugins.list.MODELS,
};
Expand Down
8 changes: 5 additions & 3 deletions cvat-ui/src/reducers/auth-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AuthActions, AuthActionTypes } from 'actions/auth-actions';
import { AuthState } from './interfaces';

const defaultState: AuthState = {
redirect: null,
initialized: false,
fetching: false,
user: null,
Expand Down Expand Up @@ -39,6 +40,7 @@ export default function (state = defaultState, action: AuthActions | BoundariesA
return {
...state,
fetching: false,
redirect: action.payload.redirect,
user: action.payload.user,
};
case AuthActionTypes.LOGIN_FAILED:
Expand Down Expand Up @@ -94,9 +96,9 @@ export default function (state = defaultState, action: AuthActions | BoundariesA
return {
...state,
showChangePasswordDialog:
typeof action.payload.showChangePasswordDialog === 'undefined'
? !state.showChangePasswordDialog
: action.payload.showChangePasswordDialog,
typeof action.payload.showChangePasswordDialog === 'undefined' ?
!state.showChangePasswordDialog :
action.payload.showChangePasswordDialog,
};
case AuthActionTypes.REQUEST_PASSWORD_RESET:
return {
Expand Down
1 change: 1 addition & 0 deletions cvat-ui/src/reducers/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type StringObject = {
};

export interface AuthState {
redirect: string | null;
initialized: boolean;
fetching: boolean;
user: any;
Expand Down

0 comments on commit 8dd2ef4

Please sign in to comment.