2022-03-21 04:36:31 -04:00
|
|
|
import Cookies from "js-cookie";
|
|
|
|
import { useEffect, useState } from "react";
|
2022-03-22 23:06:15 -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-03-22 23:06:15 -04:00
|
|
|
const [signedIn, setSignedIn] = useSharedState('signedIn', typeof window === 'undefined' ? false : !!Cookies.get("drift-token"));
|
2022-03-21 21:51:19 -04:00
|
|
|
const token = Cookies.get("drift-token")
|
2022-03-22 23:06:15 -04:00
|
|
|
const signin = (token: string) => {
|
|
|
|
setSignedIn(true);
|
|
|
|
Cookies.set("drift-token", token);
|
|
|
|
}
|
2022-03-06 19:46:59 -05:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-03-21 21:51:19 -04:00
|
|
|
if (token) {
|
2022-03-21 04:36:31 -04:00
|
|
|
setSignedIn(true);
|
|
|
|
} else {
|
|
|
|
setSignedIn(false);
|
2022-03-06 19:46:59 -05:00
|
|
|
}
|
2022-03-22 23:06:15 -04:00
|
|
|
}, [setSignedIn, token]);
|
2022-03-06 19:46:59 -05:00
|
|
|
|
2022-03-22 23:06:15 -04:00
|
|
|
return { signedIn, signin, token };
|
2022-03-06 19:46:59 -05:00
|
|
|
}
|
|
|
|
|
2022-03-21 04:36:31 -04:00
|
|
|
export default useSignedIn;
|