#Job done

## feat
- Fix bug login
### No issuue
This commit is contained in:
2024-03-01 14:36:02 +08:00
parent b1395a8ded
commit 0f2280f116
31 changed files with 698 additions and 192 deletions

View File

@@ -0,0 +1,18 @@
"use server";
import prisma from "@/app/lib/prisma";
export async function auth_funEditAktivasiKodeOtpById(otpId: string) {
console.log(otpId);
const updt = await prisma.kodeOtp.update({
where: {
id: otpId,
},
data: {
isActive: false,
},
});
if (!updt) return { status: 400, message: "Gagal Update Aktivasi Kode OTP" };
return { status: 200, message: "Berhasil Update Aktivasi Kode OTP" };
}

View File

@@ -0,0 +1,58 @@
"use server";
import prisma from "@/app/lib/prisma";
import { randomOTP } from "./rondom_otp";
export async function auth_funLogin(nomor: string) {
const codeOtp = randomOTP();
// const res = await fetch(
// `https://wa.wibudev.com/code?nom=${nomor}&text=Masukan Kode OTP:${codeOtp}`
// );
// const sendWa = await res.json();
// if (sendWa.status !== "success")
// return { status: 400, message: "WA Tidak Terdaftar" };
// const createOtpId = await prisma.kodeOtp.create({
// data: {
// nomor: nomor,
// otp: codeOtp,
// },
// });
// if (!createOtpId) return { status: 400, message: "Gagal Membuat Kode OTP" };
// return {
// status: 200,
// message: "Kode Verifikasi Dikirim",
// kodeOtpId: createOtpId.id
// };
try {
const res = await fetch(
`https://wa.wibudev.com/code?nom=${nomor}&text=Masukan Kode OTP:${codeOtp}`
);
const sendWa = await res.json();
if (sendWa.status !== "success")
return { status: 400, message: "WA Tidak Terdaftar" };
const createOtpId = await prisma.kodeOtp.create({
data: {
nomor: nomor,
otp: codeOtp,
},
});
if (!createOtpId) return { status: 400, message: "Gagal Membuat Kode OTP" };
return {
status: 200,
message: "Kode Verifikasi Dikirim",
kodeOtpId: createOtpId.id,
};
} catch (error) {
return { status: 500, message: "Server Error !!!" };
}
}

View File

@@ -0,0 +1,25 @@
"use server";
import prisma from "@/app/lib/prisma";
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 del = await prisma.kodeOtp.delete({
where: {
id: kodeId,
},
});
if (!del) return { status: 400, message: "Gagal Hapus Kode OTP Id" };
return { status: 200, message: "Logout Berhasil" };
}

View File

@@ -0,0 +1,49 @@
"use server";
import prisma from "@/app/lib/prisma";
import { sealData } from "iron-session";
import { cookies } from "next/headers";
import fs from "fs";
import yaml from "yaml";
const config = yaml.parse(fs.readFileSync("config.yaml").toString());
export async function Auth_funRegister(data: any) {
const cekUsername = await prisma.user.findUnique({
where: {
username: data.username,
},
});
if (cekUsername != null)
return {
status: 400,
message: "Username sudah terdaftar",
};
const create = await prisma.user.create({
data: {
username: data.username,
nomor: data.nomor,
},
});
if (!create) return { status: 400, message: "Gagal Mendaftar" };
const seal = await sealData(
JSON.stringify({
id: data.id,
username: data.username,
}),
{
password: await config.server.password,
}
);
cookies().set({
name: "ssn",
value: seal,
maxAge: 60 * 60 * 24 * 7,
});
return { status: 200, message: "Berhasil Mendaftar" };
}

View File

@@ -0,0 +1,46 @@
"use server";
import prisma from "@/app/lib/prisma";
import { sealData } from "iron-session";
import fs from "fs";
import yaml from "yaml";
import { cookies } from "next/headers";
import { revalidatePath } from "next/cache";
import { RouterHome } from "@/app/lib/router_hipmi/router_home";
const config = yaml.parse(fs.readFileSync("config.yaml").toString());
export async function auth_funValidasi(nomor: string) {
const cek = await prisma.user.findUnique({
where: {
nomor: nomor,
},
select: {
id: true,
nomor: true,
username: true,
},
});
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: await config.server.password,
}
);
cookies().set({
name: "ssn",
value: res,
maxAge: 60 * 60 * 24 * 7,
});
revalidatePath(RouterHome.main_home);
}
return { status: 200, message: "Nomor Terverivikasi" };
}

View File

@@ -0,0 +1,13 @@
"use server";
import prisma from "@/app/lib/prisma";
export async function auth_getKodeOtpById(otpId: string) {
const data = await prisma.kodeOtp.findFirst({
where: {
id: otpId,
},
});
return data
}

View File

@@ -0,0 +1,4 @@
export function randomOTP(){
const random = Math.floor(Math.random() * (9000 - 1000 )) + 1000
return random;
}