-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
60 lines (49 loc) · 1.55 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
import { NextRequest, NextResponse } from 'next/server'
import { auth } from '@/auth'
export default auth(async (req) => {
const isAdminRoute = req.nextUrl.pathname.startsWith('/admin')
if (!isAdminRoute) return
if (isAdminRoute) {
if ((await isAuthorized(req)) == false)
return new NextResponse('Unauthorized', {
status: 401,
headers: {
'WWW-Authenticate': 'basic',
},
})
}
return
})
//->>>>>>>>>>.without using auth.js <<<<<<<<<<<<<<<<----
// export default async function middleware(req: NextRequest) {
// // console.log(req.cookies)
// console.log(req)
// // console.log(a)
// // if ((await isAuthorized(req)) == false) {
// // return new NextResponse('Unauthorized', {
// // status: 401,
// // headers: {
// // 'WWW-Authenticate': 'basic',
// // },
// // })
// // }
// }
async function isAuthorized(req: NextRequest) {
const authHeader =
req.headers.get('authorization') || req.headers.get('Authorization')
if (!authHeader) return false
const [userName, password] = Buffer.from(authHeader.split(' ')[1], 'base64')
.toString()
.split(':')
if (
userName === process.env.ADMIN_USER_NAME &&
password === process.env.ADMIN_PASSWORD
) {
return true
}
return false
}
export const config = {
// matcher: ['/((?!.*\\..*|_next).*)', '/', '/(api|trpc)(.*)'],
mathcher: ['/admin/:path*'],
}