"use client"; import React, { useState } from "react"; import Link from "next/link"; import Cookies from "js-cookie"; import { close, error, loading } from "@/config/swal"; import { useRouter } from "next/navigation"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; import { checkUsernames, emailValidation, getProfile, postSignIn, setupEmail, } from "@/service/master-user"; import { Input } from "../ui/input"; import { Button } from "../ui/button"; import { Label } from "../ui/label"; import { EyeFilledIcon, EyeSlashFilledIcon } from "../icons"; import { saveActivity } from "@/service/activity-log"; export default function Login() { const router = useRouter(); const [isVisible, setIsVisible] = useState(false); const [isVisibleSetup, setIsVisibleSetup] = useState([false, false]); const [oldEmail, setOldEmail] = useState(""); const [newEmail, setNewEmail] = useState(""); const [passwordSetup, setPasswordSetup] = useState(""); const [confPasswordSetup, setConfPasswordSetup] = useState(""); const toggleVisibility = () => setIsVisible(!isVisible); const [needOtp, setNeedOtp] = useState(false); const [isFirstLogin, setFirstLogin] = useState(false); const [otpValue, setOtpValue] = useState(""); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [accessData, setAccessData] = useState(); const [profile, setProfile] = useState(); const [isValidEmail, setIsValidEmail] = useState(false); const [isResetPassword, setIsResetPassword] = useState(false); const [checkUsernameValue, setCheckUsernameValue] = useState(""); const MySwal = withReactContent(Swal); const setValUsername = (e: any) => { const uname = e.replaceAll(/[^\w.-]/g, ""); setUsername(uname.toLowerCase()); }; const onSubmit = async () => { const data = { username: username, password: password, }; if (!username || !password) { error("Username & Password Wajib Diisi !"); } else { loading(); const response = await postSignIn(data); if (response?.error) { error("Username / Password Tidak Sesuai"); } else { const profile = await getProfile(response?.data?.data?.access_token); const dateTime: any = new Date(); const newTime: any = dateTime.getTime() + 10 * 60 * 1000; Cookies.set("access_token", response?.data?.data?.access_token, { expires: 1, }); Cookies.set("refresh_token", response?.data?.data?.refresh_token, { expires: 1, }); Cookies.set("time_refresh", newTime, { expires: 1, }); Cookies.set("is_first_login", "true", { secure: true, sameSite: "strict", }); await saveActivity( { activityTypeId: 1, url: "https://dev.mikulnews.com/auth", userId: profile?.data?.data?.id, }, response?.data?.data?.access_token ); Cookies.set("profile_picture", profile?.data?.data?.profilePictureUrl, { expires: 1, }); Cookies.set("uie", profile?.data?.data?.id, { expires: 1, }); Cookies.set("ufne", profile?.data?.data?.fullname, { expires: 1, }); Cookies.set("ulie", profile?.data?.data?.userLevelGroup, { expires: 1, }); Cookies.set("username", profile?.data?.data?.username, { expires: 1, }); Cookies.set("fullname", profile?.data?.data?.fullname, { expires: 1, }); Cookies.set("urie", profile?.data?.data?.roleId, { expires: 1, }); Cookies.set("roleName", profile?.data?.data?.roleName, { expires: 1, }); Cookies.set("masterPoldaId", profile?.data?.data?.masterPoldaId, { expires: 1, }); Cookies.set("ulne", profile?.data?.data?.userLevelId, { expires: 1, }); Cookies.set("urce", profile?.data?.data?.roleCode, { expires: 1, }); Cookies.set("email", profile?.data?.data?.email, { expires: 1, }); router.push("/admin/dashboard"); Cookies.set("status", "login", { expires: 1, }); close(); } } }; const checkUsername = async () => { const res = await checkUsernames(checkUsernameValue); if (res?.error) { error("Username tidak ditemukan"); return false; } MySwal.fire({ title: "", text: "", html: ( <>

Kami telah mengirimkan tautan untuk mengatur ulang kata sandi ke email Anda

Apakah Anda sudah menerima emailnya? Jika belum, periksa folder spam Anda

), icon: "info", cancelButtonColor: "#d33", confirmButtonColor: "#3085d6", confirmButtonText: "Oke", }).then((result) => { if (result.isConfirmed) { } }); }; const submitCheckEmail = async () => { const req = { oldEmail: oldEmail, newEmail: newEmail, username: username, password: password, }; const res = await setupEmail(req); if (res?.error) { if (res.message?.messages[0]) { error(res.message?.messages[0]); } else { error(res?.message); } return false; } close(); setNeedOtp(true); setFirstLogin(false); }; return (
{/* Left Side - Logo Section */}
Warga Bicara Logo

Portal Warga Bicara

Platform berita terpercaya untuk informasi terkini

{/* Decorative elements */}
{/* Right Side - Login Form */}
{/* Mobile Logo */}
Warga Bicara Logo
{isFirstLogin ? (

Setup Akun

Lengkapi informasi email Anda

setOldEmail(e.target.value)} />
setNewEmail(e.target.value)} />
) : needOtp ? (

Verifikasi OTP

Masukkan kode OTP yang telah dikirim

) : isResetPassword ? (

Reset Password

Masukkan username untuk reset password

setCheckUsernameValue(e.target.value.trim()) } onPaste={(e) => setCheckUsernameValue(e.currentTarget.value.trim()) } onCopy={(e) => setCheckUsernameValue(e.currentTarget.value.trim()) } />
Beranda
) : (

Selamat Datang

Portal Warga Bicara - Platform berita terpercaya

setValUsername(e.target.value.trim())} onPaste={(e) => setValUsername(e.currentTarget.value.trim()) } onCopy={(e) => setValUsername(e.currentTarget.value.trim())} />
setPassword(e.target.value)} />
Beranda
{/*

Informasi Portal

Akses informasi terkini dan status permintaan informasi yang telah diajukan.

*/}
)}
); }