Skip to content

Commit

Permalink
redirect after login to original route (#739) (#759)
Browse files Browse the repository at this point in the history
Co-authored-by: Aashish Upadhyay <[email protected]>
Co-authored-by: mnida <[email protected]>
  • Loading branch information
3 people authored Mar 25, 2023
1 parent 87df2d5 commit d8e445b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
6 changes: 5 additions & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ function App() {
error?.response?.status === 401 &&
!publicRoutes.includes(pathname)
) {
navigate("/");
navigate("/", {
state: {
redirectTo: pathname,
},
});
}
return error;
});
Expand Down
13 changes: 8 additions & 5 deletions frontend/src/pages/Login.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { FC, useState } from "react";
import { useNavigate, Link } from "react-router-dom";
import { useNavigate, Link, useLocation } from "react-router-dom";
import { Authentication, instance } from "../api/api";
import { Card, Input, Button, Form } from "antd";
import "./Login.css";
Expand All @@ -24,7 +24,9 @@ interface LoginForm extends HTMLFormControlsCollection {
interface FormElements extends HTMLFormElement {
readonly elements: LoginForm;
}

interface LocationState {
redirectTo?: string;
}
interface LoginProps {
username?: string;
password?: string;
Expand All @@ -36,6 +38,7 @@ const Login: FC<LoginProps> = (props) => {
const [error, setError] = useState("");
const setUsernameToStore = useGlobalStore((state) => state.setUsername);
const queryClient = useQueryClient();
const location = useLocation();

const [isAuthenticated, setIsAuthenticated] = useState(false);
const navigate = useNavigate();
Expand All @@ -48,8 +51,8 @@ const Login: FC<LoginProps> = (props) => {
setUsername(event.target.value);
};

const redirectDashboard = () => {
navigate("/dashboard");
const redirectAfterLogin = () => {
navigate((location.state as LocationState).redirectTo!);
};

const isDemo = (import.meta as any).env.VITE_IS_DEMO === "true";
Expand Down Expand Up @@ -79,7 +82,7 @@ const Login: FC<LoginProps> = (props) => {
cookies.set("Token", token);
instance.defaults.headers.common.Authorization = `Token ${token}`;
queryClient.refetchQueries(["session"]);
redirectDashboard();
redirectAfterLogin();
},
onError: (error: QueryErrors) => {
// setError(error.message);
Expand Down

0 comments on commit d8e445b

Please sign in to comment.