-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
43 lines (37 loc) · 1.19 KB
/
middleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { NextResponse } from 'next/server';
import { getToken } from 'next-auth/jwt';
import prisma from './lib/prisma';
export async function middleware(req) {
const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET });
const { pathname } = req.nextUrl;
// Check if the path matches an article route
const articleMatch = pathname.match(/^\/([^/]+)\/([^/]+)$/);
if (articleMatch) {
const [, author, titleSlug] = articleMatch;
// Fetch the article from the database
const article = await prisma.article.findFirst({
where: {
author: author,
titleSlug: titleSlug
},
select: {
tags: true
}
});
// Check if the article has the 'vc' tag
if (article && article.tags.includes('vc')) {
// If not authenticated, redirect to login
if (!token) {
return NextResponse.redirect(new URL('/api/auth/signin', req.url));
}
// If authenticated but not premium, redirect to subscribe page
if (!token.isPremium) {
return NextResponse.redirect(new URL('/subscribe', req.url));
}
}
}
return NextResponse.next();
}
export const config = {
matcher: '/:author/:titleSlug'
};