Use token from cookies directly

This commit is contained in:
2025-09-27 14:31:17 +02:00
parent aec851f0d1
commit 72171c4dfb
5 changed files with 24 additions and 20 deletions
+8 -1
View File
@@ -40,7 +40,14 @@ export async function GET(req: NextRequest) {
} }
export async function POST(req: NextRequest) { export async function POST(req: NextRequest) {
let { server_ip, session_token } = await req.json(); let { server_ip } = await req.json();
const session_token = req.cookies.get("token")?.value;
if (!session_token)
return NextResponse.json(
{ message: "Token cookie not found" },
{ status: StatusCodes.UNAUTHORIZED }
);
const token = crypto.randomUUID(); const token = crypto.randomUUID();
+1 -1
View File
@@ -76,7 +76,7 @@ export async function PUT(req: NextRequest) {
export async function GET(req: NextRequest) { export async function GET(req: NextRequest) {
const url = new URL(req.url); const url = new URL(req.url);
let query_id = url.searchParams.get("id"); let query_id = url.searchParams.get("id");
const token = url.searchParams.get("token"); const token = req.cookies.get("token")?.value;
if (!query_id && !token) if (!query_id && !token)
return NextResponse.json( return NextResponse.json(
+13 -9
View File
@@ -11,6 +11,12 @@ export default function Login() {
const [username, setUsername] = useState(""); const [username, setUsername] = useState("");
const [password, setPassword] = useState(""); const [password, setPassword] = useState("");
const login = async () => {
const res = await axios.put("/api/user", { username, password });
Cookies.set("token", (res.data as any).token);
redirect("/");
};
return ( return (
<div className="h-screen flex items-center"> <div className="h-screen flex items-center">
<div className="w-md mx-auto p-7 rounded-2xl flex flex-col gap-4 bg-neutral-900"> <div className="w-md mx-auto p-7 rounded-2xl flex flex-col gap-4 bg-neutral-900">
@@ -21,17 +27,15 @@ export default function Login() {
</span> </span>
<span> <span>
Password Password
<Input onChange={(e) => setPassword(e.target.value)} /> <Input
onChange={(e) => setPassword(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter") login();
}}
/>
</span> </span>
<Button <Button className="mt-4" onClick={login}>
className="mt-4"
onClick={async () => {
const res = await axios.put("/api/user", { username, password });
Cookies.set("token", (res.data as any).token);
redirect("/");
}}
>
Login Login
</Button> </Button>
</div> </div>
+1 -4
View File
@@ -23,10 +23,7 @@ export default function Server() {
async function auth() { async function auth() {
const server_auth = ( const server_auth = (
( (
await axios.post("/api/auth", { await axios.post("/api/auth", { server_ip: ip })
server_ip: ip,
session_token: Cookies.get("token"),
})
).data as any ).data as any
).token; ).token;
+1 -5
View File
@@ -25,11 +25,7 @@ export default function useUser() {
} }
try { try {
const res = ( const res = (await axios.get("/api/user")).data as User;
await axios.get("/api/user", {
params: { token },
})
).data as User;
setUser(res); setUser(res);
} catch { } catch {