feat: citizenship applications

This commit is contained in:
Ivan Rezchikov
2026-06-23 19:03:10 +03:00
parent 58ec75c948
commit c60ab9e8f9
44 changed files with 1254 additions and 130 deletions
+64
View File
@@ -0,0 +1,64 @@
"use server"
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 { deleteUser, updateUser } from "./repository";
import { getAccessToken } from "@/lib/auth/tokens";
import { fetchKotosoftUser } from "@/lib/kotosoft/client";
import { revalidatePath } from "next/cache";
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);
const params = new URLSearchParams({
client_id: process.env.KOTOSOFT_ID_CLIENT_ID!,
redirect_uri: `${process.env.PUBLIC_URL}/api/callback`,
scope: "profile:read",
response_type: "code",
code_challenge: challenge,
code_challenge_method: "S256",
state,
});
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,
});
revalidatePath("/account")
}
+13 -2
View File
@@ -10,6 +10,12 @@ export async function findUserById(id: string) {
return prisma.user.findUnique({ where: { id } });
}
export async function findUsersByIds(ids: string[]) {
const filtered = ids.filter(Boolean);
if (filtered.length === 0) return [];
return prisma.user.findMany({ where: { id: { in: filtered } } });
}
export async function findOrCreateUser(data: UserData) {
const existing = await findUserByKotosoftId(data.kotosoftId);
if (existing) return existing;
@@ -17,8 +23,13 @@ export async function findOrCreateUser(data: UserData) {
return prisma.user.create({
data: {
id: uuidv4(),
...data,
birthDate: new Date(data.birthDate),
kotosoftId: data.kotosoftId,
email: data.email,
firstName: data.firstName,
secondName: data.secondName,
sex: data.sex,
birthDate: data.birthDate ? new Date(data.birthDate) : null,
isAdmin: data.isAdmin,
},
});
}
+2 -2
View File
@@ -3,7 +3,7 @@ export type UserData = {
email: string;
firstName: string;
secondName: string;
sex: string;
birthDate: string;
sex: "MALE" | "FEMALE";
birthDate?: string;
isAdmin: boolean;
}