71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
"use server";
|
|
import { redirect } from "next/navigation";
|
|
import { cookies } from "next/headers";
|
|
|
|
const SIGNUP_URL = "https://tachy.daoyoucloud.com/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 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) {
|
|
const result = await fetch(SIGNUP_URL, {
|
|
method: "POST",
|
|
headers: {
|
|
"X-App": "danmu-sim",
|
|
"content-type": "application/json",
|
|
"x-authenticator": "basic",
|
|
"x-locale": "zh-CN",
|
|
"x-timezone": "+08:00",
|
|
},
|
|
body: JSON.stringify(Object.fromEntries(formData)),
|
|
});
|
|
const response = await result.json();
|
|
if (response.errors) {
|
|
return response.errors[0].message;
|
|
}
|
|
redirect("/signin");
|
|
}
|