Skip to content

Commit 87b7053

Browse files
committed
Use table functions
1 parent f457f70 commit 87b7053

3 files changed

Lines changed: 74 additions & 38 deletions

File tree

apps/obsidian/src/components/AdminPanelSettings.tsx

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,16 @@ export const AdminPanelSettings = () => {
1414
const [username, setUsername] = useState<string>(
1515
plugin.settings.username || "",
1616
);
17-
const [accessToken, setAccessToken] = useState<string | null>(null);
17+
const [isLoggedIn, setIsLoggedIn] = useState(false);
1818
useEffect(() => {
1919
if (syncModeEnabled) {
20-
const fetchTokens = async () => {
20+
const checkLogin = async () => {
2121
const client = await getLoggedInClient(plugin);
22-
if (client) {
23-
const session = await client.auth.getSession();
24-
if (session.data.session) {
25-
setAccessToken(session.data.session.access_token);
26-
}
27-
}
22+
setIsLoggedIn(client !== null);
2823
};
29-
void fetchTokens();
24+
void checkLogin();
3025
} else {
31-
setAccessToken(null);
26+
setIsLoggedIn(false);
3227
}
3328
}, [syncModeEnabled, plugin]);
3429

@@ -60,6 +55,22 @@ export const AdminPanelSettings = () => {
6055
await updateUsername(plugin, newValue);
6156
};
6257

58+
const handleLoginHandoff = async () => {
59+
const client = await getLoggedInClient(plugin);
60+
if (!client) return;
61+
const sessionData = await client.auth.getSession();
62+
if (!sessionData.data.session) return;
63+
/* eslint-disable @typescript-eslint/naming-convention */
64+
const { access_token, refresh_token } = sessionData.data.session;
65+
const { data, error } = await client.rpc("create_secret_token", {
66+
v_payload: JSON.stringify({ access_token, refresh_token }),
67+
expiry_interval: "10s",
68+
});
69+
/* eslint-enable @typescript-eslint/naming-convention */
70+
if (error) return;
71+
if (data) window.open(`${nextRoot()}/auth/token?t=${data}&url=/`, "_blank");
72+
};
73+
6374
return (
6475
<div className="general-settings">
6576
<div className="setting-item">
@@ -99,26 +110,21 @@ export const AdminPanelSettings = () => {
99110
/>
100111
</div>
101112
</div>
102-
<div className={"setting-item " + (accessToken ? "" : "hidden")}>
113+
<div className={"setting-item " + (isLoggedIn ? "" : "hidden")}>
103114
<div className="setting-item-info">
104115
<div className="setting-item-name">Group management</div>
105116
<div className="setting-item-description">
106117
This will allow you to view and manage your sharing groups
107118
</div>
108119
</div>
109120
<div className="setting-item-control">
110-
{accessToken && (
111-
<button
112-
onClick={() => {
113-
window.open(
114-
`${nextRoot()}/auth/token?t=${accessToken}&url=/`,
115-
"_blank",
116-
);
117-
}}
118-
>
119-
Manage groups
120-
</button>
121-
)}
121+
<button
122+
onClick={() => {
123+
void handleLoginHandoff();
124+
}}
125+
>
126+
Manage groups
127+
</button>
122128
</div>
123129
</div>
124130
</div>

apps/roam/src/components/settings/AdminPanel.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,22 @@ const FeatureFlagsTab = (): React.ReactElement => {
306306
}
307307
};
308308

309+
const handleLoginHandoff = async () => {
310+
const client = await getLoggedInClient();
311+
if (!client) return;
312+
const sessionData = await client.auth.getSession();
313+
if (!sessionData.data.session) return;
314+
/* eslint-disable @typescript-eslint/naming-convention */
315+
const { access_token, refresh_token } = sessionData.data.session;
316+
const { data, error } = await client.rpc("create_secret_token", {
317+
v_payload: JSON.stringify({ access_token, refresh_token }),
318+
expiry_interval: "10s",
319+
});
320+
/* eslint-enable @typescript-eslint/naming-convention */
321+
if (error) return;
322+
if (data) window.open(`${nextRoot()}/auth/token?t=${data}&url=/`, "_blank");
323+
};
324+
309325
return (
310326
<div className="flex flex-col gap-4 p-4">
311327
<FeatureFlagPanel
@@ -414,10 +430,7 @@ const FeatureFlagsTab = (): React.ReactElement => {
414430
className="w-96"
415431
icon="document-open"
416432
onClick={() => {
417-
window.open(
418-
`${nextRoot()}/auth/token?t=${accessToken}&url=/`,
419-
"_blank",
420-
);
433+
handleLoginHandoff();
421434
}}
422435
>
423436
Manage groups

apps/website/app/components/auth/LoginWithToken.tsx

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,41 @@ import { useState, useEffect, useCallback } from "react";
77
export const LoginWithToken = () => {
88
const searchParams = useSearchParams();
99
const router = useRouter();
10-
const [accessToken] = useState(searchParams.get("t"));
11-
const [refreshToken] = useState(searchParams.get("r"));
10+
const [secretToken] = useState(searchParams.get("t"));
1211
const [url] = useState(searchParams.get("url"));
1312
const [done, setDone] = useState(false);
1413
const [error, setError] = useState<string | null>(
15-
accessToken === null ? "Please provide access token" : null,
14+
secretToken === null ? "Please provide token" : null,
1615
);
1716

1817
const login = useCallback(async () => {
1918
try {
2019
const client = createClient();
21-
const response = await client.auth.setSession({
22-
/* eslint-disable @typescript-eslint/naming-convention */
23-
access_token: accessToken!,
24-
// in most cases, do not provide the refresh token! The access token will expire after 1h
25-
refresh_token: refreshToken ?? "faketoken",
26-
/* eslint-enable @typescript-eslint/naming-convention */
20+
const result = await client.rpc("get_secret_token", {
21+
token: secretToken!,
2722
});
23+
if (result.error) {
24+
setError(result.error.message);
25+
return;
26+
}
27+
if (typeof result.data !== "string") {
28+
setError("Payload is not a string");
29+
return;
30+
}
31+
const data = JSON.parse(result.data) as {
32+
access_token: string;
33+
refresh_token: string;
34+
};
35+
if (
36+
!data ||
37+
typeof data !== "object" ||
38+
!data.access_token ||
39+
!data.refresh_token
40+
) {
41+
setError("Malformed token information");
42+
return;
43+
}
44+
const response = await client.auth.setSession(data);
2845
if (response.error) {
2946
setError(response.error.message);
3047
} else if (url) {
@@ -35,12 +52,12 @@ export const LoginWithToken = () => {
3552
} finally {
3653
setDone(true);
3754
}
38-
}, [accessToken, refreshToken, url, router]);
55+
}, [secretToken, url, router]);
3956
useEffect(() => {
4057
if (!error && !done) {
4158
void login();
4259
}
43-
}, [error, login, accessToken, refreshToken, done]);
60+
}, [error, login, secretToken, done]);
4461
return (
4562
<div className="flex min-h-svh w-full items-center justify-center p-6 md:p-10">
4663
<div className="w-full max-w-sm">

0 commit comments

Comments
 (0)