From 5c6b5b976194e00fea9514d35740bbd80ef70dff Mon Sep 17 00:00:00 2001 From: sealday Date: Fri, 5 Apr 2024 15:35:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=95=B4=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E6=B3=A8=E9=94=80=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 4 +++ app/components/signout-button.tsx | 11 +++++++ app/globals.css | 4 +-- app/lib/actions.ts | 51 ++++++++++++++++++++++++++++--- app/music/page.tsx | 3 ++ app/page.tsx | 4 +++ app/signin/page.tsx | 8 ++--- middleware.ts | 20 ++++++++++++ 8 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 app/components/signout-button.tsx create mode 100644 app/music/page.tsx create mode 100644 middleware.ts diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..ddd9dae --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "typescript.suggest.paths": false, + "javascript.suggest.paths": false, +} \ No newline at end of file diff --git a/app/components/signout-button.tsx b/app/components/signout-button.tsx new file mode 100644 index 0000000..9bee84f --- /dev/null +++ b/app/components/signout-button.tsx @@ -0,0 +1,11 @@ +'use client'; +import { signout } from '@/app/lib/actions'; + +export default function SignoutButton() { + return +}; \ No newline at end of file diff --git a/app/globals.css b/app/globals.css index 875c01e..cad0201 100644 --- a/app/globals.css +++ b/app/globals.css @@ -2,7 +2,7 @@ @tailwind components; @tailwind utilities; -:root { +/* :root { --foreground-rgb: 0, 0, 0; --background-start-rgb: 214, 219, 220; --background-end-rgb: 255, 255, 255; @@ -30,4 +30,4 @@ body { .text-balance { text-wrap: balance; } -} +} */ \ No newline at end of file diff --git a/app/lib/actions.ts b/app/lib/actions.ts index 66fe415..052a8c9 100644 --- a/app/lib/actions.ts +++ b/app/lib/actions.ts @@ -1,12 +1,55 @@ 'use server'; -import axios from "axios"; +import { redirect } from 'next/navigation' +import { cookies } from 'next/headers' const SIGNUP_URL = 'https://tachy.daoyoucloud.com/api/auth:signUp'; -// const SIGNUP_URL = 'http://127.0.0.1:13000/api/auth:signUp'; +const SIGNIN_URL = 'https://tachy.daoyoucloud.com/api/auth:signIn'; +const SIGNOUT_URL = 'https://tachy.daoyoucloud.com/api/auth:signOut' -export async function authenticate(_currentState: unknown, formData: FormData) { - return ''; +export async function signout() { + const token = cookies().get('token'); + const result = await fetch(SIGNOUT_URL, { + "headers": { + "authorization": "Bearer " + token?.value, + "cache-control": "no-cache", + "pragma": "no-cache", + "x-app": "danmu-sim", + "x-authenticator": "basic", + "x-locale": "zh-CN", + "x-timezone": "+08:00", + "x-with-acl-meta": "true", + }, + "body": '', + "method": "POST" + }); + cookies().delete('token'); + redirect('/signin'); +} + + +export async function signin(_currentState: unknown, formData: FormData) { + const result = await fetch(SIGNIN_URL, { + "headers": { + "cache-control": "no-cache", + "content-type": "application/json", + "pragma": "no-cache", + "x-app": "danmu-sim", + "x-authenticator": "basic", + "x-hostname": "tachy.daoyoucloud.com", + "x-locale": "zh-CN", + "x-timezone": "+08:00", + "x-with-acl-meta": "true", + }, + "body": JSON.stringify(Object.fromEntries(formData)), + "method": "POST" + }); + const res = await result.json(); + if (res.errors) { + return res.errors[0].message; + } + cookies().set('token', res.data.token); + redirect('/'); } export async function signup(_currentState: unknown, formData: FormData) { diff --git a/app/music/page.tsx b/app/music/page.tsx new file mode 100644 index 0000000..57e519a --- /dev/null +++ b/app/music/page.tsx @@ -0,0 +1,3 @@ +export default function Page() { + return
hello world
+} \ No newline at end of file diff --git a/app/page.tsx b/app/page.tsx index dc191aa..dcc2b40 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,8 +1,12 @@ import Image from "next/image"; +import SignoutButton from "@/app/components/signout-button"; +import Link from "next/link"; export default function Home() { return (
+ + 弹幕测试

Get started by editing  diff --git a/app/signin/page.tsx b/app/signin/page.tsx index df6697c..f6c839a 100644 --- a/app/signin/page.tsx +++ b/app/signin/page.tsx @@ -1,14 +1,14 @@ 'use client' -import { authenticate } from '@/app/lib/actions' +import { signin } from '@/app/lib/actions' import { useFormState, useFormStatus } from 'react-dom' export default function Page() { - const [errorMessage, dispatch] = useFormState(authenticate, undefined) + const [errorMessage, dispatch] = useFormState(signin, undefined) return (

- +
{errorMessage &&

{errorMessage}

}
@@ -19,7 +19,7 @@ export default function Page() { function LoginButton() { const { pending } = useFormStatus() - const handleClick = (event) => { + const handleClick = (event: React.MouseEvent) => { if (pending) { event.preventDefault() } diff --git a/middleware.ts b/middleware.ts new file mode 100644 index 0000000..21a3794 --- /dev/null +++ b/middleware.ts @@ -0,0 +1,20 @@ +import type { NextRequest } from 'next/server' + +export function middleware(request: NextRequest) { + const currentUser = request.cookies.get('token')?.value + + if (currentUser && !request.nextUrl.pathname.startsWith('/')) { + return Response.redirect(new URL('/', request.url)) + } + + if (!currentUser + && !request.nextUrl.pathname.startsWith('/signin') + && !request.nextUrl.pathname.startsWith('/signup') + ) { + return Response.redirect(new URL('/signin', request.url)) + } +} + +export const config = { + matcher: ['/((?!api|_next/static|_next/image|.*\\.png$).*)'], +} \ No newline at end of file