Created
July 25, 2021 10:23
-
-
Save Jerga99/12fa6b3690991dc080a91b113b23632a to your computer and use it in GitHub Desktop.
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
import useSWR from 'swr'; | |
import { fetcher } from '@/actions'; | |
import { useEffect, useState } from "react" | |
function doesHttpOnlyCookieExist(cookiename) { | |
var d = new Date(); | |
d.setTime(d.getTime() + (1000)); | |
var expires = "expires=" + d.toUTCString(); | |
document.cookie = cookiename + "=new_value;path=/;" + expires; | |
if (document.cookie.indexOf(cookiename + '=') == -1) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
export const useGetUser = () => { | |
const [isMounted, setIsMounted] = useState(false) | |
const hasAuthCookie = isMounted ? | |
doesHttpOnlyCookieExist('a0:session') : false; | |
useEffect(() => setIsMounted(true)) | |
const { data, error, isValidating, ...rest } = useSWR(hasAuthCookie ? '/api/v1/me' : null, fetcher); | |
return { | |
data, | |
error, | |
loading: isMounted && isValidating, | |
...rest | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment