diff options
Diffstat (limited to 'src/routes/login.ts')
| -rw-r--r-- | src/routes/login.ts | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/routes/login.ts b/src/routes/login.ts new file mode 100644 index 0000000..03ca5ee --- /dev/null +++ b/src/routes/login.ts @@ -0,0 +1,35 @@ +import type { RequestHandler } from '@sveltejs/kit' + +const DISCORD_CLIENT_ID = process.env.DISCORD_CLIENT_ID +if (!DISCORD_CLIENT_ID) + console.warn('DISCORD_CLIENT_ID is not set as an environment variable. This is required for logging in with Discord to work.') + +export const get: RequestHandler = async ({ request }) => { + const host = request.headers.get('host') + if (!DISCORD_CLIENT_ID) + return { + status: 500, + headers: { + 'content-type': 'text/plain', + }, + body: 'DISCORD_CLIENT_ID is not set as an environment variable. Please contact the owner of the website if this was expected to work.', + } + + if (!host) + return { + status: 400, + headers: { + 'content-type': 'text/plain', + }, + body: 'Host header is required.', + } + + const protocol = request.url.startsWith('https') ? 'https' : 'http' + + return { + status: 303, + headers: { + location: `https://discord.com/oauth2/authorize?client_id=${DISCORD_CLIENT_ID}&redirect_uri=${protocol}://${host}%2Floggedin&response_type=code&scope=identify` + } + } +} |
