Skip to content

This project contains a complete authentication system using Node.js, Express, and MongoDB. It includes features like sign up, sign in, sign out, password reset, and social authentication (Google).

Notifications You must be signed in to change notification settings

Dramilar/HK7_Lap07_BT6_NodeJS-Authentication-System

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧪 Test API – Auth Module (Local + Google OAuth)

Tài liệu hướng dẫn test các API của hệ thống Auth, bao gồm Local Auth (signup, signin, forgot/change password) và Google OAuth2.


1. Local Auth – /user

🔹 Đăng ký

POST http://localhost:3000/user/signup
Content-Type: application/json

{
  "username": "trantien",
  "email": "trantien3791@gmail.com",
  "password": "12345",
  "confirmPassword": "12345"
}

✅ Kết quả: User mới được lưu trong MongoDB.
Register Register exist User


🔹 Đăng nhập

POST http://localhost:3000/user/signin
Content-Type: application/json

{
  "email": "trantien3791@gmail.com",
  "password": "12345"
}

✅ Kết quả: "Login successful!" và cookie connect.sid được tạo.
Login Login failed


🔹 Truy cập Homepage (Profile)

GET http://localhost:3000/user/homepage
  • Nếu session hợp lệ → trả về trang homepage hoặc thông tin user.
  • Nếu chưa đăng nhập/hết hạn → 401 Unauthorized.
    Profile Profile failed

🔹 Đăng xuất

GET http://localhost:3000/user/signout

✅ Kết quả: "Logout successful!", session bị xóa khỏi DB, cookie hết hiệu lực.
Logout


🔹 Quên mật khẩu

POST http://localhost:3000/user/forgot-password
Content-Type: application/json

{
  "email": "admin@example.com"
}

✅ Kết quả: "Reset link sent to email" (nếu email tồn tại).
Forgot Forgot failed


🔹 Đổi mật khẩu

POST http://localhost:3000/user/change-password
Content-Type: application/json

{
  "oldPassword": "12345",
  "newPassword": "67890",
  "confirmPassword": "67890"
}

✅ Kết quả: "Password changed successfully!".
Change Change failed


2. Google OAuth2 – /auth

🔹 Đăng nhập với Google

GET http://localhost:3000/auth/google

✅ Redirect sang trang Google Login.
GoogleLogin


🔹 Callback từ Google

GET http://localhost:3000/auth/google/callback
  • Nếu thành công → redirect về CLIENT_URL (trong .env).
  • Nếu thất bại → redirect /login/failed.
    GoogleCallback

🔹 Lấy thông tin user sau khi login Google

GET http://localhost:3000/auth/login/success

✅ Kết quả: JSON thông tin user (name, email, avatar…).
Success


🔹 Login thất bại

GET http://localhost:3000/auth/login/failed

✅ Kết quả: { "error": "Login failed" }.
GoogleLogin failed


📂 Thư mục ảnh đề xuất

public/
  results/
    register.png
    login.png
    profile.png
    logout.png
    forgot.png
    change.png
    google-login.png
    google-callback.png
    login-success.png
    login-failed.png

⚠️ Lưu ý

  • Cần bật MongoDB trước khi test.
  • CALLBACK_URL phải trùng cấu hình trong Google Cloud Console.
  • Production: bật HTTPS + cookie.secure=true.

About

This project contains a complete authentication system using Node.js, Express, and MongoDB. It includes features like sign up, sign in, sign out, password reset, and social authentication (Google).

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 69.0%
  • EJS 31.0%