VercelLogotypeVercelLogotype
LoginSign Up
Back to Templates

Next.js Redis Session Store

This Next.js template demonstrates how to use Redis as a session store for app auth using multiple providers like GitHub and Discord.

DeployView Demo
redis-template-thumbnail

šŸš€ Next.js Redis Session Store Example

This Next.js project demonstrates how to use Redis as a session store for authentication. The application supports multiple authentication providers, offering a scalable and performant solution for your web applications.

šŸ“‹ Table of Contents

  • šŸš€ Deploy with Redis Cloud on Vercel
  • ✨ Features
  • 🌱 Getting Started
  • šŸ› ļø Configuration
  • šŸ’¾ Session Management with Redis
  • šŸ—‚ļø Project Structure
  • šŸ“œ Available Scripts
  • šŸ“š Learn More
  • šŸ¤ Contributing
  • šŸ“„ License

šŸš€ Deploy with Redis Cloud on Vercel

Follow these steps to deploy your application with Redis Cloud on Vercel:

  1. Click the "Deploy" button below to start the deployment process:
  1. In the Vercel UI, create a new repository for your project.

  1. Add Redis as a store in the Vercel UI. This will automatically set your REDIS_URL environment variable.

  1. Wait for the deployment to complete.

  1. Once deployed, use the URL of your deployed app to set up the authentication providers. See the Configuration section below for detailed instructions for setting these up. By default, the template is configured to use GitHub and Discord. Only one provider is required.

✨ Features

  • šŸ—„ļø Redis Session Store: Utilizes Redis for storing session data
  • šŸ” NextAuth.js Integration: Implements authentication using NextAuth.js with a custom Redis adapter
  • 🌐 Multiple Authentication Providers: Supports GitHub and Discord authentication
  • āš™ļø Dynamic Configuration: Easily enable or disable authentication providers via environment variables

🌱 Getting Started

  1. Install dependencies:

    npm install
    # or
    yarn install
    # or
    pnpm install
  2. Run the development server:

    npm run dev
    # or
    yarn dev
    # or
    pnpm dev
  3. Open http://localhost:3000 in your browser to see the application.

šŸ› ļø Configuration

Setting Up Redis

Ensure you have a Redis server running and configure the connection in your .env file:

REDIS_URL=redis://localhost:6379

Note: If you deployed using Vercel with Redis Cloud, this will be automatically set for you.

Enabling/Disabling Authentication Providers

Configure authentication providers by setting or omitting their respective environment variables in your .env file:

NEXTAUTH_SECRET=your_nextauth_secret
# GitHub Authentication
GITHUB_CLIENT_ID=your_github_client_id
GITHUB_CLIENT_SECRET=your_github_client_secret
# Discord Authentication
DISCORD_CLIENT_ID=your_discord_client_id
DISCORD_CLIENT_SECRET=your_discord_client_secret
  • To enable a provider: Set both the CLIENT_ID and CLIENT_SECRET
  • To disable a provider: Remove or comment out the CLIENT_ID and CLIENT_SECRET

Provider-Specific Configuration

GitHub
  1. Go to GitHub Developer Settings
  2. Click on "New OAuth App"
  3. Fill in the application details:
    • Application name: Your app name
    • Homepage URL: https://your-vercel-app-url.vercel.app
    • Authorization callback URL: https://your-vercel-app-url.vercel.app/api/auth/callback/github (or your local URL if you're running it locally)
  4. Click "Register application"
  5. Copy the Client ID and generate a new Client Secret
  6. Add these to your Vercel project's environment variables as GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET

Example:

GITHUB_CLIENT_ID=1234567890abcdef1234
GITHUB_CLIENT_SECRET=1234567890abcdef1234567890abcdef12345678
Discord
  1. Go to the Discord Developer Portal
  2. Click on "New Application"
  3. Give your application a name and create it
  4. Go to the "OAuth2" tab in the left sidebar
  5. Add a redirect URL: https://your-vercel-app-url.vercel.app/api/auth/callback/discord (or your local URL if you're running it locally)
  6. Copy the Client ID and Client Secret
  7. Add these to your Vercel project's environment variables as DISCORD_CLIENT_ID and DISCORD_CLIENT_SECRET

Example:

DISCORD_CLIENT_ID=123456789012345678
DISCORD_CLIENT_SECRET=abcdefghijklmnopqrstuvwxyz123456

Adding Additional Providers

To add more authentication providers:

  1. Install the provider package (if required)
  2. Update the NextAuth configuration in app/api/auth/[...nextauth]/route.js
  3. Add the necessary environment variables

Example for adding Google provider:

import GoogleProvider from "next-auth/providers/google";
// In the providers array:
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),

For more information on adding and configuring providers, refer to the NextAuth.js documentation on providers.

NextAuth.js Secret

Generate a secure NEXTAUTH_SECRET:

openssl rand -base64 32

Add this to your Vercel project's environment variables as NEXTAUTH_SECRET.

šŸ’¾ Session Management with Redis

This application leverages Redis for efficient session data storage, offering:

  • ⚔ High Performance: Fast read/write operations with in-memory data store
  • šŸ“ˆ Scalability: Share session data across multiple application instances
  • šŸ’½ Optional Persistence: Configure Redis to persist data to disk for recovery

šŸ—‚ļø Project Structure

/
ā”œā”€ā”€ app/ # Next.js pages and API routes
ā”œā”€ā”€ components/ # Reusable React components
ā”œā”€ā”€ lib/ # Utility functions and custom adapters
ā”œā”€ā”€ hooks/ # Custom React hooks
└── styles/ # Global and brand-specific styles

šŸ“œ Available Scripts

  • npm run dev: Run the app in development mode
  • npm run build: Build the app for production
  • npm run start: Start the production build
  • npm run lint: Run linting on the codebase

šŸ“š Learn More

Explore these resources to deepen your understanding:

  • Next.js Documentation
  • NextAuth.js Documentation
  • Redis Documentation
  • Redis Clients for Node.js
  • Environment Variables in Next.js

šŸ¤ Contributing

We welcome contributions! If you have ideas, suggestions, or encounter issues, please open an issue or submit a pull request.

šŸ“„ License

This project is open source and available under the MIT License.

GitHub Reporedis-developer/session-store-nextjs
Use Cases
Starter
Stack
Next.jsTailwind
Database
Redis
Auth
NextAuth.js

Related Templates

Get Started

  • Templates
  • Supported frameworks
  • Marketplace
  • Domains

Build

  • Next.js on Vercel
  • Turborepo
  • v0

Scale

  • Content delivery network
  • Fluid compute
  • CI/CD
  • Observability
  • AI GatewayNew
  • Vercel AgentNew

Secure

  • Platform security
  • Web Application Firewall
  • Bot management
  • BotID
  • SandboxNew

Resources

  • Pricing
  • Customers
  • Enterprise
  • Articles
  • Startups
  • Solution partners

Learn

  • Docs
  • Blog
  • Changelog
  • Knowledge Base
  • Academy
  • Community

Frameworks

  • Next.js
  • Nuxt
  • Svelte
  • Nitro
  • Turbo

SDKs

  • AI SDK
  • Workflow SDKNew
  • Flags SDK
  • Chat SDK
  • Streamdown AINew

Use Cases

  • Composable commerce
  • Multi-tenant platforms
  • Web apps
  • Marketing sites
  • Platform engineers
  • Design engineers

Company

  • About
  • Careers
  • Help
  • Press
  • Legal
  • Privacy Policy

Community

  • Open source program
  • Events
  • Shipped on Vercel
  • GitHub
  • LinkedIn
  • X
  • YouTube

Loading status…

Select a display theme:
    • AI Cloud
      • v0

        Build applications with AI

      • AI SDK

        The AI Toolkit for TypeScript

      • AI Gateway

        One endpoint, all your models

      • Vercel Agent

        An agent that knows your stack

      • Sandbox

        AI workflows in live environments

    • Core Platform
      • CI/CD

        Helping teams ship 6Ɨ faster

      • Content Delivery

        Fast, scalable, and reliable

      • Fluid Compute

        Servers, in serverless form

      • Observability

        Trace every step

    • Security
      • Bot Management

        Scalable bot protection

      • BotID

        Invisible CAPTCHA

      • Platform Security

        DDoS Protection, Firewall

      • Web Application Firewall

        Granular, custom protection

    • Company
      • Customers

        Trusted by the best teams

      • Blog

        The latest posts and changes

      • Changelog

        See what shipped

      • Press

        Read the latest news

      • Events

        Join us at an event

    • Learn
      • Docs

        Vercel documentation

      • Academy

        Linear courses to level up

      • Knowledge Base

        Find help quickly

      • Community

        Join the conversation

    • Open Source
      • Next.js

        The native Next.js platform

      • Nuxt

        The progressive web framework

      • Svelte

        The web’s efficient UI framework

      • Turborepo

        Speed with Enterprise scale

    • Use Cases
      • AI Apps

        Deploy at the speed of AI

      • Composable Commerce

        Power storefronts that convert

      • Marketing Sites

        Launch campaigns fast

      • Multi-tenant Platforms

        Scale apps with one codebase

      • Web Apps

        Ship features, not infrastructure

    • Tools
      • Marketplace

        Extend and automate workflows

      • Templates

        Jumpstart app development

      • Partner Finder

        Get help from solution partners

    • Users
      • Platform Engineers

        Automate away repetition

      • Design Engineers

        Deploy for every idea

  • Enterprise
  • Pricing
Log InContact
Sign Up
Sign Up
Back to Templates
DeployView Demo

Next.js Prisma Azure MySQL Starter

Next.js project that uses Prisma to connect to an Azure database for MySQL Flexible Server database and Tailwind CSS for styling.
Next.js Prisma Azure MySQL Starter

Azure CosmosDB - Next.js Starter

This is a Next.js project that uses @azure/cosmos JS SDK to connect to a Azure Cosmos DB database.
Azure CosmosDB - Next.js Starter

Postgres + Drizzle Next.js Starter

Simple Next.js template that uses a Postgres database and Drizzle as the ORM.
Postgres + Drizzle Next.js Starter
v0

Build applications with AI

AI SDK

The AI Toolkit for TypeScript

AI Gateway

One endpoint, all your models

Vercel Agent

An agent that knows your stack

Sandbox

AI workflows in live environments

CI/CD

Helping teams ship 6Ɨ faster

Content Delivery

Fast, scalable, and reliable

Fluid Compute

Servers, in serverless form

Observability

Trace every step

Bot Management

Scalable bot protection

BotID

Invisible CAPTCHA

Platform Security

DDoS Protection, Firewall

Web Application Firewall

Granular, custom protection

Customers

Trusted by the best teams

Blog

The latest posts and changes

Changelog

See what shipped

Press

Read the latest news

Events

Join us at an event

Docs

Vercel documentation

Academy

Linear courses to level up

Knowledge Base

Find help quickly

Community

Join the conversation

Next.js

The native Next.js platform

Nuxt

The progressive web framework

Svelte

The web’s efficient UI framework

Turborepo

Speed with Enterprise scale

AI Apps

Deploy at the speed of AI

Composable Commerce

Power storefronts that convert

Marketing Sites

Launch campaigns fast

Multi-tenant Platforms

Scale apps with one codebase

Web Apps

Ship features, not infrastructure

Marketplace

Extend and automate workflows

Templates

Jumpstart app development

Partner Finder

Get help from solution partners

Platform Engineers

Automate away repetition

Design Engineers

Deploy for every idea