Skip to content

Commit

Permalink
redirect to public url in middleware #435
Browse files Browse the repository at this point in the history
  • Loading branch information
maany committed Aug 15, 2024
1 parent 66b7642 commit a03e562
Showing 1 changed file with 36 additions and 30 deletions.
66 changes: 36 additions & 30 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,78 @@
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { getIronSession } from 'iron-session/edge'
import { getIronSession } from 'iron-session/edge'
import { sessionOptions } from './lib/infrastructure/auth/session'
import { IronSession } from 'iron-session'
import { validateRucioToken } from './lib/infrastructure/auth/auth-utils'


const getSession = async(
const getSession = async (
req: NextRequest,
res: NextResponse,
): Promise<IronSession> => {
const session = await getIronSession(
req, res, sessionOptions
)
const session = await getIronSession(req, res, sessionOptions)
return session
}

export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
'/((?!api/auth|_next/static|_next/image|favicon.ico|auth/.*).*)',
]
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
'/((?!api/auth|_next/static|_next/image|favicon.ico|auth/.*).*)',
],
}

async function reLogin(request: NextRequest) {
const logoutPage = new URL(`/api/auth/logout?callbackUrl=${request.url}`, request.url)
async function reLogin(request: NextRequest, publicHost: string) {
const logoutPage = new URL(
`/api/auth/logout?callbackUrl=${request.nextUrl.pathname}`,
`${publicHost}`,
)
return NextResponse.redirect(logoutPage)
}

async function initiateLogin(request: NextRequest){
const loginPage = new URL(`/api/auth/login?callbackUrl=${request.url}`, request.url)
async function initiateLogin(request: NextRequest, publicHost: string) {
const loginPage = new URL(
`/api/auth/login?callbackUrl=${request.nextUrl.pathname}`,
`${publicHost}`,
)
return NextResponse.redirect(loginPage)
}

export async function middleware(request: NextRequest) {
const response = NextResponse.next()
const publicHost =
process.env.NEXT_PUBLIC_WEBUI_HOST ||
`${request.nextUrl.protocol}//${request.nextUrl.host}`
try {
const session = await getSession(request, response)

// check session
if(!session || !session.user) {
return await initiateLogin(request)
if (!session || !session.user) {
return await initiateLogin(request, publicHost)
}

// check if user is logged in and token exists
if(!session.user.isLoggedIn || !session.user.rucioAuthToken) {
return await reLogin(request)
if (!session.user.isLoggedIn || !session.user.rucioAuthToken) {
return await reLogin(request, publicHost)
}
// check if rucio token is valid
try {
validateRucioToken(session.user)
}
catch(error) {
return await reLogin(request)
} catch (error) {
return await reLogin(request, publicHost)
}

// All checks have passed, redirect to the original request if it exists
return response

}catch(error) {
} catch (error) {
console.log(error)
return new Response('Internal Server Error. Could not authenticate or rediect to login page', { status: 500 })
return new Response(
'Internal Server Error. Could not authenticate or rediect to login page',
{ status: 500 },
)
}
}

0 comments on commit a03e562

Please sign in to comment.