1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
"use client";
import { authClient } from "@lib/auth";
import { useRouter } from "next/navigation";
import { useEffect } from "react";
export const AnonymousAuth = ({
dashboardPath = "/dashboard",
loginPath = "/login",
}) => {
const router = useRouter();
useEffect(() => {
const createAnonymousSession = async () => {
const session = await authClient.getSession();
if (!session?.session) {
console.debug(
"[ANONYMOUS_AUTH] No session found, creating anonymous session...",
);
try {
// Create anonymous session
console.debug("[ANONYMOUS_AUTH] Calling signIn.anonymous()...");
const res = await authClient.signIn.anonymous();
if (!res.token) {
throw new Error("Failed to get anonymous token");
}
// Get the new session
console.debug(
"[ANONYMOUS_AUTH] Getting new session with anonymous token...",
);
const newSession = await authClient.getSession();
console.debug("[ANONYMOUS_AUTH] New session retrieved:", newSession);
if (!newSession?.session || !newSession?.user) {
console.error(
"[ANONYMOUS_AUTH] Failed to create anonymous session - missing session or user",
);
throw new Error("Failed to create anonymous session");
}
// Get the user's organization
console.debug(
"[ANONYMOUS_AUTH] Fetching organizations for anonymous user...",
);
const orgs = await authClient.organization.list();
console.debug("[ANONYMOUS_AUTH] Organizations retrieved:", {
count: orgs?.length || 0,
orgs: orgs?.map((o) => ({
id: o.id,
name: o.name,
slug: o.slug,
})),
});
const org = orgs?.[0];
if (!org) {
console.error(
"[ANONYMOUS_AUTH] No organization found for anonymous user",
);
throw new Error("Failed to get organization for anonymous user");
}
// Redirect to the organization dashboard
console.debug(
`[ANONYMOUS_AUTH] Redirecting anonymous user to /${org.slug}${dashboardPath}`,
);
router.push(dashboardPath);
} catch (error) {
console.error(
"[ANONYMOUS_AUTH] Anonymous session creation error:",
error,
);
console.error("[ANONYMOUS_AUTH] Error details:", {
message: error instanceof Error ? error.message : "Unknown error",
stack: error instanceof Error ? error.stack : undefined,
});
router.push(loginPath);
}
} else if (session.session) {
// Session exists, handle organization routing
console.debug(
"[ANONYMOUS_AUTH] Session exists, checking organization...",
);
if (!session.session.activeOrganizationId) {
console.debug(
"[ANONYMOUS_AUTH] No active organization ID, fetching organizations...",
);
const orgs = await authClient.organization.list();
console.debug("[ANONYMOUS_AUTH] Organizations for existing user:", {
count: orgs?.length || 0,
orgs: orgs?.map((o) => ({
id: o.id,
name: o.name,
slug: o.slug,
})),
});
if (orgs?.[0]) {
console.debug(
`[ANONYMOUS_AUTH] Setting active organization to ${orgs[0].id}`,
);
await authClient.organization.setActive({
organizationId: orgs[0].id,
});
console.debug(
`[ANONYMOUS_AUTH] Redirecting to /${orgs[0].slug}${dashboardPath}`,
);
router.push(dashboardPath);
}
} else {
console.debug(
`[ANONYMOUS_AUTH] Active organization ID: ${session.session.activeOrganizationId}`,
);
console.debug(
"[ANONYMOUS_AUTH] Fetching full organization details...",
);
const org = await authClient.organization.getFullOrganization({
query: {
organizationId: session.session.activeOrganizationId,
},
});
console.debug("[ANONYMOUS_AUTH] Full organization retrieved:", {
id: org.id,
name: org.name,
slug: org.slug,
});
console.debug(
`[ANONYMOUS_AUTH] Redirecting to /${org.slug}${dashboardPath}`,
);
router.push(dashboardPath);
}
}
};
createAnonymousSession();
}, [router.push]);
// Return null as this component only handles the redirect logic
return null;
};
|