Spaces:
Running
Running
voyage canada
#368
by
Immigration
- opened
- hooks/useUser.ts +11 -7
- lib/cookie-options.ts +0 -47
hooks/useUser.ts
CHANGED
@@ -8,8 +8,6 @@ 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 |
-
import { getAuthCookieOptions, getIframeCookieOptions, getRemoveCookieOptions } from "@/lib/cookie-options";
|
12 |
-
|
13 |
|
14 |
export const useUser = (initialData?: {
|
15 |
user: User | null;
|
@@ -18,7 +16,7 @@ export const useUser = (initialData?: {
|
|
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,7 +47,7 @@ export const useUser = (initialData?: {
|
|
49 |
};
|
50 |
|
51 |
const openLoginWindow = async () => {
|
52 |
-
setCurrentRoute(window.location.pathname
|
53 |
return router.push("/auth");
|
54 |
};
|
55 |
|
@@ -60,14 +58,20 @@ export const useUser = (initialData?: {
|
|
60 |
.post("/auth", { code })
|
61 |
.then(async (res: any) => {
|
62 |
if (res.data) {
|
63 |
-
setCookie(res.data.access_token,
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
client.setQueryData(["user.me"], {
|
65 |
user: res.data.user,
|
66 |
errCode: null,
|
67 |
});
|
68 |
if (currentRoute) {
|
69 |
router.push(currentRoute);
|
70 |
-
setCurrentRoute(""
|
71 |
} else {
|
72 |
router.push("/projects");
|
73 |
}
|
@@ -83,7 +87,7 @@ export const useUser = (initialData?: {
|
|
83 |
};
|
84 |
|
85 |
const logout = async () => {
|
86 |
-
|
87 |
router.push("/");
|
88 |
toast.success("Logout successful");
|
89 |
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 |
|
12 |
export const useUser = (initialData?: {
|
13 |
user: User | null;
|
|
|
16 |
const cookie_name = MY_TOKEN_KEY();
|
17 |
const client = useQueryClient();
|
18 |
const router = useRouter();
|
19 |
+
const [, setCookie, removeCookie] = useCookie(cookie_name);
|
20 |
const [currentRoute, setCurrentRoute] = useCookie("deepsite-currentRoute");
|
21 |
|
22 |
const { data: { user, errCode } = { user: null, errCode: null }, isLoading } =
|
|
|
47 |
};
|
48 |
|
49 |
const openLoginWindow = async () => {
|
50 |
+
setCurrentRoute(window.location.pathname);
|
51 |
return router.push("/auth");
|
52 |
};
|
53 |
|
|
|
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 |
};
|
88 |
|
89 |
const logout = async () => {
|
90 |
+
removeCookie();
|
91 |
router.push("/");
|
92 |
toast.success("Logout successful");
|
93 |
client.setQueryData(["user.me"], {
|
lib/cookie-options.ts
DELETED
@@ -1,47 +0,0 @@
|
|
1 |
-
// Cookie options for iframe compatibility using CHIPS (Cookies Having Independent Partitioned State)
|
2 |
-
export interface IframeCookieOptions {
|
3 |
-
expires?: Date;
|
4 |
-
maxAge?: number;
|
5 |
-
sameSite?: "strict" | "lax" | "none";
|
6 |
-
secure?: boolean;
|
7 |
-
partitioned?: boolean;
|
8 |
-
domain?: string;
|
9 |
-
path?: string;
|
10 |
-
}
|
11 |
-
|
12 |
-
/**
|
13 |
-
* Get cookie options optimized for iframe usage
|
14 |
-
* Uses CHIPS (Cookies Having Independent Partitioned State) for cross-site cookie support
|
15 |
-
*/
|
16 |
-
export function getIframeCookieOptions(
|
17 |
-
customOptions: Partial<IframeCookieOptions> = {}
|
18 |
-
): IframeCookieOptions {
|
19 |
-
return {
|
20 |
-
sameSite: "none",
|
21 |
-
secure: true,
|
22 |
-
partitioned: true,
|
23 |
-
path: "/",
|
24 |
-
...customOptions,
|
25 |
-
};
|
26 |
-
}
|
27 |
-
|
28 |
-
/**
|
29 |
-
* Get cookie options for the auth token specifically
|
30 |
-
*/
|
31 |
-
export function getAuthCookieOptions(expiresIn?: number): IframeCookieOptions {
|
32 |
-
return getIframeCookieOptions({
|
33 |
-
expires: expiresIn
|
34 |
-
? new Date(Date.now() + expiresIn * 1000)
|
35 |
-
: undefined,
|
36 |
-
});
|
37 |
-
}
|
38 |
-
|
39 |
-
/**
|
40 |
-
* Get cookie options for removing iframe-compatible cookies
|
41 |
-
* Sets the cookie to expire immediately while maintaining the same attributes
|
42 |
-
*/
|
43 |
-
export function getRemoveCookieOptions(): IframeCookieOptions {
|
44 |
-
return getIframeCookieOptions({
|
45 |
-
expires: new Date(0), // Set to epoch time (expired)
|
46 |
-
});
|
47 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|