diff --git a/src/app/(application)/layout.tsx b/src/app/(application)/layout.tsx
new file mode 100644
index 0000000..75575d1
--- /dev/null
+++ b/src/app/(application)/layout.tsx
@@ -0,0 +1,17 @@
+import { pwd_key_config } from "@/module/_global"
+import { funDetectCookies } from "@/module/auth"
+import { unsealData } from "iron-session"
+import _ from "lodash"
+import { cookies } from "next/headers"
+import { redirect } from "next/navigation"
+
+export default async function Layout({ children }: { children: React.ReactNode }) {
+ const cookies = await funDetectCookies()
+ if (!cookies) return redirect('/')
+
+ return (
+ <>
+ {children}
+ >
+ );
+}
\ No newline at end of file
diff --git a/src/app/api/auth/get-user-by-cookies/route.ts b/src/app/api/auth/get-user-by-cookies/route.ts
index add8676..99798fa 100644
--- a/src/app/api/auth/get-user-by-cookies/route.ts
+++ b/src/app/api/auth/get-user-by-cookies/route.ts
@@ -3,7 +3,7 @@ import { unsealData } from "iron-session";
import { cookies } from "next/headers";
export async function GET() {
- const sessionCookie = cookies().get("sessionCookie");
+ const sessionCookie = cookies().get("sessionCookieSDM");
const userId = await unsealData(sessionCookie!.value, {
password: pwd_key_config,
});
diff --git a/src/app/api/auth/login/route.ts b/src/app/api/auth/login/route.ts
index 519f463..c034ddb 100644
--- a/src/app/api/auth/login/route.ts
+++ b/src/app/api/auth/login/route.ts
@@ -1,26 +1,31 @@
-
import { prisma } from "@/module/_global";
import { ILogin } from "@/types";
import { NextRequest } from "next/server";
export async function POST(req: NextRequest) {
- const { phone }: ILogin = await req.json();
- const user = await prisma.user.findUnique({
- where: { phone, isActive: true },
- select: { id: true, phone: true },
- });
-
- if (!user) {
- return Response.json({
- success: false,
- message: "Email atau Password salah",
+ try {
+ const { phone }: ILogin = await req.json();
+ const user = await prisma.user.findUnique({
+ where: { phone, isActive: true },
+ select: { id: true, phone: true },
});
- }
- return Response.json({
- success: true,
- message: "Login Berhasil",
- phone: user.phone,
- id: user.id,
- });
+ if (!user) {
+ return Response.json({
+ success: false,
+ message: "Nomor telepon tidak terdaftar",
+ });
+ }
+
+ return Response.json({
+ success: true,
+ message: "Sukses",
+ phone: user.phone,
+ id: user.id,
+ });
+
+ } catch (error) {
+ console.log(error);
+ return Response.json({ message: "Internal Server Error", success: false });
+ }
}
diff --git a/src/app/api/auth/logout/route.ts b/src/app/api/auth/logout/route.ts
index c17ef71..29f11a1 100644
--- a/src/app/api/auth/logout/route.ts
+++ b/src/app/api/auth/logout/route.ts
@@ -1,7 +1,7 @@
import { cookies } from "next/headers";
export async function DELETE() {
- cookies().delete('sessionCookie')
+ cookies().delete('sessionCookieSDM')
return Response.json({ success: true })
}
\ No newline at end of file
diff --git a/src/app/api/auth/set-cookies/route.ts b/src/app/api/auth/set-cookies/route.ts
deleted file mode 100644
index b201337..0000000
--- a/src/app/api/auth/set-cookies/route.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-import { pwd_key_config } from "@/module/_global";
-import { sealData } from "iron-session";
-import { cookies } from "next/headers";
-import { redirect } from "next/navigation";
-
-export async function POST(req: Request) {
- const { user } = await req.json();
- const encryptedUserData = await sealData(user, { password: pwd_key_config });
-
- cookies().set({
- name: "sessionCookie",
- value: encryptedUserData,
- });
-
- return Response.json({ success: true });
-}
diff --git a/src/app/page.tsx b/src/app/page.tsx
index fcd135f..4bec541 100644
--- a/src/app/page.tsx
+++ b/src/app/page.tsx
@@ -1,10 +1,16 @@
-import { ViewLogin } from "@/module/auth";
-import { Box, Image, rem, Stack, Text } from "@mantine/core";
+import { pwd_key_config } from "@/module/_global";
+import { funDetectCookies, ViewLogin } from "@/module/auth";
+import { unsealData } from "iron-session";
+import _ from "lodash";
+import { cookies } from "next/headers";
+import { redirect } from "next/navigation";
-export default function Home() {
+export default async function Home() {
+ const cookies = await funDetectCookies()
+ if (cookies) return redirect('/home')
return (
<>
-
+
>
);
}
diff --git a/src/module/auth/api/funDetectCookies.ts b/src/module/auth/api/funDetectCookies.ts
new file mode 100644
index 0000000..e8fd0d4
--- /dev/null
+++ b/src/module/auth/api/funDetectCookies.ts
@@ -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
+}
\ No newline at end of file
diff --git a/src/module/auth/api/funSetCookies.ts b/src/module/auth/api/funSetCookies.ts
new file mode 100644
index 0000000..965203f
--- /dev/null
+++ b/src/module/auth/api/funSetCookies.ts
@@ -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 };
+ }
+}
\ No newline at end of file
diff --git a/src/module/auth/index.ts b/src/module/auth/index.ts
index 3f6d0c0..9c3273d 100644
--- a/src/module/auth/index.ts
+++ b/src/module/auth/index.ts
@@ -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 }
\ No newline at end of file
+export { ViewWelcome }
+export { funSetCookies }
+export { funDetectCookies }
\ No newline at end of file
diff --git a/src/module/auth/login/view/view_login.tsx b/src/module/auth/login/view/view_login.tsx
index 8c5eb23..03a28ae 100644
--- a/src/module/auth/login/view/view_login.tsx
+++ b/src/module/auth/login/view/view_login.tsx
@@ -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={+62}
placeholder="XXX XXX XXX"
- onChange={(val) => { setPhone(val.target.value) }}
+ onChange={(val) => { setPhone('62' + val.target.value) }}
/>
{textInfo}
-
Ingat saya
}
- />
+ /> */}
-
+
- Didnt receive a code ? {""}
+ Tidak menerima kode verifikasi? {""}
{ onResend() }}
>
- Resend
+ Kirim Ulang