Skip to content

Commit 9918a4d

Browse files
committed
implementing callback auth with strabo login page
1 parent c905fd8 commit 9918a4d

2 files changed

Lines changed: 71 additions & 6 deletions

File tree

pages/dev/strabospot/+Page.client.ts

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { FeatureDetails } from "./featured-checkins";
2525
import {
2626
refreshStoredStrabospotAuth,
2727
clearStoredStrabospotAuth,
28+
loginAndRefreshStrabospotFromUUID,
2829
} from "./strabospot-integration";
2930
import { getStoredRockdPersonId, clearRockdAuth } from "../../login/rockd-auth";
3031

@@ -190,10 +191,40 @@ export function Page() {
190191
useEffect(() => {
191192
let cancelled = false;
192193
async function checkStrabospotSync() {
193-
const ok = await refreshStoredStrabospotAuth();
194-
if (cancelled) return;
195-
setIsStrabospotSynced(ok);
196-
setIsCheckingStrabospotSync(false);
194+
try {
195+
const uuid = new URLSearchParams(window.location.search).get("u");
196+
if (uuid != null) {
197+
const auth = await loginAndRefreshStrabospotFromUUID(uuid);
198+
if (cancelled) return;
199+
200+
const isFullyLinked =
201+
auth.accessToken != null &&
202+
auth.refreshToken != null &&
203+
auth.datasetId != null &&
204+
auth.projectId != null;
205+
206+
setIsStrabospotSynced(isFullyLinked);
207+
208+
const cleanURL = `${window.location.origin}${window.location.pathname}`;
209+
window.history.replaceState({}, "", cleanURL);
210+
211+
return;
212+
}
213+
const ok = await refreshStoredStrabospotAuth();
214+
if (cancelled) return;
215+
setIsStrabospotSynced(ok);
216+
} catch (err: any) {
217+
console.error("Failed to complete StraboSpot login:", err);
218+
console.error("Message:", err?.message);
219+
220+
if (cancelled) return;
221+
222+
setIsStrabospotSynced(false);
223+
} finally {
224+
if (!cancelled) {
225+
setIsCheckingStrabospotSync(false);
226+
}
227+
}
197228
}
198229
checkStrabospotSync();
199230
return () => {
@@ -207,8 +238,10 @@ export function Page() {
207238
setIsStrabospotSynced(false);
208239
return;
209240
}
210-
211-
window.location.href = "/dev/strabospot/login";
241+
const callbackURL = `${window.location.origin}/dev/strabospot`;
242+
const strabospotLoginURL = new URL("https://strabospot.org/rockd_login");
243+
strabospotLoginURL.searchParams.set("redirect_uri", callbackURL);
244+
window.location.href = strabospotLoginURL.toString();
212245
}
213246

214247
const style = useMapStyle(

pages/dev/strabospot/strabospot-integration.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const STRABOSPOT_CREATE_PROJECT_ENDPOINT =
1414
"https://strabospot.org/jwtdb/project";
1515
const STRABOSPOT_IMAGE_ENDPOINT = "https://strabospot.org/jwtdb/image";
1616
const STORAGE_KEY = "strabospot-auth";
17+
const STRABOSPOT_ROCKD_LOGIN_ENDPOINT = "https://strabospot.org/rockd_login";
1718

1819
export interface StrabospotLoginResponse {
1920
access_token: string;
@@ -301,6 +302,37 @@ export async function loginAndRefreshStrabospot(
301302
return await ensureRockdIntegrationResources(auth);
302303
}
303304

305+
export async function loginToStrabospotWithUUID(uuid: string) {
306+
const url = new URL(STRABOSPOT_ROCKD_LOGIN_ENDPOINT);
307+
url.searchParams.set("u", uuid);
308+
const res = await fetch(url.toString(), {
309+
method: "GET",
310+
headers: { Accept: "application/json" },
311+
});
312+
const body = await parseJsonResponse(res);
313+
if (!res.ok) {
314+
throw new Error(
315+
typeof body === "string" ? body : JSON.stringify(body, null, 2)
316+
);
317+
}
318+
return body as StrabospotLoginResponse;
319+
}
320+
321+
export async function loginAndRefreshStrabospotFromUUID(uuid: string) {
322+
const login = await loginToStrabospotWithUUID(uuid);
323+
324+
const auth: StoredStrabospotAuth = {
325+
accessToken: login.access_token,
326+
refreshToken: login.refresh_token,
327+
tokenType: login.token_type,
328+
expiresIn: login.expires_in,
329+
user: login.user,
330+
};
331+
332+
saveStoredStrabospotAuth(auth);
333+
return await ensureRockdIntegrationResources(auth);
334+
}
335+
304336
export function getStoredStrabospotAuth(): StoredStrabospotAuth | null {
305337
const raw = localStorage.getItem(STORAGE_KEY);
306338
if (raw == null) return null;

0 commit comments

Comments
 (0)