-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: split user from info endpoint
- Loading branch information
Showing
7 changed files
with
40 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
import { useLocation, useNavigate } from "react-router-dom"; | ||
import { useInfo } from "./useInfo"; | ||
import React from "react"; | ||
import { useUser } from "./useUser"; | ||
|
||
const RETURN_TO_KEY = "returnTo"; | ||
|
||
export function useLogin() { | ||
const { data: info, isLoading } = useInfo(); | ||
const { data: user, isLoading } = useUser(); | ||
const location = useLocation(); | ||
const navigate = useNavigate(); | ||
|
||
React.useEffect(() => { | ||
if (!isLoading) { | ||
if (info?.user) { | ||
if (user) { | ||
const returnTo = window.localStorage.getItem(RETURN_TO_KEY); | ||
if (returnTo) { | ||
window.localStorage.removeItem(RETURN_TO_KEY); | ||
navigate(returnTo); | ||
} | ||
} | ||
if (!info?.user && ["/login", "/about"].indexOf(location.pathname) < 0) { | ||
if (!user && ["/login", "/about"].indexOf(location.pathname) < 0) { | ||
const returnTo = location.pathname + location.search; | ||
window.localStorage.setItem(RETURN_TO_KEY, returnTo); | ||
|
||
navigate("/login"); | ||
} | ||
} | ||
}, [info?.user, isLoading, location.pathname, location.search, navigate]); | ||
}, [user, isLoading, location.pathname, location.search, navigate]); | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import useSWR from "swr"; | ||
import { swrFetcher } from "../swr"; | ||
import { User } from "../types"; | ||
|
||
export function useUser() { | ||
return useSWR<User>("/api/user/me", swrFetcher); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters