Merge pull request #81 from bipproduction/amalia/30-jul-24
Amalia/30 jul 24
This commit is contained in:
20
src/module/auth/api/funGetUserByCookies.ts
Normal file
20
src/module/auth/api/funGetUserByCookies.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
'use server'
|
||||
|
||||
import { pwd_key_config, prisma } from "@/module/_global";
|
||||
import { unsealData } from "iron-session";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export default async function funGetUserByCookies() {
|
||||
const sessionCookie = cookies().get("sessionCookieSDM");
|
||||
const userId = await unsealData(sessionCookie!.value, {
|
||||
password: pwd_key_config,
|
||||
});
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: String(userId),
|
||||
},
|
||||
});
|
||||
|
||||
return { id: user?.id, name: user?.name, idVillage: user?.idVillage };
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import funDetectCookies from "./api/funDetectCookies";
|
||||
import funGetUserByCookies from "./api/funGetUserByCookies";
|
||||
import funSetCookies from "./api/funSetCookies";
|
||||
import ViewLogin from "./login/view/view_login";
|
||||
import ViewVerification from "./varification/view/view_verification";
|
||||
@@ -8,4 +9,5 @@ export { ViewLogin }
|
||||
export { ViewVerification }
|
||||
export { ViewWelcome }
|
||||
export { funSetCookies }
|
||||
export { funDetectCookies }
|
||||
export { funDetectCookies }
|
||||
export { funGetUserByCookies }
|
||||
@@ -38,8 +38,7 @@ export default function ViewVerification({ phone, otp, user }: IVerification) {
|
||||
toast.success(setCookies.message)
|
||||
if (setCookies.pertamaLogin == true)
|
||||
return router.replace('/welcome')
|
||||
console.log(setCookies.pertamaLogin)
|
||||
return router.replace('/home')
|
||||
return window.location.href = '/home';
|
||||
} else {
|
||||
toast.error(setCookies.message)
|
||||
}
|
||||
|
||||
@@ -1,20 +1,26 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { funDetectCookies, funGetUserByCookies } from "@/module/auth";
|
||||
|
||||
export default async function createLogUser({ act, desc, table, data }: { act: string, desc: string, table: string, data: string }) {
|
||||
try {
|
||||
|
||||
// diambil dari cookies
|
||||
const user = 'devAmalia'
|
||||
// cek cookies
|
||||
const cek = await funDetectCookies()
|
||||
if (cek) {
|
||||
const user = await funGetUserByCookies()
|
||||
|
||||
await prisma.userLog.create({
|
||||
data: {
|
||||
idUser: user,
|
||||
action: act,
|
||||
desc: desc,
|
||||
idContent: data,
|
||||
tbContent: table
|
||||
}
|
||||
})
|
||||
await prisma.userLog.create({
|
||||
data: {
|
||||
idUser: String(user.id),
|
||||
action: act,
|
||||
desc: desc,
|
||||
idContent: data,
|
||||
tbContent: table
|
||||
}
|
||||
})
|
||||
|
||||
return { success: true, message: "Success" }
|
||||
}
|
||||
|
||||
return { success: true, message: "Success" }
|
||||
|
||||
|
||||
@@ -16,13 +16,18 @@ import { useState } from "react";
|
||||
export default function ViewProfile() {
|
||||
const [openModal, setOpenModal] = useState(false);
|
||||
|
||||
async function onLogout() {
|
||||
async function onLogout(val: boolean) {
|
||||
try {
|
||||
await fetch('/api/auth/logout', {
|
||||
method: 'DELETE',
|
||||
});
|
||||
toast.success('Logout Success')
|
||||
window.location.href = '/';
|
||||
if (val) {
|
||||
await fetch('/api/auth/logout', {
|
||||
method: 'DELETE',
|
||||
});
|
||||
toast.success('Logout Success')
|
||||
window.location.href = '/';
|
||||
}
|
||||
|
||||
setOpenModal(false)
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
@@ -33,9 +38,9 @@ export default function ViewProfile() {
|
||||
<Group justify="space-between">
|
||||
<LayoutIconBack />
|
||||
|
||||
<ActionIcon onClick={() => { setOpenModal(true) }} variant="light" bg={WARNA.bgIcon} size="lg" radius="lg" aria-label="Info">
|
||||
<LuLogOut size={20} color='white' />
|
||||
</ActionIcon>
|
||||
<ActionIcon onClick={() => { setOpenModal(true) }} variant="light" bg={WARNA.bgIcon} size="lg" radius="lg" aria-label="Info">
|
||||
<LuLogOut size={20} color='white' />
|
||||
</ActionIcon>
|
||||
</Group>
|
||||
<Stack
|
||||
align="center"
|
||||
@@ -82,7 +87,7 @@ export default function ViewProfile() {
|
||||
|
||||
<LayoutModal opened={openModal} onClose={() => setOpenModal(false)}
|
||||
description="Apakah Anda yakin ingin Keluar?"
|
||||
onYes={() => onLogout()} />
|
||||
onYes={(val) => onLogout(val)} />
|
||||
</>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user