From ab1bb590bfe7d781431f81517de2b260af8cfed8 Mon Sep 17 00:00:00 2001 From: Bagasbanuna02 Date: Tue, 3 Oct 2023 10:22:31 +0800 Subject: [PATCH] title: auto des: auto note:auto --- src/app/api/user/get-token/route.ts | 16 +++++++++++----- src/app_modules/home/fun/get-token.ts | 16 ++++++++++++++++ src/app_modules/home/view.tsx | 24 ++++++++++++++++-------- 3 files changed, 43 insertions(+), 13 deletions(-) create mode 100644 src/app_modules/home/fun/get-token.ts diff --git a/src/app/api/user/get-token/route.ts b/src/app/api/user/get-token/route.ts index f6cb81fd..6061a18c 100644 --- a/src/app/api/user/get-token/route.ts +++ b/src/app/api/user/get-token/route.ts @@ -1,12 +1,18 @@ -import { getConfig } from '@/bin/config'; -import { unsealData } from 'iron-session'; +import { getConfig } from "@/bin/config"; +import { unsealData } from "iron-session"; import { cookies } from "next/headers"; import { NextResponse } from "next/server"; +import yaml from "yaml"; +import fs from "fs"; +import path from 'path' +const config = yaml.parse( + fs.readFileSync("config.yaml").toString() +); export async function GET() { const c = cookies().get("ssn"); const data = JSON.parse( - await unsealData(c?.value as string, {password: ((await getConfig()).server.password)}) - ) - return NextResponse.json(data); + await unsealData(c?.value as string, { password: config.server.password }) + ); + return NextResponse.json({}); } diff --git a/src/app_modules/home/fun/get-token.ts b/src/app_modules/home/fun/get-token.ts new file mode 100644 index 00000000..9fac69d1 --- /dev/null +++ b/src/app_modules/home/fun/get-token.ts @@ -0,0 +1,16 @@ +"use server"; + +import prisma from "@/app/lib/prisma"; +import { getConfig } from "@/bin/config"; +import { unsealData } from "iron-session"; +import { cookies } from "next/headers"; + +export async function getToken() { + const c = cookies().get("ssn"); + + const token = await unsealData(c?.value as string, { + password: (await getConfig()).server.password, + }); + + return token +} diff --git a/src/app_modules/home/view.tsx b/src/app_modules/home/view.tsx index 1657e0ae..03b55209 100644 --- a/src/app_modules/home/view.tsx +++ b/src/app_modules/home/view.tsx @@ -5,24 +5,32 @@ import { Logout } from "../auth"; import { useState } from "react"; import { ApiHipmi } from "@/app/lib/api"; import { useShallowEffect } from "@mantine/hooks"; +import { getToken } from "./fun/get-token"; export default function HomeView() { const [token, setToken] = useState(null); - + + // useShallowEffect(() => { + // userToken(); + // }, []); + + // async function userToken() { + // await fetch(ApiHipmi.get_token) + // .then((res) => res.json()) + // .then((val) => setToken(val)); + // } useShallowEffect(() => { - userToken(); + getUserId(); }, []); - - async function userToken() { - await fetch(ApiHipmi.get_token) - .then((res) => res.json()) - .then((val) => setToken(val)); + async function getUserId() { + const data = await getToken(); + setToken(data); } return ( <> - {/*
{JSON.stringify(token, null, 2)}
*/} +
{JSON.stringify(token, null, 2)}
Home Welcome, {token?.username}