feat: images upload
This commit is contained in:
+41
-31
@@ -4,16 +4,18 @@ import { generatePkce, generateState } from "@/lib/auth/pkce";
|
||||
import { pkceConfig } from "@/lib/cookieConfig";
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import { deleteSession } from "@/lib/auth/session";
|
||||
import { deleteSession, getSessionUserId } from "@/lib/auth/session";
|
||||
import { deleteUser, updateUser } from "./repository";
|
||||
import { getAccessToken } from "@/lib/auth/tokens";
|
||||
import { fetchKotosoftUser } from "@/lib/kotosoft/client";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { getCurrentUser } from "../user";
|
||||
import { runAction } from "@/lib/run-action";
|
||||
import { AppError, type ActionResult } from "@/lib/action-result";
|
||||
|
||||
export async function login() {
|
||||
const { verifier, challenge } = generatePkce();
|
||||
const state = generateState();
|
||||
|
||||
const store = await cookies();
|
||||
store.set("pkce_verifier", verifier, pkceConfig);
|
||||
store.set("oauth_state", state, pkceConfig);
|
||||
@@ -31,34 +33,42 @@ export async function login() {
|
||||
redirect(`${process.env.KOTOSOFT_ID_API_URL}/oauth/authorize?${params}`);
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
await deleteSession();
|
||||
redirect("/");
|
||||
}
|
||||
|
||||
export async function deleteUserAction(userId: string) {
|
||||
await deleteUser(userId);
|
||||
await deleteSession();
|
||||
redirect("/");
|
||||
}
|
||||
|
||||
export async function updateUserAction() {
|
||||
const accessToken = await getAccessToken();
|
||||
if (!accessToken) throw new Error("Not authenticated");
|
||||
|
||||
const kotosoftUser = await fetchKotosoftUser(accessToken);
|
||||
|
||||
const { getSessionUserId } = await import("@/lib/auth/session");
|
||||
const userId = await getSessionUserId();
|
||||
if (!userId) throw new Error("Not authenticated");
|
||||
|
||||
await updateUser(userId, {
|
||||
email: kotosoftUser.email,
|
||||
firstName: kotosoftUser.first_name,
|
||||
secondName: kotosoftUser.second_name,
|
||||
sex: kotosoftUser.sex as "MALE" | "FEMALE",
|
||||
birthDate: kotosoftUser.birth_date,
|
||||
export async function logout(): Promise<ActionResult> {
|
||||
return runAction(async () => {
|
||||
await deleteSession();
|
||||
redirect("/");
|
||||
});
|
||||
|
||||
revalidatePath("/account")
|
||||
}
|
||||
|
||||
export async function deleteUserAction(): Promise<ActionResult> {
|
||||
return runAction(async () => {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) throw new AppError("Not authenticated");
|
||||
|
||||
await deleteUser(user.id);
|
||||
await deleteSession();
|
||||
redirect("/");
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateUserAction(): Promise<ActionResult> {
|
||||
return runAction(async () => {
|
||||
const accessToken = await getAccessToken();
|
||||
if (!accessToken) throw new AppError("Not authenticated");
|
||||
|
||||
const kotosoftUser = await fetchKotosoftUser(accessToken);
|
||||
|
||||
const userId = await getSessionUserId();
|
||||
if (!userId) throw new AppError("Not authenticated");
|
||||
|
||||
await updateUser(userId, {
|
||||
email: kotosoftUser.email,
|
||||
firstName: kotosoftUser.first_name,
|
||||
secondName: kotosoftUser.second_name,
|
||||
sex: kotosoftUser.sex as "MALE" | "FEMALE",
|
||||
birthDate: kotosoftUser.birth_date,
|
||||
});
|
||||
|
||||
revalidatePath("/account");
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { getCurrentUser } from "@/lib/user";
|
||||
import { redirect } from "next/navigation";
|
||||
import { AppError } from "@/lib/action-result";
|
||||
|
||||
export async function requireUser() {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) redirect("/");
|
||||
return user;
|
||||
}
|
||||
|
||||
export async function requireAdmin() {
|
||||
const user = await requireUser();
|
||||
if (!user.isAdmin) throw new AppError("You do not have permission to perform this action.");
|
||||
return user;
|
||||
}
|
||||
Reference in New Issue
Block a user