feat: tambah dependensi 'jose' versi 5.9.2 pada package.json
refactor: rapikan identasi dan buat field 'expires' opsional di model UserSession pada schema prisma chore: bersihkan import tidak terpakai di route login dan register API
This commit is contained in:
@@ -1,18 +1,32 @@
|
||||
"use server";
|
||||
|
||||
import prisma from "@/app/lib/prisma";
|
||||
import { funGetUserIdByToken } from "@/app_modules/_global/fun/get";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export async function auth_Logout(kodeId: string) {
|
||||
cookies().set({
|
||||
name: "ssn",
|
||||
value: "",
|
||||
maxAge: 0,
|
||||
});
|
||||
|
||||
const c = cookies().get("ssn");
|
||||
if (c?.value !== "") return { status: 400, message: "Gagal Logout" };
|
||||
const userId = await funGetUserIdByToken();
|
||||
|
||||
try {
|
||||
const delToken = await prisma.userSession.delete({
|
||||
where: {
|
||||
userId: userId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!delToken) return { status: 400, message: "Gagal Hapus User Session" };
|
||||
cookies().set({
|
||||
name: "ssn",
|
||||
value: "",
|
||||
maxAge: 0,
|
||||
expires: 0,
|
||||
});
|
||||
|
||||
return { status: 200, message: "Logout Berhasil" };
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
// const del = await prisma.kodeOtp.delete({
|
||||
// where: {
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
"use server";
|
||||
|
||||
import { PwdCookies } from "@/app/lib";
|
||||
import prisma from "@/app/lib/prisma";
|
||||
import { sealData } from "iron-session";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
|
||||
export async function Auth_funRegister(data: any) {
|
||||
|
||||
export async function Auth_funRegister({
|
||||
data,
|
||||
HIPMI_PWD,
|
||||
}: {
|
||||
data: any;
|
||||
HIPMI_PWD: string;
|
||||
}) {
|
||||
const cekUsername = await prisma.user.findUnique({
|
||||
where: {
|
||||
username: data.username,
|
||||
@@ -28,21 +31,31 @@ export async function Auth_funRegister(data: any) {
|
||||
});
|
||||
if (!create) return { status: 400, message: "Gagal Mendaftar" };
|
||||
|
||||
const seal = await sealData(
|
||||
const sealToken = await sealData(
|
||||
JSON.stringify({
|
||||
id: create.id,
|
||||
username: create.username,
|
||||
}),
|
||||
{
|
||||
password: PwdCookies
|
||||
password: HIPMI_PWD,
|
||||
}
|
||||
);
|
||||
|
||||
cookies().set({
|
||||
name: "ssn",
|
||||
value: seal,
|
||||
maxAge: 60 * 60 * 24 * 7,
|
||||
value: sealToken,
|
||||
// maxAge: 60 * 60 * 24 * 7,
|
||||
});
|
||||
|
||||
const createUserSession = await prisma.userSession.create({
|
||||
data: {
|
||||
token: sealToken,
|
||||
userId: create.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!createUserSession)
|
||||
return { status: 400, message: "Gagal Membuat User Session" };
|
||||
|
||||
return { status: 200, message: "Berhasil Mendaftar" };
|
||||
}
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
"use server";
|
||||
|
||||
import prisma from "@/app/lib/prisma";
|
||||
import { sealData } from "iron-session";
|
||||
import { cookies } from "next/headers";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { RouterHome } from "@/app/lib/router_hipmi/router_home";
|
||||
import { PwdCookies } from "@/app/lib";
|
||||
import { sealData, unsealData } from "iron-session";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
|
||||
export async function auth_funValidasi(nomor: string) {
|
||||
const cek = await prisma.user.findUnique({
|
||||
export async function auth_funValidasi({
|
||||
nomor,
|
||||
HIPMI_PWD,
|
||||
}: {
|
||||
nomor: string;
|
||||
HIPMI_PWD: string;
|
||||
}) {
|
||||
const cekUser = await prisma.user.findUnique({
|
||||
where: {
|
||||
nomor: nomor,
|
||||
},
|
||||
@@ -21,30 +25,44 @@ export async function auth_funValidasi(nomor: string) {
|
||||
},
|
||||
});
|
||||
|
||||
if (cek === null) return { status: 400, message: "Nomor Belum Terdaftar" };
|
||||
if (cek) {
|
||||
const res = await sealData(
|
||||
JSON.stringify({
|
||||
id: cek.id,
|
||||
username: cek.username,
|
||||
}),
|
||||
{
|
||||
password: PwdCookies,
|
||||
}
|
||||
);
|
||||
if (cekUser === null) return { status: 400, message: "Nomor Belum Terdaftar" };
|
||||
|
||||
cookies().set({
|
||||
name: "ssn",
|
||||
value: res,
|
||||
maxAge: 60 * 60 * 24 * 30,
|
||||
const sealToken = await sealData(
|
||||
JSON.stringify({
|
||||
id: cekUser.id,
|
||||
username: cekUser.username,
|
||||
}),
|
||||
{
|
||||
password: HIPMI_PWD,
|
||||
}
|
||||
);
|
||||
|
||||
cookies().set({
|
||||
name: "ssn",
|
||||
value: sealToken,
|
||||
// maxAge: 60 * 60 * 24 * 30,
|
||||
// expires: 60 * 60 * 24 * 30,
|
||||
});
|
||||
|
||||
try {
|
||||
const createUserSession = await prisma.userSession.create({
|
||||
data: {
|
||||
token: sealToken,
|
||||
userId: cekUser.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!createUserSession)
|
||||
return { status: 401, message: "Gagal Membuat User Session" };
|
||||
|
||||
revalidatePath(RouterHome.main_home);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
return {
|
||||
status: 200,
|
||||
message: "Nomor Terverifikasi",
|
||||
role: cek.masterUserRoleId,
|
||||
role: cekUser.masterUserRoleId,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -6,8 +6,6 @@ import {
|
||||
MainColor,
|
||||
} from "@/app_modules/_global/color/color_pallet";
|
||||
import ComponentGlobal_ErrorInput from "@/app_modules/_global/component/error_input";
|
||||
import { ComponentGlobal_NotifikasiBerhasil } from "@/app_modules/_global/notif_global/notifikasi_berhasil";
|
||||
import { ComponentGlobal_NotifikasiPeringatan } from "@/app_modules/_global/notif_global/notifikasi_peringatan";
|
||||
import { auth_funLogin } from "@/app_modules/auth/fun/fun_login";
|
||||
import {
|
||||
BackgroundImage,
|
||||
@@ -17,18 +15,20 @@ import {
|
||||
Text,
|
||||
Title,
|
||||
} from "@mantine/core";
|
||||
import { useFocusTrap } from "@mantine/hooks";
|
||||
import { useAtom } from "jotai";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useState } from "react";
|
||||
import { PhoneInput } from "react-international-phone";
|
||||
import "react-international-phone/style.css";
|
||||
import { gs_kodeId } from "../state/state";
|
||||
import {
|
||||
ComponentGlobal_NotifikasiBerhasil,
|
||||
ComponentGlobal_NotifikasiPeringatan,
|
||||
} from "@/app_modules/_global/notif_global";
|
||||
|
||||
export default function Login() {
|
||||
const router = useRouter();
|
||||
const [kodeId, setKodeId] = useAtom(gs_kodeId);
|
||||
const focusTrapRef = useFocusTrap();
|
||||
const [phone, setPhone] = useState("");
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [isError, setError] = useState(false);
|
||||
@@ -38,16 +38,15 @@ export default function Login() {
|
||||
|
||||
if (nomorHp.length <= 4) return setError(true);
|
||||
|
||||
await auth_funLogin(nomorHp).then((res) => {
|
||||
if (res.status === 200) {
|
||||
setLoading(true);
|
||||
ComponentGlobal_NotifikasiBerhasil(res.message, 2000);
|
||||
setKodeId(res.kodeOtpId);
|
||||
router.push(RouterAuth.validasi + res.kodeOtpId);
|
||||
} else {
|
||||
ComponentGlobal_NotifikasiPeringatan(res.message);
|
||||
}
|
||||
});
|
||||
const res = await auth_funLogin(nomorHp);
|
||||
if (res.status === 200) {
|
||||
setLoading(true);
|
||||
ComponentGlobal_NotifikasiBerhasil(res.message, 2000);
|
||||
setKodeId(res.kodeOtpId);
|
||||
router.push(RouterAuth.validasi + res.kodeOtpId);
|
||||
} else {
|
||||
ComponentGlobal_NotifikasiPeringatan(res.message);
|
||||
}
|
||||
|
||||
// await fetch(ApiHipmi.login, {
|
||||
// method: "POST",
|
||||
|
||||
@@ -4,12 +4,7 @@ import { Warna } from "@/app/lib/warna";
|
||||
import { ComponentGlobal_NotifikasiBerhasil } from "@/app_modules/_global/notif_global/notifikasi_berhasil";
|
||||
import { ComponentGlobal_NotifikasiPeringatan } from "@/app_modules/_global/notif_global/notifikasi_peringatan";
|
||||
import UIGlobal_Modal from "@/app_modules/_global/ui/ui_modal";
|
||||
import {
|
||||
ActionIcon,
|
||||
Button,
|
||||
Stack,
|
||||
Text
|
||||
} from "@mantine/core";
|
||||
import { ActionIcon, Button, Stack, Text } from "@mantine/core";
|
||||
import { IconLogout } from "@tabler/icons-react";
|
||||
import { useAtom } from "jotai";
|
||||
import { useRouter } from "next/navigation";
|
||||
@@ -24,9 +19,6 @@ export default function Component_Logout() {
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
async function onClickLogout() {
|
||||
// await auth_Logout(kodeId).then((res) => {
|
||||
// ComponentGlobal_NotifikasiBerhasil("Berhasil Logout");
|
||||
// });
|
||||
await auth_Logout(kodeId).then((res) => {
|
||||
if (res.status === 200) {
|
||||
ComponentGlobal_NotifikasiBerhasil(res.message);
|
||||
|
||||
@@ -15,17 +15,16 @@ import {
|
||||
Stack,
|
||||
Text,
|
||||
TextInput,
|
||||
Title
|
||||
Title,
|
||||
} from "@mantine/core";
|
||||
import { useFocusTrap } from "@mantine/hooks";
|
||||
import {
|
||||
IconUserCircle
|
||||
} from "@tabler/icons-react";
|
||||
import { IconUserCircle } from "@tabler/icons-react";
|
||||
import _ from "lodash";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useState } from "react";
|
||||
import { auth_funDeleteAktivasiKodeOtpById } from "../fun/fun_edit_aktivasi_kode_otp_by_id";
|
||||
import { Auth_funRegister } from "../fun/fun_register";
|
||||
import { GlobalEnv } from "@/app/lib/token";
|
||||
|
||||
export default function Register({ dataOtp }: { dataOtp: any }) {
|
||||
const router = useRouter();
|
||||
@@ -40,7 +39,6 @@ export default function Register({ dataOtp }: { dataOtp: any }) {
|
||||
username: value,
|
||||
nomor: nomor,
|
||||
};
|
||||
// console.log(body);
|
||||
|
||||
if (body.username === "") {
|
||||
setIsValue(true);
|
||||
@@ -49,7 +47,7 @@ export default function Register({ dataOtp }: { dataOtp: any }) {
|
||||
if (body.username.length < 5) return null;
|
||||
if (_.values(body.username).includes(" ")) return null;
|
||||
|
||||
const res = await Auth_funRegister(body)
|
||||
const res = await Auth_funRegister({data: body, HIPMI_PWD: GlobalEnv.value?.WIBU_PWD as string});
|
||||
if (res.status === 200) {
|
||||
await auth_funDeleteAktivasiKodeOtpById(dataOtp.id).then((val) => {
|
||||
if (val.status === 200) {
|
||||
@@ -67,16 +65,12 @@ export default function Register({ dataOtp }: { dataOtp: any }) {
|
||||
|
||||
return (
|
||||
<>
|
||||
{/* <pre>{JSON.stringify(dataOtp,null,2)}</pre> */}
|
||||
<BackgroundImage
|
||||
src={"/aset/global/main_background.png"}
|
||||
h={"100vh"}
|
||||
// pos={"static"}
|
||||
>
|
||||
<BackgroundImage src={"/aset/global/main_background.png"} h={"100vh"}>
|
||||
<Center h={"100vh"}>
|
||||
<Stack h={"100%"} align="center" justify="center" spacing={70}>
|
||||
<Title order={2} c={MainColor.yellow}>
|
||||
REGISTRASI
|
||||
{GlobalEnv.value?.DATABASE_URL}
|
||||
</Title>
|
||||
|
||||
<IconUserCircle size={100} color="white" />
|
||||
|
||||
@@ -26,6 +26,7 @@ import { useRouter } from "next/navigation";
|
||||
import { useState } from "react";
|
||||
import { auth_funDeleteAktivasiKodeOtpById } from "../fun/fun_edit_aktivasi_kode_otp_by_id";
|
||||
import { auth_funValidasi } from "../fun/fun_validasi";
|
||||
import { GlobalEnv } from "@/app/lib/token";
|
||||
|
||||
export default function Validasi({ dataOtp }: { dataOtp: any }) {
|
||||
const router = useRouter();
|
||||
@@ -41,9 +42,11 @@ export default function Validasi({ dataOtp }: { dataOtp: any }) {
|
||||
if (code != inputCode)
|
||||
return ComponentGlobal_NotifikasiPeringatan("Kode Salah");
|
||||
|
||||
const res = await auth_funValidasi(nomor);
|
||||
const res = await auth_funValidasi({
|
||||
nomor: nomor,
|
||||
HIPMI_PWD: GlobalEnv.value?.WIBU_PWD as string,
|
||||
});
|
||||
if (res.status === 200) {
|
||||
|
||||
const resAktivasi = await auth_funDeleteAktivasiKodeOtpById(dataOtp.id);
|
||||
if (resAktivasi.status === 200) {
|
||||
if (res.role === "1") {
|
||||
@@ -51,12 +54,13 @@ export default function Validasi({ dataOtp }: { dataOtp: any }) {
|
||||
setLoading(true);
|
||||
router.push(RouterHome.main_home, { scroll: false });
|
||||
} else {
|
||||
router.push(RouterAdminDashboard.splash_admin);
|
||||
ComponentGlobal_NotifikasiBerhasil("Admin Logged in");
|
||||
setLoading(true);
|
||||
router.push(RouterAdminDashboard.splash_admin, { scroll: false });
|
||||
}
|
||||
} else {
|
||||
ComponentGlobal_NotifikasiPeringatan(resAktivasi.message);
|
||||
}
|
||||
|
||||
} else {
|
||||
ComponentGlobal_NotifikasiBerhasil(res.message);
|
||||
router.push(RouterAuth.register + dataOtp.id, { scroll: false });
|
||||
|
||||
Reference in New Issue
Block a user