just.another.template

Back to Documentation

Authentication

This template includes a comprehensive self-rolled authentication system with multiple authentication methods and security features.

Overview

The authentication system is built from scratch without relying on third-party authentication providers like Auth.js or Clerk. This gives you complete control over your auth stack while maintaining high security standards.

The system is designed to be modular, allowing you to easily enable or disable specific authentication methods based on your needs.

Features

Passkeys

Modern, passwordless authentication using the WebAuthn standard for enhanced security.

JWT

JSON Web Token implementation for secure, stateless authentication.

Sessions

Server-side session management for secure user sessions across multiple devices.

OAuth

Social authentication with multiple providers (Google, GitHub, Discord, etc.).

Magic Links

Passwordless authentication through secure email links.

Password Reset

Secure flow for users to reset their passwords with email verification.

2FA

Two-factor authentication with TOTP for additional security.

Onboarding

Customizable user onboarding flow after registration.

Password Breach Detection

Check if passwords have been compromised in known data breaches.

Email Verification

Verify user email addresses to ensure they're valid and owned by the user.

Password Strength Checks

Enforce strong password policies with client and server validation.

Implementation

The authentication system is implemented in the src/server/auth directory and consists of several key components:

  • Session Management: Secure HTTP-only cookies for session storage with JWT or database-backed sessions.
  • Password Hashing: Uses Argon2id for secure password hashing.
  • Rate Limiting: Protection against brute force attacks.
  • CSRF Protection: Prevention of cross-site request forgery attacks.
  • Account Recovery: Secure flows for password reset and account recovery.

Usage

To authenticate users in your application, you can use the authentication hooks and components provided:

// Example: Using the authentication context
import { useAuth } from "@/hooks/use-auth";

function ProfilePage() {
  const { user, isLoading, signOut } = useAuth();
  
  if (isLoading) return <div>Loading...</div>;
  
  return (
    <div>
      <h1>Welcome, {user.name}!</h1>
      <button onClick={signOut}>Sign Out</button>
    </div>
  );
}

Configuration

Authentication settings can be configured in the .env file and in the auth configuration files. See the environment variables documentation for more details on available settings.