Skip to content

Commit

Permalink
feat: /authorize route
Browse files Browse the repository at this point in the history
For authenticating the vscode extension using a URI scheme.
  • Loading branch information
ahnl committed Jan 3, 2024
1 parent a2dfa72 commit cc201bf
Show file tree
Hide file tree
Showing 4 changed files with 70 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."
}
}
}
53 changes: 53 additions & 0 deletions src/app/[locale]/authorize/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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>
<a href={`vscode://testausserveri-ry.testaustime/authorize?${token}`}>
<Button>{t("authorize.continue", { username })}</Button>
</a>
<Text c="dark.2">
{t("authorize.notWorking.text")}&nbsp;
<Link href="profile">{t("authorize.notWorking.link")}</Link>
</Text>
</Stack>
);
}
2 changes: 1 addition & 1 deletion src/components/LoginForm/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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 cc201bf

Please sign in to comment.