mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-18 10:56:38 +00:00
shorten share links and add migration
This commit is contained in:
@@ -224,21 +224,18 @@ export default function CreateShareLinkForm({
|
||||
|
||||
if (res && res.data.data.accessTokenId) {
|
||||
const token = res.data.data;
|
||||
const link = constructShareLink(
|
||||
values.resourceId,
|
||||
token.accessTokenId,
|
||||
token.accessToken
|
||||
);
|
||||
const link = constructShareLink(token.accessToken);
|
||||
setLink(link);
|
||||
const directLink = constructDirectShareLink(
|
||||
env.server.resourceAccessTokenParam,
|
||||
values.resourceUrl,
|
||||
token.accessTokenId,
|
||||
token.accessToken
|
||||
);
|
||||
setDirectLink(directLink);
|
||||
|
||||
const resource = resources.find((r) => r.resourceId === values.resourceId);
|
||||
const resource = resources.find(
|
||||
(r) => r.resourceId === values.resourceId
|
||||
);
|
||||
|
||||
onCreated?.({
|
||||
accessTokenId: token.accessTokenId,
|
||||
@@ -247,7 +244,7 @@ export default function CreateShareLinkForm({
|
||||
title: token.title,
|
||||
createdAt: token.createdAt,
|
||||
expiresAt: token.expiresAt,
|
||||
siteName: resource?.siteName || null,
|
||||
siteName: resource?.siteName || null
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -15,15 +15,13 @@ import Link from "next/link";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
type AccessTokenProps = {
|
||||
accessTokenId: string | undefined;
|
||||
accessToken: string | undefined;
|
||||
resourceId: number;
|
||||
redirectUrl: string;
|
||||
token: string;
|
||||
resourceId?: number;
|
||||
redirectUrl?: string;
|
||||
};
|
||||
|
||||
export default function AccessToken({
|
||||
accessTokenId,
|
||||
accessToken,
|
||||
token,
|
||||
resourceId,
|
||||
redirectUrl
|
||||
}: AccessTokenProps) {
|
||||
@@ -43,11 +41,49 @@ export default function AccessToken({
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!accessTokenId || !accessToken) {
|
||||
if (!token) {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
let accessTokenId = "";
|
||||
let accessToken = "";
|
||||
|
||||
const parts = token.split(".");
|
||||
|
||||
if (parts.length === 2) {
|
||||
accessTokenId = parts[0];
|
||||
accessToken = parts[1];
|
||||
} else if (parts.length === 1) {
|
||||
accessToken = parts[0];
|
||||
} else {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
async function checkSHA256() {
|
||||
try {
|
||||
const res = await api.post<
|
||||
AxiosResponse<AuthWithAccessTokenResponse>
|
||||
>(`/auth/access-token`, {
|
||||
accessToken,
|
||||
accessTokenId
|
||||
});
|
||||
|
||||
if (res.data.data.session) {
|
||||
setIsValid(true);
|
||||
window.location.href = appendRequestToken(
|
||||
res.data.data.redirectUrl!,
|
||||
res.data.data.session
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Error checking access token", e);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function check() {
|
||||
try {
|
||||
const res = await api.post<
|
||||
@@ -60,7 +96,7 @@ export default function AccessToken({
|
||||
if (res.data.data.session) {
|
||||
setIsValid(true);
|
||||
window.location.href = appendRequestToken(
|
||||
redirectUrl,
|
||||
redirectUrl!,
|
||||
res.data.data.session
|
||||
);
|
||||
}
|
||||
@@ -71,8 +107,13 @@ export default function AccessToken({
|
||||
}
|
||||
}
|
||||
|
||||
check();
|
||||
}, [accessTokenId, accessToken]);
|
||||
if (!accessTokenId) {
|
||||
// no access token id so check the sha256
|
||||
checkSHA256();
|
||||
} else {
|
||||
check();
|
||||
}
|
||||
}, [token]);
|
||||
|
||||
function renderTitle() {
|
||||
if (isValid) {
|
||||
|
||||
@@ -118,12 +118,10 @@ export default async function ResourceAuthPage(props: {
|
||||
}
|
||||
|
||||
if (searchParams.token) {
|
||||
const [accessTokenId, accessToken] = searchParams.token.split(".");
|
||||
return (
|
||||
<div className="w-full max-w-md">
|
||||
<AccessToken
|
||||
accessToken={accessToken}
|
||||
accessTokenId={accessTokenId}
|
||||
token={searchParams.token}
|
||||
resourceId={params.resourceId}
|
||||
redirectUrl={redirectUrl}
|
||||
/>
|
||||
|
||||
13
src/app/s/[accessToken]/page.tsx
Normal file
13
src/app/s/[accessToken]/page.tsx
Normal file
@@ -0,0 +1,13 @@
|
||||
import AccessToken from "@app/app/auth/resource/[resourceId]/AccessToken";
|
||||
|
||||
export default async function ResourceAuthPage(props: {
|
||||
params: Promise<{ accessToken: string }>;
|
||||
}) {
|
||||
const params = await props.params;
|
||||
|
||||
return (
|
||||
<div className="w-full max-w-md mx-auto p-3 md:mt-32">
|
||||
<AccessToken token={params.accessToken} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,18 +1,13 @@
|
||||
import { pullEnv } from "./pullEnv";
|
||||
|
||||
export function constructShareLink(
|
||||
resourceId: number,
|
||||
id: string,
|
||||
token: string
|
||||
) {
|
||||
return `${window.location.origin}/auth/resource/${resourceId}?token=${id}.${token}`;
|
||||
return `${window.location.origin}/s/${token!}`;
|
||||
}
|
||||
|
||||
export function constructDirectShareLink(
|
||||
param: string,
|
||||
resourceUrl: string,
|
||||
id: string,
|
||||
token: string
|
||||
) {
|
||||
return `${resourceUrl}?${param}=${id}.${token}`;
|
||||
return `${resourceUrl}?${param}=${token}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user