From 6ea6976c4160afd0e13906ce3a5e054a3e9c287b Mon Sep 17 00:00:00 2001 From: ducanh Date: Sat, 4 Apr 2026 23:54:52 +0700 Subject: [PATCH] cookie --- prettier.config.js | 3 - src/app/testing/page.tsx | 11 ++ src/components/auth/SignInForm.tsx | 187 +++++++++++++++++------------ src/components/auth/SignUpForm.tsx | 148 ++++++++++++++--------- 4 files changed, 213 insertions(+), 136 deletions(-) delete mode 100644 prettier.config.js create mode 100644 src/app/testing/page.tsx diff --git a/prettier.config.js b/prettier.config.js deleted file mode 100644 index 84faa16..0000000 --- a/prettier.config.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - plugins: ['prettier-plugin-tailwindcss'], -} \ No newline at end of file diff --git a/src/app/testing/page.tsx b/src/app/testing/page.tsx new file mode 100644 index 0000000..95daeaf --- /dev/null +++ b/src/app/testing/page.tsx @@ -0,0 +1,11 @@ +import { API } from "../../../api"; + +export default async function GetUser() { + let data = await fetch(API.User.CURRENT, { + credentials: "include", + }); + + let result = await data.json(); + console.log("Current User from GetUser component:", result); + return
GetUser
; +} diff --git a/src/components/auth/SignInForm.tsx b/src/components/auth/SignInForm.tsx index d6b8819..d1a88e9 100644 --- a/src/components/auth/SignInForm.tsx +++ b/src/components/auth/SignInForm.tsx @@ -7,7 +7,7 @@ import { ChevronLeftIcon, EyeCloseIcon, EyeIcon } from "@/icons"; import { apiGetCurrentUser, apiSignIn } from "@/service/auth"; import Link from "next/link"; import React, { useState } from "react"; -import { toast } from 'sonner'; +import { toast } from "sonner"; import { API } from "../../../api"; import { setUserData } from "@/store/features/userSlice"; import { useDispatch } from "react-redux"; @@ -30,7 +30,7 @@ export default function SignInForm() { const handleChange = (e: React.ChangeEvent) => { setFormData({ ...formData, [e.target.name]: e.target.value }); - setErrorMsg(""); + setErrorMsg(""); }; const isValidEmail = (email: string) => { @@ -45,7 +45,7 @@ export default function SignInForm() { const handleSignInClick = async (e: React.FormEvent) => { e.preventDefault(); - if (loading || isFormEmpty) return; + if (loading || isFormEmpty) return; setErrorMsg(""); @@ -55,34 +55,36 @@ export default function SignInForm() { } if (!isValidPassword(formData.password)) { - setErrorMsg("Mật khẩu tối thiểu 8 ký tự, 1 in hoa, 1 số và 1 ký tự đặc biệt."); + setErrorMsg( + "Mật khẩu tối thiểu 8 ký tự, 1 in hoa, 1 số và 1 ký tự đặc biệt.", + ); return; } try { - setLoading(true); + setLoading(true); + const res = await apiSignIn(formData); console.log("API Sign In Response:", res); if (res.status === true) { - toast.success('Đăng nhập thành công!'); - const data = await apiGetCurrentUser(); + toast.success("Đăng nhập thành công!"); + const data = await apiGetCurrentUser(); console.log("Current User Data:", data); if (data?.data) { dispatch(setUserData(data.data)); - // router.push("/profile"); + // router.push("/profile"); } - }else{ - toast.error('Email hoặc mật khẩu không đúng.'); + } else { + toast.error("Email hoặc mật khẩu không đúng."); } - } catch (error) { setErrorMsg("Lỗi khi đăng nhập. Vui lòng thử lại."); - toast.error('Đăng nhập thất bại. Vui lòng kiểm tra lại thông tin.'); + toast.error("Đăng nhập thất bại. Vui lòng kiểm tra lại thông tin."); } finally { setLoading(false); } - } + }; return (
@@ -107,7 +109,13 @@ export default function SignInForm() {
-
-
-
-
- - -
- -
- -
0 && !isValidPassword(formData.password) ? 'border border-red-500 ring-1 ring-red-500 rounded-lg' : ''}`}> + +
+
+ - setShowPassword(!showPassword)} className="absolute z-30 -translate-y-1/2 cursor-pointer right-4 top-1/2"> - {showPassword ? : } -
-
- {/* Hiển thị thông báo lỗi nếu có */} - {errorMsg && ( -

{errorMsg}

- )} - -
-
- - - Keep me logged in - -
- - Forgot password? - -
- -
- +
+
+ + {/* Hiển thị thông báo lỗi nếu có */} + {errorMsg && ( +

{errorMsg}

+ )} + +
+
+ + + Keep me logged in + +
+ + Forgot password? + +
+ +
+ +
-
-
+

@@ -242,4 +279,4 @@ export default function SignInForm() {

); -} \ No newline at end of file +} diff --git a/src/components/auth/SignUpForm.tsx b/src/components/auth/SignUpForm.tsx index 515c070..483dc0a 100644 --- a/src/components/auth/SignUpForm.tsx +++ b/src/components/auth/SignUpForm.tsx @@ -26,10 +26,10 @@ export default function SignUpForm() { const handleChange = (e: React.ChangeEvent) => { setFormData({ ...formData, [e.target.name]: e.target.value }); - setErrorMsg(""); + setErrorMsg(""); }; - const isValidEmail = (email:string) => { + const isValidEmail = (email: string) => { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); }; @@ -43,7 +43,12 @@ export default function SignUpForm() { e.preventDefault(); setErrorMsg(""); - if (!formData.fname || !formData.lname || !formData.email || !formData.password) { + if ( + !formData.fname || + !formData.lname || + !formData.email || + !formData.password + ) { setErrorMsg("Vui lòng điền đầy đủ thông tin."); return; } @@ -51,8 +56,10 @@ export default function SignUpForm() { setErrorMsg("Email không đúng định dạng."); return; } - if (!isValidPassword(formData.password)) { - setErrorMsg("Mật khẩu chưa đủ điều kiện. Vui lòng nhập tối thiểu 8 ký tự, 1 in hoa, 1 số và 1 ký tự đặc biệt."); + if (!isValidPassword(formData.password)) { + setErrorMsg( + "Mật khẩu chưa đủ điều kiện. Vui lòng nhập tối thiểu 8 ký tự, 1 in hoa, 1 số và 1 ký tự đặc biệt.", + ); return; } @@ -62,7 +69,7 @@ export default function SignUpForm() { setStep(2); } catch (error) { setErrorMsg("Lỗi khi tạo OTP. Vui lòng thử lại."); - toast.error('Tạo OTP thất bại. Vui lòng kiểm tra lại thông tin.'); + toast.error("Tạo OTP thất bại. Vui lòng kiểm tra lại thông tin."); } finally { setLoading(false); } @@ -79,10 +86,10 @@ export default function SignUpForm() { try { setLoading(true); - + const verifyRes = await apiVerifyOTP(formData.email, otp); - const tokenId = verifyRes?.data?.token_id; + const tokenId = verifyRes?.data?.token_id; if (!tokenId) { throw new Error("OTP không hợp lệ hoặc không có token_id"); } @@ -93,6 +100,7 @@ export default function SignUpForm() { password: formData.password, token_id: tokenId, }; + const signupRes = await apiSignUp(signupPayload); console.log("API Sign Up Response:", signupRes); @@ -100,10 +108,12 @@ export default function SignUpForm() { console.log("Đăng ký thành công!", signupRes); alert("Đăng ký thành công! Đang chuyển hướng..."); - window.location.href = '/signin'; - + window.location.href = "/signin"; } catch (error) { - const errorMessage = error instanceof Error ? error.message : "Xác thực OTP hoặc đăng ký thất bại."; + const errorMessage = + error instanceof Error + ? error.message + : "Xác thực OTP hoặc đăng ký thất bại."; setErrorMsg(errorMessage || "Xác thực OTP hoặc đăng ký thất bại."); } finally { setLoading(false); @@ -121,7 +131,7 @@ export default function SignUpForm() { Back to dashboard - +
@@ -129,8 +139,8 @@ export default function SignUpForm() { {step === 1 ? "Sign Up" : "Verify OTP"}

- {step === 1 - ? "Enter your email and password to sign up!" + {step === 1 + ? "Enter your email and password to sign up!" : `We sent an OTP to ${formData.email}. Please check your email and enter it below.`}

@@ -144,31 +154,37 @@ export default function SignUpForm() { {step === 1 && ( <>
- {/* @@ -320,7 +353,7 @@ export default function SignUpForm() { placeholder="Enter the 6-digit code" />
- +
); -} \ No newline at end of file +}