2023-02-09 21:51:12 +00:00
|
|
|
// middleware.ts
|
|
|
|
|
import { NextResponse } from 'next/server'
|
|
|
|
|
import type { NextRequest } from 'next/server'
|
2023-02-22 13:05:52 +00:00
|
|
|
import { withAuth } from "next-auth/middleware"
|
2023-02-09 21:51:12 +00:00
|
|
|
|
2023-02-22 13:05:52 +00:00
|
|
|
export default withAuth((request: NextRequest) => {
|
|
|
|
|
if (request.nextUrl.pathname.startsWith('/login')) {
|
|
|
|
|
return NextResponse.next()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @ts-expect-error
|
|
|
|
|
if (!request.nextauth.token) {
|
|
|
|
|
return NextResponse.redirect("/login")
|
|
|
|
|
}
|
|
|
|
|
// @ts-expect-error
|
|
|
|
|
console.log({ token: request.nextauth.token })
|
2023-02-09 21:51:12 +00:00
|
|
|
console.log("INTO middleware")
|
|
|
|
|
const path = request.nextUrl.pathname
|
|
|
|
|
console.log({ path })
|
2023-02-22 14:08:53 +00:00
|
|
|
const zammadPaths = ['/zammad', '/assets', '/api/v1', '/auth/sso', '/ws'];
|
2023-02-22 13:05:52 +00:00
|
|
|
if (zammadPaths.some((p) => path.startsWith(p))) {
|
|
|
|
|
console.log("MATCHED ZAMMAD PATH")
|
2023-02-09 21:51:12 +00:00
|
|
|
const finalURL = new URL(path.replace("/zammad", ""), process.env.ZAMMAD_URL)
|
|
|
|
|
console.log(finalURL.toString())
|
|
|
|
|
|
2023-02-22 13:05:52 +00:00
|
|
|
const requestHeaders = new Headers(request.headers)
|
|
|
|
|
// @ts-expect-error
|
|
|
|
|
requestHeaders.set('X-Forwarded-User', request.nextauth.token?.email)
|
2023-02-09 21:51:12 +00:00
|
|
|
requestHeaders.set('Host', 'zammad.example.com')
|
|
|
|
|
|
|
|
|
|
console.log(requestHeaders)
|
|
|
|
|
return NextResponse.rewrite(finalURL, {
|
|
|
|
|
request: {
|
|
|
|
|
headers: requestHeaders
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
} else {
|
2023-02-22 13:05:52 +00:00
|
|
|
console.log("DID NOT MATCH ZAMMAD PATH")
|
2023-02-09 21:51:12 +00:00
|
|
|
return NextResponse.next()
|
|
|
|
|
}
|
2023-02-22 13:05:52 +00:00
|
|
|
}, {
|
|
|
|
|
pages: {
|
|
|
|
|
signIn: '/login',
|
|
|
|
|
}
|
|
|
|
|
})
|