upd: login

Deskripsi:
- pembaruan login
- batasan login

No Issues
This commit is contained in:
amel
2024-07-30 14:50:51 +08:00
parent cffd993bc0
commit 1607152d77
11 changed files with 167 additions and 82 deletions

View File

@@ -0,0 +1,15 @@
'use server'
import { pwd_key_config } from "@/module/_global"
import { unsealData } from "iron-session"
import _ from "lodash"
import { cookies } from "next/headers"
export default async function funDetectCookies() {
const cookiesnya = cookies()
const c = cookiesnya.get("sessionCookieSDM")
if (!c || _.isUndefined(c) || !c.value || _.isEmpty(c.value)) return false
const dataCookies = await unsealData(c!.value, { password: pwd_key_config as string })
if (_.isEmpty(_.toString(dataCookies))) return false
return true
}

View File

@@ -0,0 +1,44 @@
'use server'
import { sealData } from "iron-session";
import { cookies } from "next/headers";
import { prisma, pwd_key_config } from "@/module/_global";
export default async function funSetCookies({ user }: { user: string }) {
try {
const encryptedUserData = await sealData(user, { password: pwd_key_config });
// data user
const dataUser = await prisma.user.findUnique({
where: {
id: user
},
select:{
isFirstLogin: true
}
})
if (dataUser?.isFirstLogin) {
await prisma.user.update({
where: {
id: user
},
data: {
isFirstLogin: false
}
})
}
// set cookies
cookies().set({
name: "sessionCookieSDM",
value: encryptedUserData,
});
return { success: true, message: "Login berhasil!", pertamaLogin: dataUser?.isFirstLogin };
} catch (error) {
console.error(error);
return { message: "Internal Server Error", success: false };
}
}

View File

@@ -1,7 +1,11 @@
import funDetectCookies from "./api/funDetectCookies";
import funSetCookies from "./api/funSetCookies";
import ViewLogin from "./login/view/view_login";
import ViewVerification from "./varification/view/view_verification";
import { ViewWelcome } from "./welcome/view_welcome";
export { ViewLogin }
export { ViewVerification }
export { ViewWelcome }
export { ViewWelcome }
export { funSetCookies }
export { funDetectCookies }

View File

@@ -32,7 +32,11 @@ function ViewLogin() {
async function onLogin() {
if (isPhone == "")
return toast.error('Please fill in completely')
return toast.error('Silakan diisi dengan lengkap')
if (isPhone.toString().length <= 11)
return toast.error('Nomor telepon tidak valid')
const cek = await fetch('/api/auth/login', {
method: 'POST',
headers: {
@@ -40,29 +44,30 @@ function ViewLogin() {
},
body: JSON.stringify({ phone: isPhone })
})
const json = await cek.json()
console.log(json)
const cekLogin = await cek.json()
const code = Math.floor(Math.random() * 1000) + 1000
if (cekLogin.success) {
const code = Math.floor(Math.random() * 1000) + 1000
setLoading(true)
setLoading(true)
const res = await fetch(`https://wa.wibudev.com/code?nom=${json.phone}&text=${code}`).then(
async (res) => {
if (res.status == 200) {
setValPhone(json.phone)
setOTP(code)
setUser(json.id)
setVerif(true)
setLoading(false)
toast.success('OTP sent successfully')
} else {
toast.error('OTP not sent')
setLoading(false)
const res = await fetch(`https://wa.wibudev.com/code?nom=${cekLogin.phone}&text=${code}`).then(
async (res) => {
if (res.status == 200) {
setValPhone(cekLogin.phone)
setOTP(code)
setUser(cekLogin.id)
setVerif(true)
setLoading(false)
toast.success('Kode verifikasi telah dikirim')
} else {
toast.error('Internal Server Error')
setLoading(false)
}
}
console.log("code", code)
}
)
)
} else {
return toast.error(cekLogin.message)
}
}
@@ -87,19 +92,19 @@ function ViewLogin() {
radius={30}
leftSection={<Text>+62</Text>}
placeholder="XXX XXX XXX"
onChange={(val) => { setPhone(val.target.value) }}
onChange={(val) => { setPhone('62' + val.target.value) }}
/>
<Text fz={10} mt={10} c={WARNA.biruTua}>
{textInfo}
</Text>
<Checkbox
{/* <Checkbox
mt={20}
label={
<Text fz={10} c={WARNA.biruTua}>
Ingat saya
</Text>
}
/>
/> */}
<Box mt={20}>
<Button
c={"white"}

View File

@@ -5,6 +5,7 @@ import { Anchor, Box, Button, Group, PinInput, Stack, Text, Title } from "@manti
import { useRouter } from "next/navigation";
import React, { useState } from "react";
import toast from "react-hot-toast";
import funSetCookies from "../../api/funSetCookies";
export default function ViewVerification({ phone, otp, user }: IVerification) {
const router = useRouter()
@@ -19,10 +20,10 @@ export default function ViewVerification({ phone, otp, user }: IVerification) {
.then(
async (res) => {
if (res.status == 200) {
toast.success('Verification code has been sent')
toast.success('Kode verifikasi telah dikirim')
setOTP(code)
} else {
toast.error('Error')
toast.error('Internal Server Error')
}
}
);
@@ -31,19 +32,22 @@ export default function ViewVerification({ phone, otp, user }: IVerification) {
async function getVerification() {
setLoading(true)
if (isOTP == inputOTP) {
const setCookies = await funSetCookies({ user: user })
if (setCookies.success) {
toast.success(setCookies.message)
if (setCookies.pertamaLogin == true)
return router.replace('/welcome')
console.log(setCookies.pertamaLogin)
return router.replace('/home')
} else {
toast.error(setCookies.message)
}
setLoading(false)
const res = await fetch('/api/auth/set-cookies', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ user: user })
})
router.push('/welcome')
toast.success("Verification code is correct")
setLoading(false)
} else {
toast.error("Verification code is incorrect")
toast.error("Kode verifikasi salah")
setLoading(false)
}
}
@@ -62,7 +66,7 @@ export default function ViewVerification({ phone, otp, user }: IVerification) {
Masukkan kode yang kami kirimkan melalui WhatsApp
</Text>
<Text fz={12} c={WARNA.biruTua} fw={"bold"}>
{phone}
{'+' + phone}
</Text>
<Box pt={30}>
<PinInput
@@ -89,19 +93,20 @@ export default function ViewVerification({ phone, otp, user }: IVerification) {
size="md"
radius={30}
fullWidth
loading={isLoading}
onClick={() => { getVerification() }}
>
Lanjut
</Button>
</Box>
<Group justify="center" >
<Group justify="center" mt={5}>
<Text fz={12} c={WARNA.biruTua}>
Didnt receive a code ? {""}
Tidak menerima kode verifikasi? {""}
<Anchor c={WARNA.biruTua}
fz={12}
onClick={() => { onResend() }}
>
Resend
Kirim Ulang
</Anchor>
</Text>
</Group>