0Pricing
Node.js Backend Development Bootcamp · บทเรียน

การลงทะเบียนและเข้าสู่ระบบผู้ใช้

สร้างความสามารถในการลงทะเบียนและเข้าสู่ระบบผู้ใช้ รวมถึงการแฮชรหัสผ่านและจัดเก็บข้อมูลรับรองอย่างปลอดภัย

การลงทะเบียนและเข้าสู่ระบบผู้ใช้ เป็นบทเรียน Node.js Backend Development Bootcamp ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 6 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Node.js Backend Development Bootcamp และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 6 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Welcome to User Authentication

User authentication is how we verify who a user is. It's a critical part of almost any application that handles personal data or restricted features.

  • Why it matters: Protects user accounts and sensitive information.
  • What we'll cover: Building registration and login flows from scratch.

The User Registration Flow

Registering a new user involves several steps to create a new account:

  1. User provides credentials (e.g., username, password, email).
  2. Input data is validated (e.g., strong password, unique email).
  3. The password is hashed for security.
  4. New user data (including the hashed password) is saved to the database.

Why Hash Passwords?

Storing passwords in plain text is a huge security risk! If your database is breached, all user passwords would be exposed.

Hashing transforms a password into a fixed-size, unreadable string. It's a one-way process, meaning you can't easily get the original password back from the hash.

We use libraries like bcrypt in Node.js for robust password hashing, which also adds a 'salt' to prevent common attacks.

Hashing Passwords with bcrypt

bcrypt is a popular library for securely hashing passwords. It's computationally intensive, making brute-force attacks harder.

Try running this example to see a password hashed:

const bcrypt = require('bcrypt');

const password = "mySecretP@ssword";
const saltRounds = 10; // Cost factor for hashing (higher is slower/more secure)

async function hashPassword() {
  try {
    const hashedPassword = await bcrypt.hash(password, saltRounds);
    console.log("Original: " + password);
    console.log("Hashed: " + hashedPassword);
  } catch (error) {
    console.error("Error hashing:" + error.message);
  }
}

hashPassword();

Storing Hashed Credentials

After hashing, only the hashed password should be stored in your database, along with other user details like their email or username.

  • NEVER store plain-text passwords.
  • The hash is unique for each password, even if the original passwords are the same (thanks to salting).
  • This hash is what you'll use for comparison during login.

The User Login Flow

When a user tries to log in, your application follows these steps:

  1. User provides their username/email and password.
  2. Application retrieves the user's record (including their stored hashed password) from the database based on the username/email.
  3. The provided password is hashed and compared against the stored hash.
  4. If they match, the user is authenticated, and a session or token is created.

Verifying Passwords with bcrypt

To check if a user's provided password matches the stored hash, we use bcrypt.compare(). It performs the hashing and comparison securely.

Run this code to see password comparison in action:

const bcrypt = require('bcrypt');

// This hash would typically come from your database
const storedHash = "$2b$10$w090/qB2k6n0Y7o8p9q.u.0Z1X2Y3Z4A5B6C7D8E9F0G1H2I3J4K5L6M7N8O9P0Q1R"; 

const passwordAttempt = "mySecretP@ssword";
const wrongAttempt = "incorrectPassword";

async function comparePasswords() {
  try {
    const isMatch = await bcrypt.compare(passwordAttempt, storedHash);
    console.log(`'${passwordAttempt}' matches: ${isMatch}`);

    const isWrongMatch = await bcrypt.compare(wrongAttempt, storedHash);
    console.log(`'${wrongAttempt}' matches: ${isWrongMatch}`);
  } catch (error) {
    console.error("Error comparing:" + error.message);
  }
}

comparePasswords();

Secure Credential Storage Practices

Beyond just hashing passwords, other credentials need protection:

  • API Keys & Database URLs: Store these in environment variables (e.g., .env files), not directly in your code.
  • Sensitive User Data: Encrypt any highly sensitive data at rest in your database.
  • Regular Updates: Keep your hashing libraries and dependencies up-to-date.

Handling Authentication Errors

When registration or login fails, provide helpful but generic error messages to the user. This prevents revealing too much information to potential attackers.

  • Instead of 'User not found', say 'Invalid credentials'.
  • Instead of 'Password incorrect', also say 'Invalid credentials'.
  • Log detailed errors on the server side for debugging, but don't expose them to the client.

Quick Check: Password Hashing

Test your understanding of why password hashing is essential for security.

Recap: Registration & Login

In this lesson, you learned the fundamental steps for user registration and login:

  • We covered the importance of password hashing using bcrypt to protect sensitive user data.
  • You saw how to implement both the hashing for registration and the comparison for login.
  • We also touched on best practices for secure credential storage and handling authentication errors gracefully.

Next, we'll dive into implementing stateless authentication using JSON Web Tokens (JWTs).

คำถามที่พบบ่อย

บทเรียน “การลงทะเบียนและเข้าสู่ระบบผู้ใช้” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การลงทะเบียนและเข้าสู่ระบบผู้ใช้” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Node.js Backend Development Bootcamp ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 6 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การลงทะเบียนและเข้าสู่ระบบผู้ใช้”

สร้างความสามารถในการลงทะเบียนและเข้าสู่ระบบผู้ใช้ รวมถึงการแฮชรหัสผ่านและจัดเก็บข้อมูลรับรองอย่างปลอดภัย คุณปฏิบัติ Node.js Backend Development Bootcamp ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Node.js Backend Development Bootcamp หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Node.js Backend Development Bootcamp บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 6 บทเรียน

บทเรียน “การลงทะเบียนและเข้าสู่ระบบผู้ใช้” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Node.js Backend Development Bootcamp นี้ได้ไหม

ได้ บทเรียน Node.js Backend Development Bootcamp ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การลงทะเบียนและเข้าสู่ระบบผู้ใช้
  2. การสร้างและตรวจสอบโทเค็น JWT
  3. JWT สำหรับการยืนยันตัวตนแบบไร้สถานะ
  4. การผสานรวมโฟลว์รหัสผ่าน OAuth2
  5. การควบคุมการเข้าถึงตามบทบาท
  6. การควบคุมการเข้าถึงตามบทบาท (RBAC)
← กลับไปที่ Node.js Backend Development Bootcamp