Upstash Documentation

Authentication

1 min read

Protect your realtime endpoints with custom auth logic.

Basic Middleware#

api/realtime/route.ts

Middleware API#

The middleware function receives:

requestRequest#

The incoming HTTP Request object

channelsstring[]#

The channels a user is attempting to connect to

Default: "['default']"

return#
  • Return undefined or nothing to allow the connection
  • Return a Response object to block the connection with a custom error

Authentication Patterns#

User-Based Auth

Verify users can access specific channels:

api/realtime/route.ts
Session-Based Auth

Verify user sessions before allowing connections:

api/realtime/route.ts
Role-Based Access

Control access based on user roles:

api/realtime/route.ts