Skip to content

Commit

Permalink
Login with next, alternative implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
bsekachev committed Dec 7, 2020
1 parent e5356cd commit 6a779a7
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 15 deletions.
8 changes: 5 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,7 @@ 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, next: string | null) => createAction(AuthActionTypes.LOGIN_SUCCESS, { user, next }),
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 +98,16 @@ export const registerAsync = (
}
};

export const loginAsync = (username: string, password: string): ThunkAction => async (dispatch) => {
export const loginAsync = (username: string, password: string, next: 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], next));
} catch (error) {
dispatch(authActions.loginFailed(error));
}
Expand Down
12 changes: 9 additions & 3 deletions 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;
next: string | null;
isModelPluginActive: boolean;
}

Expand Down Expand Up @@ -231,16 +232,19 @@ class CVATApplication extends React.PureComponent<CVATAppProps & RouteComponentP
aboutInitialized,
pluginsInitialized,
formatsInitialized,
modelsInitialized,
switchShortcutsDialog,
switchSettingsDialog,
user,
next,
keyMap,
location,
isModelPluginActive,
} = this.props;

const readyForRender =
(userInitialized && (user == null || !user.isVerified)) ||
(userInitialized && formatsInitialized && pluginsInitialized && aboutInitialized);
(userInitialized && formatsInitialized && pluginsInitialized && aboutInitialized && modelsInitialized);

const subKeyMap = {
SWITCH_SHORTCUTS: keyMap.SWITCH_SHORTCUTS,
Expand Down Expand Up @@ -310,7 +314,7 @@ class CVATApplication extends React.PureComponent<CVATAppProps & RouteComponentP
{isModelPluginActive && (
<Route exact path='/models' component={ModelsPageContainer} />
)}
<Redirect push to='/tasks' />
<Redirect push to={next || '/tasks'} />
</Switch>
</GlobalHotKeys>
{/* eslint-disable-next-line */}
Expand All @@ -337,7 +341,9 @@ class CVATApplication extends React.PureComponent<CVATAppProps & RouteComponentP
path='/auth/password/reset/confirm'
component={ResetPasswordPageConfirmComponent}
/>
<Redirect to='/auth/login' />
<Redirect
to={location.pathname.length > 1 ? `/auth/login/?next=${location.pathname}` : '/auth/login'}
/>
</Switch>
</GlobalErrorBoundary>
);
Expand Down
11 changes: 8 additions & 3 deletions cvat-ui/src/components/login-page/login-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import CookieDrawer from './cookie-policy-drawer';
interface LoginPageComponentProps {
fetching: boolean;
renderResetPassword: boolean;
onLogin: (username: string, password: string) => void;
next: string;
onLogin: (username: string, password: string, next: string | null) => void;
}

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

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

const search = new URLSearchParams(location.search);

return (
<>
Expand All @@ -37,7 +42,7 @@ function LoginPageComponent(props: LoginPageComponentProps & RouteComponentProps
<LoginForm
fetching={fetching}
onSubmit={(loginData: LoginData): void => {
onLogin(loginData.username, loginData.password);
onLogin(loginData.username, loginData.password, search.get('next'));
}}
/>
<Row type='flex' justify='start' align='top'>
Expand Down
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,15 +3,17 @@
// 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 location = useLocation();
const { sessionId, token } = useParams<{ sessionId: string; token: string }>();
const [cookies, setCookie] = useCookies(['sessionid', 'csrftoken']);

const expires1y = new Date(new Date().setFullYear(new Date().getFullYear() + 1));
const expires2w = new Date(new Date().setDate(new Date().getDate() + 13));
const search = new URLSearchParams(location.search);

setCookie('sessionid', sessionId, { path: '/', expires: expires2w });
setCookie('csrftoken', token, { path: '/', expires: expires1y });
Expand All @@ -24,7 +26,7 @@ export default function LoginWithTokenComponent(): JSX.Element {
);

if (cookies.sessionid && cookies.csrftoken) {
return <Redirect to='/tasks' />;
return <Redirect to={search.has('next') ? (search.get('next') as string) : '/tasks'} />;
}
return <></>;
}
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;
next: 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,
next: auth.next,
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 @@ -10,6 +10,7 @@ const defaultState: AuthState = {
initialized: false,
fetching: false,
user: null,
next: null,
authActionsFetching: false,
authActionsInitialized: false,
allowChangePassword: false,
Expand Down Expand Up @@ -40,6 +41,7 @@ export default function (state = defaultState, action: AuthActions | BoundariesA
...state,
fetching: false,
user: action.payload.user,
next: action.payload.next,
};
case AuthActionTypes.LOGIN_FAILED:
return {
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 @@ -14,6 +14,7 @@ export interface AuthState {
initialized: boolean;
fetching: boolean;
user: any;
next: string | null;
authActionsFetching: boolean;
authActionsInitialized: boolean;
showChangePasswordDialog: boolean;
Expand Down

0 comments on commit 6a779a7

Please sign in to comment.