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 funDetectCookies from "./api/funDetectCookies";
|
||||||
|
import funGetUserByCookies from "./api/funGetUserByCookies";
|
||||||
import funSetCookies from "./api/funSetCookies";
|
import funSetCookies from "./api/funSetCookies";
|
||||||
import ViewLogin from "./login/view/view_login";
|
import ViewLogin from "./login/view/view_login";
|
||||||
import ViewVerification from "./varification/view/view_verification";
|
import ViewVerification from "./varification/view/view_verification";
|
||||||
@@ -9,3 +10,4 @@ export { ViewVerification }
|
|||||||
export { ViewWelcome }
|
export { ViewWelcome }
|
||||||
export { funSetCookies }
|
export { funSetCookies }
|
||||||
export { funDetectCookies }
|
export { funDetectCookies }
|
||||||
|
export { funGetUserByCookies }
|
||||||
@@ -38,8 +38,7 @@ export default function ViewVerification({ phone, otp, user }: IVerification) {
|
|||||||
toast.success(setCookies.message)
|
toast.success(setCookies.message)
|
||||||
if (setCookies.pertamaLogin == true)
|
if (setCookies.pertamaLogin == true)
|
||||||
return router.replace('/welcome')
|
return router.replace('/welcome')
|
||||||
console.log(setCookies.pertamaLogin)
|
return window.location.href = '/home';
|
||||||
return router.replace('/home')
|
|
||||||
} else {
|
} else {
|
||||||
toast.error(setCookies.message)
|
toast.error(setCookies.message)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,26 @@
|
|||||||
import { prisma } from "@/module/_global";
|
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 }) {
|
export default async function createLogUser({ act, desc, table, data }: { act: string, desc: string, table: string, data: string }) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// diambil dari cookies
|
// cek cookies
|
||||||
const user = 'devAmalia'
|
const cek = await funDetectCookies()
|
||||||
|
if (cek) {
|
||||||
|
const user = await funGetUserByCookies()
|
||||||
|
|
||||||
await prisma.userLog.create({
|
await prisma.userLog.create({
|
||||||
data: {
|
data: {
|
||||||
idUser: user,
|
idUser: String(user.id),
|
||||||
action: act,
|
action: act,
|
||||||
desc: desc,
|
desc: desc,
|
||||||
idContent: data,
|
idContent: data,
|
||||||
tbContent: table
|
tbContent: table
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return { success: true, message: "Success" }
|
||||||
|
}
|
||||||
|
|
||||||
return { success: true, message: "Success" }
|
return { success: true, message: "Success" }
|
||||||
|
|
||||||
|
|||||||
@@ -16,13 +16,18 @@ import { useState } from "react";
|
|||||||
export default function ViewProfile() {
|
export default function ViewProfile() {
|
||||||
const [openModal, setOpenModal] = useState(false);
|
const [openModal, setOpenModal] = useState(false);
|
||||||
|
|
||||||
async function onLogout() {
|
async function onLogout(val: boolean) {
|
||||||
try {
|
try {
|
||||||
await fetch('/api/auth/logout', {
|
if (val) {
|
||||||
method: 'DELETE',
|
await fetch('/api/auth/logout', {
|
||||||
});
|
method: 'DELETE',
|
||||||
toast.success('Logout Success')
|
});
|
||||||
window.location.href = '/';
|
toast.success('Logout Success')
|
||||||
|
window.location.href = '/';
|
||||||
|
}
|
||||||
|
|
||||||
|
setOpenModal(false)
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
@@ -33,9 +38,9 @@ export default function ViewProfile() {
|
|||||||
<Group justify="space-between">
|
<Group justify="space-between">
|
||||||
<LayoutIconBack />
|
<LayoutIconBack />
|
||||||
|
|
||||||
<ActionIcon onClick={() => { setOpenModal(true) }} variant="light" bg={WARNA.bgIcon} size="lg" radius="lg" aria-label="Info">
|
<ActionIcon onClick={() => { setOpenModal(true) }} variant="light" bg={WARNA.bgIcon} size="lg" radius="lg" aria-label="Info">
|
||||||
<LuLogOut size={20} color='white' />
|
<LuLogOut size={20} color='white' />
|
||||||
</ActionIcon>
|
</ActionIcon>
|
||||||
</Group>
|
</Group>
|
||||||
<Stack
|
<Stack
|
||||||
align="center"
|
align="center"
|
||||||
@@ -82,7 +87,7 @@ export default function ViewProfile() {
|
|||||||
|
|
||||||
<LayoutModal opened={openModal} onClose={() => setOpenModal(false)}
|
<LayoutModal opened={openModal} onClose={() => setOpenModal(false)}
|
||||||
description="Apakah Anda yakin ingin Keluar?"
|
description="Apakah Anda yakin ingin Keluar?"
|
||||||
onYes={() => onLogout()} />
|
onYes={(val) => onLogout(val)} />
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user