0Pricing
Vibe Coding · 课时

登录的工作原理

会话、令牌和身份。

登录的工作原理 是 CoddyKit 上的免费 Vibe Coding 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Vibe Coding 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Vibe Coding 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What Authentication Means

Authentication answers one question: who is this user? Before your app can show a personal dashboard or save someone's data, it has to confirm the visitor is who they claim to be.

When you build by prompting an AI assistant, you don't need to memorize every detail. But you do need a mental model so you can ask precise questions and judge whether the generated code is safe.

Identify vs Authorize

Two words get mixed up constantly. Authentication confirms identity. Authorization decides what that identity is allowed to do.

A logged-in user is authenticated. Whether they can delete another person's post is authorization. Keeping the two separate in your prompts leads to cleaner code and fewer security holes.

Explain the difference between authentication and authorization in my
web app, and show where each one lives in the codebase so I don't
mix them up.

The Login Flow, Step by Step

A typical login works like this: the user submits an email and password, the server checks the credentials, and if they match it issues a proof of identity. The browser stores that proof and sends it with every future request.

That proof is usually a session cookie or a token. Understanding this loop helps you debug "why am I logged out?" problems later.

Walk me through the exact sequence of what happens when a user submits
the login form in my app, from the request to the stored session.

Why Passwords Are Hashed

Your app should never store raw passwords. Instead it stores a hash: a one-way scrambled version produced by an algorithm like bcrypt or argon2.

When a user logs in, the server hashes the submitted password and compares it to the stored hash. If your database leaks, attackers only get hashes, not the actual passwords.

Confirm that my app hashes passwords with bcrypt or argon2 before
saving them, and never logs or stores the plain password anywhere.

Sessions vs Tokens

There are two common ways to remember a logged-in user. Session-based auth stores a session ID in a cookie and keeps the matching record on the server. Token-based auth (like JWT) hands the client a signed token that carries the user's identity.

Sessions are easy to revoke; tokens scale well across services. Many modern stacks use a hybrid, so ask which one your tool defaults to.

Should my project use server sessions or JWT tokens for keeping users
logged in? Compare both for a small app and recommend one.

Cookies Carry the Proof

However the proof is stored, cookies are how the browser sends it back automatically on each request. The key is making those cookies safe.

Look for three flags: HttpOnly stops JavaScript from reading the cookie, Secure forces HTTPS, and SameSite limits cross-site sending. Missing flags are a common vibe-coding mistake.

Check that my session cookie is set with HttpOnly, Secure, and
SameSite=Lax flags, and explain what each flag protects against.

Stateless vs Stateful

Stateful auth means the server remembers each active session in memory or a database. Stateless auth means the server trusts a signed token and stores nothing per-user.

Stateful gives instant logout but needs shared storage. Stateless is simpler to scale but harder to revoke early. Neither is universally "better" — it depends on your app's size and needs.

What Happens on Logout

Logging out should actually destroy the proof of identity, not just hide a button. For sessions, the server deletes the session record. For tokens, the client discards the token and the server may add it to a blocklist.

A common bug: the UI shows "logged out" but the cookie still works. Always test that protected pages truly reject you after logout.

After a user clicks logout, make sure the session is destroyed on the
server and the cookie is cleared, then verify protected routes reject
the old session.

Expiry and Refresh

Proof of identity should not last forever. Sessions and tokens expire after a set time so a stolen cookie has a limited window of use.

To keep users from logging in constantly, many systems use a short-lived access token plus a longer refresh token that quietly renews it. Ask your assistant to set sensible expiry times for your use case.

Where Auth Code Lives

In most apps, auth logic sits in a few predictable places: a login route, a signup route, middleware that checks each request, and a small client helper that knows the current user.

Knowing this map lets you prompt surgically. Instead of "fix my login," you can say "update the middleware that verifies the session cookie."

Show me a map of every file in my project that touches authentication,
and give each one a one-line description of its job.

Threats to Keep in Mind

A few attacks target login systems directly. Credential stuffing reuses leaked passwords, brute force guesses many passwords fast, and session hijacking steals a valid cookie.

You don't have to solve all of these by hand. But mentioning them in prompts — like asking for rate limiting on the login route — nudges your assistant toward safer defaults.

Add rate limiting to my login route so repeated failed attempts from
the same IP are slowed down, and explain the limits you chose.

Quick Check

Let's confirm the core idea before moving on.

Recap

You now have a working model of login: authentication proves who a user is, passwords are hashed not stored, and a session or token becomes the proof carried in cookies on each request.

You also saw the lifecycle — login, expiry, refresh, logout — and where auth code lives. With this map, your prompts to an AI assistant can be specific and safe.

常见问题解答

「登录的工作原理」课时是免费的吗?

是的 — 「登录的工作原理」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Vibe Coding 课程的其余内容,请升级到 CoddyKit PRO。 Vibe Coding 课程共包含 4 节课。

「登录的工作原理」这节课中我会学到什么?

会话、令牌和身份。 你通过在浏览器中直接运行的动手代码来练习 Vibe Coding,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Vibe Coding 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Vibe Coding 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「登录的工作原理」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Vibe Coding 课中编写并运行代码吗?

能。每节 Vibe Coding 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 登录的工作原理
  2. 选择身份验证提供商
  3. 连接注册和登录
  4. 保护页面和数据
← 返回 Vibe Coding