Skip to content

Commit

Permalink
Merge pull request #464 from ahnl/main
Browse files Browse the repository at this point in the history
Route for logging into IDE extensions using custom URL scheme
  • Loading branch information
Eldemarkki committed Feb 17, 2024
2 parents 8de7617 + f164007 commit fb9cc29
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
8 changes: 8 additions & 0 deletions public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -298,5 +298,13 @@
"button": "Search"
}
}
},
"authorize": {
"body": "You are logging in to the official Visual Studio Code extension.",
"continue": "Continue as {{username}}",
"notWorking": {
"text": "Not working?",
"link": "Copy authentication token on settings page."
}
}
}
8 changes: 8 additions & 0 deletions public/locales/fi/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -298,5 +298,13 @@
"button": "Etsi"
}
}
},
"authorize": {
"body": "Olet kirjautumassa sisään viralliseen Visual Studio Code -lisäosaan.",
"continue": "Jatka käyttäjänä {{username}}",
"notWorking": {
"text": "Eikö toimi?",
"link": "Kopioi tunnistautumistunnus asetukset-sivulla."
}
}
}
56 changes: 56 additions & 0 deletions src/app/[locale]/authorize/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Text, Button, Stack } from "@mantine/core";
import initTranslations from "../../i18n";
import { cookies } from "next/headers";
import { getMe } from "../../../api/usersApi";
import { redirect } from "next/navigation";
import Image from "next/image";
import Link from "next/link";

export default async function AuthorizePage({
params: { locale },
searchParams: { editor },
}: {
params: { locale: string };
searchParams: { editor?: string };
}) {
const { t } = await initTranslations(locale, ["common"]);

const loginUrl =
editor == "vscode"
? `/login?redirect=${encodeURIComponent("/authorize?editor=vscode")}`
: "/login";

const token = cookies().get("token")?.value;
if (!token) {
redirect(loginUrl);
}

const me = await getMe();
if (!me || "error" in me) {
redirect(loginUrl);
}

const { username } = me;

return (
<Stack align="center" gap="xl">
<Image
src="/images/vscode.svg"
alt="Visual Studio Code"
width={40}
height={40}
/>
<Text>{t("authorize.body")}</Text>
<Button
component="a"
href={`vscode://testausserveri-ry.testaustime/authorize?token=${token}`}
>
{t("authorize.continue", { username })}
</Button>
<Text c="dark.2">
{t("authorize.notWorking.text")}&nbsp;
<Link href="profile">{t("authorize.notWorking.link")}</Link>
</Text>
</Stack>
);
}
7 changes: 6 additions & 1 deletion src/components/LoginForm/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ export interface SecureAccessTokenResponse {
token: string;
}

const allowedRedirects = ["/profile", "/friends", "/leaderboards"];
const allowedRedirects = [
"/profile",
"/friends",
"/leaderboards",
"/authorize?editor=vscode",
];

export const logIn = async (
username: string,
Expand Down

0 comments on commit fb9cc29

Please sign in to comment.