-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiddleware.js
More file actions
60 lines (59 loc) · 2.11 KB
/
middleware.js
File metadata and controls
60 lines (59 loc) · 2.11 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* tgsnake - Telegram MTProto framework for nodejs.
* Copyright (C) 2024 butthx <https://github.com/butthx>
*
* THIS FILE IS PART OF TGSNAKE
*
* tgsnake is a free software : you can redistribute it and/or modify
* it under the terms of the MIT License as published.
*/
import { NextResponse } from 'next/server';
import { cookies } from 'next/headers';
import { getIronSession } from 'iron-session';
// import { locales as localeMiddleware } from 'nextra/locales';
let regex = /^\/?((pages|api)|((.*\/)?images))/i;
export async function middleware(req) {
if (/^\/blog\/post/i.test(req.nextUrl.pathname)) {
const session = await getIronSession(cookies(), {
password: [process.env.PWD_1, process.env.PWD_2, process.env.PWD_3],
cookieName: process.env.COOKIE_NAME,
cookieOptions: {
secure: true,
sameSite: 'strict',
path: '/',
},
});
if (!session.logined || !session.admin) {
return NextResponse.redirect(new URL('/blog', req.url));
}
}
// instant view
/*let userAgent = req.headers.get('user-agent');
if (userAgent == 'TelegramBot (like TwitterBot)' && !regex.test(req.nextUrl.pathname)) {
let locale = req.nextUrl.locale == 'default' ? 'en' : req.nextUrl.locale;
console.log(locale);
let home = ['/', locale, `/${locale}`, `/${locale}/`];
let paths = `/pages${
home.includes(req.nextUrl.pathname)
? `/index.${locale}.html`
: `${req.nextUrl.pathname}.${locale}.html`
}`;
let url =
process.env.VERCEL_URL && process.env.VERCEL_ENV && process.env.VERCEL_ENV !== 'production'
? `https://${process.env.VERCEL_URL}`
: 'https://tgsnake.js.org';
let res = await fetch(`${url}/api/content.json`);
let json = (await res.json()) || [];
console.log(`${userAgent} - ${paths}`);
if (json.includes(paths.replace(/\.html$/, ''))) {
return NextResponse.rewrite(`${url}/${paths}`);
}
}*/
/*if (!/^\/blog/.test(req.nextUrl.pathname)) {
return localeMiddleware(req);
}*/
return;
}
export const config = {
matcher: ['/', '/((?!api|_next/static|_next/image|favicon.ico).*)'],
};