---
title: "How to Setup Auth with Fresh"
description: "Cookie-based authentication can be added to a Fresh app with server-side route handlers and std/http cookie helpers, without any client-side JavaScript."
url: "https://swe.quest/articles/how-to-setup-auth-with-fresh-f4d6b8731e3bd49f"
canonical: "https://deno.com/blog/setup-auth-with-fresh"
source: "Deno"
published: "2022-11-04"
difficulty: "medium"
kind: "How-to"
reading_minutes: 5
tags: ["identity-auth","typescript","frontend"]
---

# How to Setup Auth with Fresh

A post on [Deno](https://swe.quest/sources/deno.md), published 2022-11-04, 5 min read, How-to. Original: [https://deno.com/blog/setup-auth-with-fresh](https://deno.com/blog/setup-auth-with-fresh)

## Summary

Cookie-based authentication can be added to a Fresh app with server-side route handlers and std/http cookie helpers, without any client-side JavaScript. A plain HTML form POSTs to a /api/login handler that checks credentials, sets an auth cookie (SameSite=Lax, Secure, 2-minute maxAge) and returns a 303 redirect. A /logout route deletes the cookie, and protected pages either redirect or render conditionally based on the cookie. The example hardcodes credentials and a static cookie value; production use needs unique per-session values and a persistent store such as Redis or MongoDB.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Identity & auth](https://swe.quest/tags/identity-auth.md) (Security)
- [TypeScript](https://swe.quest/tags/typescript.md) (Languages)
- [Frontend](https://swe.quest/tags/frontend.md) (Web & mobile)

## Prerequisites

- HTTP cookies
- Deno basics
- JSX/Preact

## Keywords

Fresh, Deno, Preact, Deno std

## Related posts

- [Authenticating users in Astro using Neon Postgres and Lucia Auth](https://swe.quest/articles/authenticating-users-in-astro-using-neon-postgres-and-lucia-auth-235da5a83f745233.md): Neon · 2024-04-12 · Medium · 16 min read · Identity & auth, PostgreSQL, TypeScript, Frontend, Serverless
  Lucia Auth and a Neon serverless Postgres database provide cookie-based username/password authentication in an Astro app, with Vercel as the server-side rendering target.
- [Fresh 1.0](https://swe.quest/articles/fresh-1-0-407c9d4b84f51b2c.md): Deno · 2022-06-28 · Medium · 5 min read · Frontend, JavaScript, TypeScript, Web performance, CDN & edge
  Fresh is a full stack web framework for Deno that renders pages just-in-time on the server and ships zero JavaScript to the client by default, using the islands architecture so only explicitly opted-in components are hydrated in the browser.
- [Solving the MCP Authentication Headache with Vercel & Better Auth](https://swe.quest/articles/solving-the-mcp-authentication-headache-with-vercel-better-auth-749640ed1439e99c.md): Neon · 2025-05-23 · Medium · 5 min read · Identity & auth, AI agents, TypeScript
  Remote MCP servers can authenticate users with OAuth instead of plaintext bearer tokens or UUIDs stored in a client's mcp.json, using Better Auth's MCP plugin together with Vercel's @vercel/mcp-adapter in a Next.js app.
- [Supabase is now compatible with Next.js 14](https://swe.quest/articles/supabase-is-now-compatible-with-next-js-14-76a3d4ceac89740e.md): Supabase · 2023-11-01 · Medium · 4 min read · React, Frontend, Identity & auth, JavaScript
  Supabase works across every Next.js 14 rendering context, including App Router Client and Server Components, Route Handlers, Server Actions and Middleware, plus the Pages Router data-fetching functions.
- [Fresh 1.1 - automatic JSX, plugins, DevTools, and more](https://swe.quest/articles/fresh-1-1-automatic-jsx-plugins-devtools-and-more-546b7dc860fe9b32.md): Deno · 2022-09-08 · Medium · 7 min read · Frontend, TypeScript, JavaScript
  Fresh 1.1, Deno's zero-client-JS, no-build-step web framework, switches new projects to automatic JSX via the deno.json jsxImportSource setting.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
