Introduction to API Authentication
As we move toward decoupled architectures (like Vue or React apps talking to a PHP backend), traditional session-based login is being replaced by Token-Based Authentication.
How Token-Based Auth Works
Instead of the server remembering who you are via a cookie, the server gives you a "Token" after you log in. The client sends this token with every single request.
What is JWT (JSON Web Token)?
JWT is the most popular token format. It is self-contained, meaning it holds the user information inside the token itself, encoded in Base64. You can see what is inside any token using our Online JWT Decoder.
The 3 Parts of a JWT:
- Header: Defines the algorithm (e.g., HS256).
- Payload: Contains user ID, name, and expiration time.
- Signature: Ensures the token hasn’t been tampered with.
Key Security Tips for APIs
- Never store sensitive data in JWT: Tokens are encoded, not encrypted. Anyone can see the payload.
- Use short expiration times: If a token is stolen, it should become useless quickly.
- Store tokens securely: In web apps, use
HttpOnlycookies to prevent XSS attacks.