How Authentication Works: Passwords, Sessions, Tokens and Passkeys
Authentication is how an app checks who you are. Learn how passwords, hashing, sessions, tokens, 2FA, OAuth and passkeys work, explained simply with examples.
In this article
- What Is Authentication?
- The Three Types of Proof
- How Password Login Works Behind the Scenes
- Sessions vs Tokens
- Two-Factor Authentication (2FA)
- "Log in with Google": OAuth and Single Sign-On
- Passkeys: Logging In Without Passwords
- Common Authentication Attacks
- Common Mistakes Developers Make
- Authentication FAQ
- Key Takeaway
- Mini Cheat Sheet
Every time you log in to an app, a lot happens in the second between pressing "Sign in" and seeing your dashboard. The app has to check that you are really you, remember that you are logged in, and keep attackers out. That process is called authentication.
What Is Authentication?
Authentication is the process of verifying who someone is. It answers one question: "Are you really the person you claim to be?"
It is different from authorization, which decides what you are allowed to do after you log in. Read Authentication vs Authorization for the full comparison.
In simple words: authentication is showing your ID at the door.
The Three Types of Proof
Apps confirm your identity using one or more factors:
- Something you know: a password or PIN.
- Something you have: your phone, a security key, or an authenticator app.
- Something you are: your fingerprint or face.
Using two or more of these together is called multi-factor authentication (MFA), or two-factor authentication (2FA) when there are exactly two.
How Password Login Works Behind the Scenes
Step 1: Signing up
When you create an account, a well-built app never stores your actual password. Instead it runs the password through a hashing function, such as bcrypt or Argon2, and stores only the result:
Password: sunflower2026
Stored: $2y$12$Qm8vL2...k9aZ (a one-way hash)
A hash cannot be turned back into the password. Even if the database leaks, attackers don't get the real passwords directly.
Step 2: Logging in
- You type your email and password.
- The app finds your account and hashes the password you just entered.
- It compares the new hash with the stored hash.
- If they match, you are authenticated.
Step 3: Staying logged in
The app can't ask for your password on every click. So after a successful login it gives your browser proof that you already logged in. This is done with sessions or tokens.
Sessions vs Tokens
Session-based authentication
- The server creates a session record and sends your browser a session cookie with a random ID.
- Your browser sends that cookie automatically with each request.
- The server looks up the session to know who you are.
- Common in traditional websites, including Laravel and Django apps.
Token-based authentication
- After login, the server gives the app a token, often a JWT (JSON Web Token) or an API token.
- The app sends the token in a header with each request:
Authorization: Bearer <token>. - Common for mobile apps, single-page apps and APIs.
Both approaches answer the same question on every request: "Has this person already proven who they are?"
Two-Factor Authentication (2FA)
Passwords get stolen, reused and guessed. 2FA adds a second check, usually:
- A 6-digit code from an authenticator app like Google Authenticator or Authy.
- A code sent by SMS (better than nothing, but weaker than an app).
- A push notification to approve on your phone.
- A physical security key.
Even if someone steals your password, they still can't log in without the second factor.
"Log in with Google": OAuth and Single Sign-On
When you click Sign in with Google, the app never sees your Google password:
- The app sends you to Google.
- You log in to Google and approve sharing your basic profile.
- Google sends the app a code proving who you are.
- The app trusts Google's confirmation and logs you in.
This uses standards called OAuth 2.0 and OpenID Connect. When one login works across many apps, as at many companies, it is called Single Sign-On (SSO).
Passkeys: Logging In Without Passwords
Passkeys are a newer, more secure replacement for passwords:
- Your device creates a unique pair of cryptographic keys for each site.
- The private key never leaves your device. The site stores only the public key.
- You unlock it with your fingerprint, face or device PIN.
There is no password to steal, guess or reuse, and passkeys can't be phished on a fake website. Developers can add them using the WebAuthn standard. We covered a full setup in Laravel 13 Passkey Authentication.
Common Authentication Attacks
- Credential stuffing: using passwords leaked from other sites. Defence: 2FA and blocking known leaked passwords.
- Brute force: guessing passwords repeatedly. Defence: rate limiting and account lockouts.
- Phishing: fake login pages. Defence: passkeys and user education.
- Session hijacking: stealing a session cookie. Defence: HTTPS everywhere and secure, HttpOnly cookies.
Common Mistakes Developers Make
- Storing passwords in plain text or with fast hashes like MD5 or SHA-1.
- Building authentication from scratch instead of using a tested framework package.
- Telling attackers which part was wrong ("email not found" vs "wrong password").
- No rate limit on the login form.
- Long-lived tokens with no way to revoke them.
- Serving the login page over HTTP instead of HTTPS.
Authentication FAQ
What is the difference between authentication and authorization?
Authentication checks who you are. Authorization decides what you are allowed to access.
Is 2FA really necessary?
For email, banking, admin panels and anything important, yes. It blocks the majority of account takeovers that rely on stolen passwords.
Are passkeys safer than passwords?
Yes. They can't be reused, guessed or entered on a fake site, and nothing secret is stored on the server.
What is a JWT?
A JSON Web Token is a signed piece of data containing information like your user ID. The server can verify the signature to trust it without a database lookup.
Key Takeaway
Authentication proves who you are, usually with a password, a second factor or a passkey. The app stores only a hash of your password, then uses a session or token to remember you, so you don't have to log in on every page.
Mini Cheat Sheet
- Authentication: who are you?
- Hashing: one-way scrambling of passwords
- Session: server remembers you via a cookie
- Token: app sends proof with each request
- 2FA/MFA: more than one proof
- Passkey: passwordless, phishing-resistant login
Try it: Authentication only stays safe over HTTPS. Check any site's certificate with our free SSL checker, and read HTTP vs HTTPS to see why it matters.
Join the conversation
What do you think?
Write a comment
No comments yet. Be the first to share a thought.