Use token from cookies directly
This commit is contained in:
@@ -40,7 +40,14 @@ export async function GET(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();
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ export async function PUT(req: NextRequest) {
|
||||
export async function GET(req: NextRequest) {
|
||||
const url = new URL(req.url);
|
||||
let query_id = url.searchParams.get("id");
|
||||
const token = url.searchParams.get("token");
|
||||
const token = req.cookies.get("token")?.value;
|
||||
|
||||
if (!query_id && !token)
|
||||
return NextResponse.json(
|
||||
|
||||
+13
-9
@@ -11,6 +11,12 @@ export default function Login() {
|
||||
const [username, setUsername] = 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 (
|
||||
<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">
|
||||
@@ -21,17 +27,15 @@ export default function Login() {
|
||||
</span>
|
||||
<span>
|
||||
Password
|
||||
<Input onChange={(e) => setPassword(e.target.value)} />
|
||||
<Input
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === "Enter") login();
|
||||
}}
|
||||
/>
|
||||
</span>
|
||||
|
||||
<Button
|
||||
className="mt-4"
|
||||
onClick={async () => {
|
||||
const res = await axios.put("/api/user", { username, password });
|
||||
Cookies.set("token", (res.data as any).token);
|
||||
redirect("/");
|
||||
}}
|
||||
>
|
||||
<Button className="mt-4" onClick={login}>
|
||||
Login
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
@@ -23,10 +23,7 @@ export default function Server() {
|
||||
async function auth() {
|
||||
const server_auth = (
|
||||
(
|
||||
await axios.post("/api/auth", {
|
||||
server_ip: ip,
|
||||
session_token: Cookies.get("token"),
|
||||
})
|
||||
await axios.post("/api/auth", { server_ip: ip })
|
||||
).data as any
|
||||
).token;
|
||||
|
||||
|
||||
@@ -25,11 +25,7 @@ export default function useUser() {
|
||||
}
|
||||
|
||||
try {
|
||||
const res = (
|
||||
await axios.get("/api/user", {
|
||||
params: { token },
|
||||
})
|
||||
).data as User;
|
||||
const res = (await axios.get("/api/user")).data as User;
|
||||
|
||||
setUser(res);
|
||||
} catch {
|
||||
|
||||
Reference in New Issue
Block a user