feat: add mfa on manage user

This commit is contained in:
Wikzyy
2025-06-20 17:30:51 +07:00
parent 3b60b8ee30
commit 61c09ea7aa
3 changed files with 65 additions and 21 deletions

View File

@ -1,9 +1,12 @@
import { getAuth } from '@/auth';
import { BasicSettings, Password } from './blocks'; import { BasicSettings, Password } from './blocks';
import GenerateQr from './blocks/GenerateQr'; import GenerateQr from './blocks/GenerateQr';
import { PinCode } from './blocks/PinCode'; import { PinCode } from './blocks/PinCode';
import { AccountUserProfileContextProvider } from './hooks'; import { AccountUserProfileContextProvider } from './hooks';
const AccountUserProfileContent = () => { const AccountUserProfileContent = () => {
const parsedUser = getAuth()?.user;
return ( return (
<div className="min-h-screen bg-gradient-to-br from-slate-50 via-white to-red-100 rounded-lg"> <div className="min-h-screen bg-gradient-to-br from-slate-50 via-white to-red-100 rounded-lg">
<div className="max-w-7xl mx-auto px-4 py-8"> <div className="max-w-7xl mx-auto px-4 py-8">
@ -22,7 +25,7 @@ const AccountUserProfileContent = () => {
{/* Right Column - MFA Setup */} {/* Right Column - MFA Setup */}
<div className="xl:col-span-1"> <div className="xl:col-span-1">
<GenerateQr /> <GenerateQr idCustomer={parsedUser.id} />
</div> </div>
</div> </div>
</AccountUserProfileContextProvider> </AccountUserProfileContextProvider>

View File

@ -7,12 +7,17 @@ import { RefreshCw, Shield, Smartphone, QrCode } from 'lucide-react';
import { useState } from 'react'; import { useState } from 'react';
import { toast } from 'sonner'; import { toast } from 'sonner';
import clsx from 'clsx'; import clsx from 'clsx';
import { useUserContext } from '@/pages/settings/user/manage-user/hooks';
const API_URL = apiConfig.service_dashboard; const API_URL = apiConfig.service_dashboard;
const GenerateQr = () => { type QrProps = {
idCustomer: string | null;
};
const GenerateQr = ({ idCustomer }: QrProps) => {
const { GetData, PostData } = useCallApi(); const { GetData, PostData } = useCallApi();
const parsedUser = getAuth()?.user; const { setShowQr } = useUserContext();
const [qrCodeUrl, setQrCodeUrl] = useState<string | null>(null); const [qrCodeUrl, setQrCodeUrl] = useState<string | null>(null);
const [inputToken, setInputToken] = useState(''); const [inputToken, setInputToken] = useState('');
const [loading, setLoading] = useState<boolean>(false); const [loading, setLoading] = useState<boolean>(false);
@ -20,7 +25,7 @@ const GenerateQr = () => {
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const getQrCode = async () => { const getQrCode = async () => {
if (!parsedUser?.id) { if (!idCustomer) {
toast.error('User ID not found'); toast.error('User ID not found');
return; return;
} }
@ -29,7 +34,7 @@ const GenerateQr = () => {
setError(null); setError(null);
try { try {
const response = await GetData(`${API_URL}/user/generate_mfa/${parsedUser.id}`, {}); const response = await GetData(`${API_URL}/user/generate_mfa/${idCustomer}`, {});
if (response?.status) { if (response?.status) {
setQrCodeUrl(response.data.token_base64); setQrCodeUrl(response.data.token_base64);
toast.success('QR Code generated successfully'); toast.success('QR Code generated successfully');
@ -45,7 +50,7 @@ const GenerateQr = () => {
}; };
const verifyToken = async () => { const verifyToken = async () => {
if (!parsedUser?.id) { if (!idCustomer) {
toast.error('User ID not found'); toast.error('User ID not found');
return; return;
} }
@ -54,7 +59,7 @@ const GenerateQr = () => {
try { try {
const response = await PostData(`${API_URL}/user/verify_mfa`, { const response = await PostData(`${API_URL}/user/verify_mfa`, {
id: parsedUser.id, id: idCustomer,
token: inputToken token: inputToken
}); });
@ -69,6 +74,7 @@ const GenerateQr = () => {
} finally { } finally {
setIsSubmitting(false); setIsSubmitting(false);
setQrCodeUrl(null); setQrCodeUrl(null);
setShowQr(false);
} }
}; };

View File

@ -6,6 +6,15 @@ import { DataGridColumnHeader, DataGridProvider, KeenIcon } from '@/components';
import { EnforceSwitch } from '@/components/switch'; import { EnforceSwitch } from '@/components/switch';
import { ListToolBar } from '../blocks'; import { ListToolBar } from '../blocks';
import { useCallApi } from '@/hooks'; import { useCallApi } from '@/hooks';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger
} from '@/components/ui/dropdown-menu';
import { MoreHorizontal, MoreVertical, Pencil, QrCode, Trash } from 'lucide-react';
import GenerateQr from '@/pages/account/home/user-profile/blocks/GenerateQr';
import { Dialog, DialogContent, DialogTrigger } from '@/components/ui/dialog';
interface ContextProps { interface ContextProps {
showSearchDialog: boolean; showSearchDialog: boolean;
@ -17,6 +26,8 @@ interface ContextProps {
showDeleteDialog: boolean; showDeleteDialog: boolean;
handleDeleteDialog: (show: boolean, selected_user: string | null) => void; handleDeleteDialog: (show: boolean, selected_user: string | null) => void;
selectedUser: string | null; selectedUser: string | null;
showQr: boolean;
setShowQr: React.Dispatch<React.SetStateAction<boolean>>;
} }
interface SelectedUser { interface SelectedUser {
@ -39,7 +50,9 @@ const initialProps: ContextProps = {
handleAddDialog: () => {}, handleAddDialog: () => {},
showDeleteDialog: false, showDeleteDialog: false,
handleDeleteDialog: () => {}, handleDeleteDialog: () => {},
selectedUser: null selectedUser: null,
showQr: false,
setShowQr: () => {}
}; };
const ManageUserContext = createContext<ContextProps>(initialProps); const ManageUserContext = createContext<ContextProps>(initialProps);
@ -52,6 +65,7 @@ const ManageUserContextProvider = ({ children }: { children: React.ReactNode })
const [showSearchDialog, setShowSearchDialog] = useState(false); const [showSearchDialog, setShowSearchDialog] = useState(false);
const [showAddDialog, setShowAddDialog] = useState(false); const [showAddDialog, setShowAddDialog] = useState(false);
const [showDeleteDialog, setShowDeleteDialog] = useState(false); const [showDeleteDialog, setShowDeleteDialog] = useState(false);
const [showQr, setShowQr] = useState(false);
const [selectedUser, setSelectedUser] = useState<string | null>(null); const [selectedUser, setSelectedUser] = useState<string | null>(null);
const { GetData } = useCallApi(); const { GetData } = useCallApi();
@ -165,18 +179,32 @@ const ManageUserContextProvider = ({ children }: { children: React.ReactNode })
return ( return (
<> <>
<button <DropdownMenu>
className="btn btn-sm btn-icon btn-clear btn-light" <DropdownMenuTrigger asChild>
onClick={() => handleEditDialog(true, row.id)} <button className="btn btn-sm btn-icon btn-light">
> <MoreHorizontal className="w-4 h-4" />
<KeenIcon icon="notepad-edit" /> </button>
</button> </DropdownMenuTrigger>
<button <DropdownMenuContent align="end" className="w-48">
className="btn btn-sm btn-icon btn-clear btn-light" <DropdownMenuItem
onClick={() => handleDeleteDialog(true, row.id)} onClick={() => {
> setShowQr(true);
<KeenIcon icon="trash" /> setSelectedUser(row.id);
</button> }}
>
<QrCode className="w-4 h-4 mr-2" />
QR Code
</DropdownMenuItem>
<DropdownMenuItem onClick={() => handleEditDialog(true, row.id)}>
<Pencil className="w-4 h-4 mr-2" />
Edit
</DropdownMenuItem>
<DropdownMenuItem onClick={() => handleDeleteDialog(true, row.id)}>
<Trash className="w-4 h-4 mr-2 text-red-500" />
<span className="text-red-500">Delete</span>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</> </>
); );
}, },
@ -229,7 +257,9 @@ const ManageUserContextProvider = ({ children }: { children: React.ReactNode })
showAddDialog, showAddDialog,
handleAddDialog, handleAddDialog,
showDeleteDialog, showDeleteDialog,
handleDeleteDialog handleDeleteDialog,
showQr,
setShowQr
}} }}
> >
<DataGridProvider <DataGridProvider
@ -243,6 +273,11 @@ const ManageUserContextProvider = ({ children }: { children: React.ReactNode })
doGetListData(pageIndex, pageSize, sorting, columnFilters) doGetListData(pageIndex, pageSize, sorting, columnFilters)
} }
> >
<Dialog open={showQr} onOpenChange={setShowQr}>
<DialogContent>
<GenerateQr idCustomer={selectedUser} />
</DialogContent>
</Dialog>
{children} {children}
</DataGridProvider> </DataGridProvider>
</ManageUserContext.Provider> </ManageUserContext.Provider>