Skip to content

Commit

Permalink
Server-side login via handler, fix build error (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
ky28059 committed Nov 19, 2024
1 parent 51a1e9a commit 72ac553
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
17 changes: 17 additions & 0 deletions app/login-handler/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { NextRequest, NextResponse } from 'next/server';
import { login } from '@/util/auth';


export async function GET(req: NextRequest) {
const params = req.nextUrl.searchParams;

const token = params.get('token');
if (!token)
return NextResponse.redirect(new URL('/login', req.url));

const res = await login(token);
if ('error' in res)
return NextResponse.redirect(new URL(`/login?error=${res.error}`, req.url));

return NextResponse.redirect(new URL('/profile', req.url));
}
20 changes: 7 additions & 13 deletions app/login/LoginContent.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client'

import { useLayoutEffect, useState } from 'react';
import { useRouter, useSearchParams } from 'next/navigation';
import { useState } from 'react';
import { useRouter } from 'next/navigation';
import Link from 'next/link';
import { login } from '@/util/auth';

Expand All @@ -12,20 +12,14 @@ import IconInput from '@/components/IconInput';
import { FaAddressCard } from 'react-icons/fa6';


export default function LoginContent() {
type LoginContentProps = {
error?: string
}
export default function LoginContent(props: LoginContentProps) {
const [teamToken, setTeamToken] = useState('');
const [error, setError] = useState('');
const [error, setError] = useState(props.error ?? '');

const router = useRouter();
const params = useSearchParams();

// Automatically sign in if the `token` URL search parameter is set.
useLayoutEffect(() => {
const token = params.get('token');
if (!token) return;

void loginCallback(token);
}, [params.get('token')]);

async function loginCallback(teamToken: string) {
const res = await login(teamToken);
Expand Down
13 changes: 11 additions & 2 deletions app/login/page.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import type { Metadata } from 'next';
import { redirect } from 'next/navigation';

// Components
import LoginContent from '@/app/login/LoginContent';


export const metadata: Metadata = {
title: 'Log in'
}

export default function Login() {
export default async function Login({ searchParams }: { searchParams: Promise<{ token?: string, error?: string }> }) {
const token = (await searchParams).token;
const error = (await searchParams).error;

// Automatically sign in if the `token` search parameter is set.
if (token) return redirect(`/login-handler?token=${token}`)

return (
<div className="container pt-32 pb-24">
<h1 className="text-2xl font-bold mb-8 text-center">
Log in to b01lers CTF
</h1>

<LoginContent />
<LoginContent error={error} />
</div>
)
}

0 comments on commit 72ac553

Please sign in to comment.