Middleware and login updates

This commit is contained in:
Darren Clarke 2023-02-22 13:05:52 +00:00
parent 484e8689a4
commit 4517241ead
23 changed files with 144 additions and 112 deletions

View file

@ -1,19 +1,31 @@
// middleware.ts
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { withAuth } from "next-auth/middleware"
// This function can be marked `async` if using `await` inside
export function middleware(request: NextRequest) {
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 })
console.log("INTO middleware")
const path = request.nextUrl.pathname
console.log({ path })
if (path.startsWith('/zammad')) {
console.log("INTO middleware 2")
const zammadPaths = ['/zammad', '/assets', '/api/v1', '/auth/sso'];
if (zammadPaths.some((p) => path.startsWith(p))) {
console.log("MATCHED ZAMMAD PATH")
const finalURL = new URL(path.replace("/zammad", ""), process.env.ZAMMAD_URL)
console.log(finalURL.toString())
const requestHeaders = new Headers()
requestHeaders.set('X-Forwarded-User', 'darren@redaranj.com')
const requestHeaders = new Headers(request.headers)
// @ts-expect-error
requestHeaders.set('X-Forwarded-User', request.nextauth.token?.email)
requestHeaders.set('Host', 'zammad.example.com')
console.log(requestHeaders)
@ -23,7 +35,11 @@ export function middleware(request: NextRequest) {
}
})
} else {
console.log("INTO middleware 3")
console.log("DID NOT MATCH ZAMMAD PATH")
return NextResponse.next()
}
}
}, {
pages: {
signIn: '/login',
}
})