Skip to content

Commit

Permalink
return to current page after login
Browse files Browse the repository at this point in the history
  • Loading branch information
zmc committed Sep 30, 2024
1 parent 357f9d1 commit 3643481
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/components/Login/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState } from "react";
import { usePageContext } from 'vike-react/usePageContext'
import Button from "@mui/material/Button";
import Avatar from '@mui/material/Avatar';
import Menu from '@mui/material/Menu';
Expand All @@ -9,6 +10,7 @@ import { doLogin, doLogout, useSession, useUserData } from "../../lib/teuthology


export default function Login() {
const context = usePageContext();
const sessionQuery = useSession();
const userData = useUserData();
const [anchorEl, setAnchorEl] = useState(null);
Expand Down Expand Up @@ -47,7 +49,7 @@ export default function Login() {
</div>
: <Button
variant="contained"
onClick={doLogin}
onClick={() => (doLogin(context.urlOriginal))}
startIcon={<GitHubIcon fontSize="small" /> }
disabled={sessionQuery.isError}
>
Expand Down
17 changes: 9 additions & 8 deletions src/lib/teuthologyAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,36 @@ const TEUTHOLOGY_API_SERVER =
import.meta.env.VITE_TEUTHOLOGY_API || "";
const GH_USER_COOKIE = "GH_USER";

function getURL(relativeURL: URL|string): string {
if ( ! TEUTHOLOGY_API_SERVER ) return "";
return new URL(relativeURL, TEUTHOLOGY_API_SERVER).toString();
function getURL(relativeURL: URL|string): URL {
return new URL(relativeURL, TEUTHOLOGY_API_SERVER);
}

function doLogin() {
function doLogin(destinationUrl: string) {
const url = getURL("/login/");
if ( url ) window.location.href = url;
url.searchParams.set("dest", destinationUrl);
window.location.href = url.toString();
}

function doLogout() {
const cookies = new Cookies();
cookies.remove(GH_USER_COOKIE);

const url = getURL("/logout/");
window.location.href = url;
window.location.href = url.toString();
}


function useSession(): UseQueryResult {
const url = getURL("/");
const query = useQuery({
queryKey: ['ping-api', { url }],
queryFn: () => (
axios.get(url, {
axios.get(url.toString(), {
withCredentials: true
}).then((resp) => resp.data)
),
retry: 1,
enabled: url !== "",
enabled: url.toString() !== "",
});
return query;
}
Expand Down

0 comments on commit 3643481

Please sign in to comment.