forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
142 lines (117 loc) · 3.93 KB
/
middleware.ts
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server';
import { NextRequest, NextResponse } from 'next/server';
import { UAParser } from 'ua-parser-js';
import urlJoin from 'url-join';
import { appEnv } from '@/config/app';
import { authEnv } from '@/config/auth';
import { LOBE_THEME_APPEARANCE } from '@/const/theme';
import NextAuthEdge from '@/libs/next-auth/edge';
import { Locales } from '@/locales/resources';
import { parseBrowserLanguage } from '@/utils/locale';
import { parseDefaultThemeFromCountry } from '@/utils/server/geo';
import { RouteVariants } from '@/utils/server/routeVariants';
import { OAUTH_AUTHORIZED } from './const/auth';
export const config = {
matcher: [
// include any files in the api or trpc folders that might have an extension
'/(api|trpc|webapi)(.*)',
// include the /
'/',
'/discover',
'/discover(.*)',
'/chat',
'/chat(.*)',
'/changelog(.*)',
'/settings(.*)',
'/files',
'/files(.*)',
'/repos(.*)',
'/profile(.*)',
'/me',
'/me(.*)',
'/login(.*)',
'/signup(.*)',
'/next-auth/error',
// ↓ cloud ↓
],
};
const defaultMiddleware = (request: NextRequest) => {
const url = new URL(request.url);
// skip all api requests
if (['/api', '/trpc', '/webapi'].some((path) => url.pathname.startsWith(path))) {
return NextResponse.next();
}
// 1. 从 cookie 中读取用户偏好
const theme =
request.cookies.get(LOBE_THEME_APPEARANCE)?.value || parseDefaultThemeFromCountry(request);
// if it's a new user, there's no cookie
// So we need to use the fallback language parsed by accept-language
const locale = parseBrowserLanguage(request.headers) as Locales;
// const locale =
// request.cookies.get(LOBE_LOCALE_COOKIE)?.value ||
// browserLanguage;
const ua = request.headers.get('user-agent');
const device = new UAParser(ua || '').getDevice();
// 2. 创建规范化的偏好值
const route = RouteVariants.serializeVariants({
isMobile: device.type === 'mobile',
locale,
theme,
});
// if app is in docker, rewrite to self container
// https://github.com/lobehub/lobe-chat/issues/5876
if (appEnv.MIDDLEWARE_REWRITE_THROUGH_LOCAL) {
url.protocol = 'http';
url.host = '127.0.0.1';
url.port = process.env.PORT || '3210';
}
// refs: https://github.com/lobehub/lobe-chat/pull/5866
// new handle segment rewrite: /${route}${originalPathname}
// / -> /zh-CN__0__dark
// /discover -> /zh-CN__0__dark/discover
const nextPathname = `/${route}` + (url.pathname === '/' ? '' : url.pathname);
const nextURL = appEnv.MIDDLEWARE_REWRITE_THROUGH_LOCAL
? urlJoin(url.origin, nextPathname)
: nextPathname;
console.log(`[rewrite] ${url.pathname} -> ${nextURL}`);
url.pathname = nextPathname;
return NextResponse.rewrite(url, { status: 200 });
};
// Initialize an Edge compatible NextAuth middleware
const nextAuthMiddleware = NextAuthEdge.auth((req) => {
const response = defaultMiddleware(req);
// Just check if session exists
const session = req.auth;
// Check if next-auth throws errors
// refs: https://github.com/lobehub/lobe-chat/pull/1323
const isLoggedIn = !!session?.expires;
// Remove & amend OAuth authorized header
response.headers.delete(OAUTH_AUTHORIZED);
if (isLoggedIn) {
response.headers.set(OAUTH_AUTHORIZED, 'true');
}
return response;
});
const isProtectedRoute = createRouteMatcher([
'/settings(.*)',
'/files(.*)',
'/onboard(.*)',
// ↓ cloud ↓
]);
const clerkAuthMiddleware = clerkMiddleware(
async (auth, req) => {
if (isProtectedRoute(req)) await auth.protect();
return defaultMiddleware(req);
},
{
// https://github.com/lobehub/lobe-chat/pull/3084
clockSkewInMs: 60 * 60 * 1000,
signInUrl: '/login',
signUpUrl: '/signup',
},
);
export default authEnv.NEXT_PUBLIC_ENABLE_CLERK_AUTH
? clerkAuthMiddleware
: authEnv.NEXT_PUBLIC_ENABLE_NEXT_AUTH
? nextAuthMiddleware
: defaultMiddleware;