2022-11-10 02:11:36 -05:00
|
|
|
import { TOKEN_COOKIE_NAME } from "@lib/constants"
|
2022-11-09 22:02:06 -05:00
|
|
|
import { getCookie, setCookie } from "cookies-next"
|
2022-04-02 03:46:39 -04:00
|
|
|
import { useEffect } from "react"
|
2022-03-23 18:34:23 -04:00
|
|
|
import useSharedState from "./use-shared-state"
|
2022-03-06 19:46:59 -05:00
|
|
|
|
2022-03-21 04:36:31 -04:00
|
|
|
const useSignedIn = () => {
|
2022-11-10 02:11:36 -05:00
|
|
|
const token = getCookie(TOKEN_COOKIE_NAME)
|
2022-11-09 22:02:06 -05:00
|
|
|
|
2022-03-23 18:42:22 -04:00
|
|
|
const [signedIn, setSignedIn] = useSharedState(
|
|
|
|
"signedIn",
|
2022-11-09 22:02:06 -05:00
|
|
|
typeof window === "undefined" ? false : !!token
|
2022-03-23 18:42:22 -04:00
|
|
|
)
|
2022-11-09 22:46:12 -05:00
|
|
|
|
2022-03-23 18:42:22 -04:00
|
|
|
const signin = (token: string) => {
|
|
|
|
setSignedIn(true)
|
2022-04-02 03:45:26 -04:00
|
|
|
// TODO: investigate SameSite / CORS cookie security
|
2022-11-10 02:11:36 -05:00
|
|
|
setCookie(TOKEN_COOKIE_NAME, token)
|
2022-03-23 18:42:22 -04:00
|
|
|
}
|
2022-03-06 19:46:59 -05:00
|
|
|
|
2022-11-11 22:17:44 -05:00
|
|
|
// useEffect(() => {
|
|
|
|
// if (token) {
|
|
|
|
// setSignedIn(true)
|
|
|
|
// } else {
|
|
|
|
// setSignedIn(false)
|
|
|
|
// }
|
|
|
|
// }, [setSignedIn, token])
|
2022-03-06 19:46:59 -05:00
|
|
|
|
2022-11-10 02:11:36 -05:00
|
|
|
console.log("signed in", signedIn)
|
|
|
|
|
2022-04-02 03:46:39 -04:00
|
|
|
return { signedIn, signin, token }
|
2022-03-06 19:46:59 -05:00
|
|
|
}
|
|
|
|
|
2022-03-23 18:34:23 -04:00
|
|
|
export default useSignedIn
|