Spaces:
Running
Running
update cookies
Browse files- hooks/useUser.ts +7 -11
hooks/useUser.ts
CHANGED
|
@@ -8,6 +8,8 @@ import { User } from "@/types";
|
|
| 8 |
import MY_TOKEN_KEY from "@/lib/get-cookie-name";
|
| 9 |
import { api } from "@/lib/api";
|
| 10 |
import { toast } from "sonner";
|
|
|
|
|
|
|
| 11 |
|
| 12 |
export const useUser = (initialData?: {
|
| 13 |
user: User | null;
|
|
@@ -16,7 +18,7 @@ export const useUser = (initialData?: {
|
|
| 16 |
const cookie_name = MY_TOKEN_KEY();
|
| 17 |
const client = useQueryClient();
|
| 18 |
const router = useRouter();
|
| 19 |
-
const [, setCookie
|
| 20 |
const [currentRoute, setCurrentRoute] = useCookie("deepsite-currentRoute");
|
| 21 |
|
| 22 |
const { data: { user, errCode } = { user: null, errCode: null }, isLoading } =
|
|
@@ -47,7 +49,7 @@ export const useUser = (initialData?: {
|
|
| 47 |
};
|
| 48 |
|
| 49 |
const openLoginWindow = async () => {
|
| 50 |
-
setCurrentRoute(window.location.pathname);
|
| 51 |
return router.push("/auth");
|
| 52 |
};
|
| 53 |
|
|
@@ -58,20 +60,14 @@ export const useUser = (initialData?: {
|
|
| 58 |
.post("/auth", { code })
|
| 59 |
.then(async (res: any) => {
|
| 60 |
if (res.data) {
|
| 61 |
-
setCookie(res.data.access_token,
|
| 62 |
-
expires: res.data.expires_in
|
| 63 |
-
? new Date(Date.now() + res.data.expires_in * 1000)
|
| 64 |
-
: undefined,
|
| 65 |
-
sameSite: "none",
|
| 66 |
-
secure: true,
|
| 67 |
-
});
|
| 68 |
client.setQueryData(["user.me"], {
|
| 69 |
user: res.data.user,
|
| 70 |
errCode: null,
|
| 71 |
});
|
| 72 |
if (currentRoute) {
|
| 73 |
router.push(currentRoute);
|
| 74 |
-
setCurrentRoute("");
|
| 75 |
} else {
|
| 76 |
router.push("/projects");
|
| 77 |
}
|
|
@@ -87,7 +83,7 @@ export const useUser = (initialData?: {
|
|
| 87 |
};
|
| 88 |
|
| 89 |
const logout = async () => {
|
| 90 |
-
|
| 91 |
router.push("/");
|
| 92 |
toast.success("Logout successful");
|
| 93 |
client.setQueryData(["user.me"], {
|
|
|
|
| 8 |
import MY_TOKEN_KEY from "@/lib/get-cookie-name";
|
| 9 |
import { api } from "@/lib/api";
|
| 10 |
import { toast } from "sonner";
|
| 11 |
+
import { getAuthCookieOptions, getIframeCookieOptions, getRemoveCookieOptions } from "@/lib/cookie-options";
|
| 12 |
+
|
| 13 |
|
| 14 |
export const useUser = (initialData?: {
|
| 15 |
user: User | null;
|
|
|
|
| 18 |
const cookie_name = MY_TOKEN_KEY();
|
| 19 |
const client = useQueryClient();
|
| 20 |
const router = useRouter();
|
| 21 |
+
const [, setCookie] = useCookie(cookie_name);
|
| 22 |
const [currentRoute, setCurrentRoute] = useCookie("deepsite-currentRoute");
|
| 23 |
|
| 24 |
const { data: { user, errCode } = { user: null, errCode: null }, isLoading } =
|
|
|
|
| 49 |
};
|
| 50 |
|
| 51 |
const openLoginWindow = async () => {
|
| 52 |
+
setCurrentRoute(window.location.pathname, getIframeCookieOptions());
|
| 53 |
return router.push("/auth");
|
| 54 |
};
|
| 55 |
|
|
|
|
| 60 |
.post("/auth", { code })
|
| 61 |
.then(async (res: any) => {
|
| 62 |
if (res.data) {
|
| 63 |
+
setCookie(res.data.access_token, getAuthCookieOptions(res.data.expires_in));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
client.setQueryData(["user.me"], {
|
| 65 |
user: res.data.user,
|
| 66 |
errCode: null,
|
| 67 |
});
|
| 68 |
if (currentRoute) {
|
| 69 |
router.push(currentRoute);
|
| 70 |
+
setCurrentRoute("", getIframeCookieOptions());
|
| 71 |
} else {
|
| 72 |
router.push("/projects");
|
| 73 |
}
|
|
|
|
| 83 |
};
|
| 84 |
|
| 85 |
const logout = async () => {
|
| 86 |
+
setCookie("", getRemoveCookieOptions());
|
| 87 |
router.push("/");
|
| 88 |
toast.success("Logout successful");
|
| 89 |
client.setQueryData(["user.me"], {
|