Skip to content

Commit

Permalink
Add vercel cron authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunkomath committed Feb 10, 2024
1 parent 069d39d commit 2743bc2
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/pages/vercel-cron-job-authentication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
template: post
title: "Authenticating Vercel Cron Jobs"
date_published: 1707568444408
amp: true
---

Vercel Cron Jobs are a powerful tool for automating repetitive tasks. By using cron expressions, you can schedule tasks to run automatically at specific intervals. However, securing these cron jobs is crucial to ensure that they can't be run by just anyone with the known route.

Here's a step-by-step guide on how to authenticate Vercel Cron Jobs:
- **Create a Secret Key**: Use the `openssl rand -hex 32` command in your terminal to generate a secret key.
- **Add the Secret Key to Your Environment Variables**: Add the CRON_SECRET variable to your local .env file with the value generated from the previous step.
- **Modify Your Cron Job Route**: Look for the bearer token in your cron job route. If it doesn't exist or if it doesn't match the CRON_SECRET, return an unauthorized response with a 401 status code.

```ts
import { NextRequest, NextResponse } from 'next/server'

export async function GET(req: NextRequest) {
// get the bearer token from the header
const authToken = (req.headers.get('authorization') || '')
.split('Bearer ')
.at(1)

// if not found OR the bearer token does NOT equal the CRON_SECRET
if (!authToken || authToken != process.env.CRON_SECRET) {
return NextResponse.json(
{ error: 'Unauthorized' },
{ status: 401 }
)
}

// if token exists then move on with the cron job ...
}
```

- **Add the Secret Key to Your Vercel Project Settings**: Add the CRON_SECRET variable to the Vercel project settings Environment Variables.
- **Test Your Setup**: Deploy your project to Vercel and test on your live site. You should receive an error message "Unauthorized" if the setup is correct. Next, try triggering the cron job from Vercel dashboard, it should run as expected.

By following these steps, you can ensure that only Vercel can run the cron job from their servers.

0 comments on commit 2743bc2

Please sign in to comment.