Skip to content

Commit

Permalink
Merge pull request #58 from Sports-day/feature/sub-directory-hosting
Browse files Browse the repository at this point in the history
サブディレクトホスティングに関するミドルウェアの修正
  • Loading branch information
1nayu authored Oct 6, 2024
2 parents 00f9db0 + bfdad44 commit 2938b64
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
9 changes: 5 additions & 4 deletions app/api/auth/callback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export async function POST(request: NextRequest) {
const code = form.get('code')

// pass code to the backend
const loginEndpoint = process.env.NEXT_PUBLIC_API_URL + '/login'
const backendURL = process.env.NEXT_PUBLIC_API_INTERNAL_URL ? process.env.NEXT_PUBLIC_API_INTERNAL_URL : process.env.NEXT_PUBLIC_API_URL
const loginEndpoint = backendURL + '/login'

// redirect uri
const redirectUri = process.env.NEXT_PUBLIC_OIDC_REDIRECT_URL
Expand All @@ -26,13 +27,13 @@ export async function POST(request: NextRequest) {

// get cookie from response
const cookie = response.headers.get('set-cookie')

const subDirectory = process.env.SUB_DIRECTORY ? process.env.SUB_DIRECTORY : "/"
if (cookie) {
// redirect to root page
return new Response(null, {
status: 301,
headers: {
"Location": '/',
"Location": subDirectory,
"Set-Cookie": cookie,
},
})
Expand All @@ -42,7 +43,7 @@ export async function POST(request: NextRequest) {
return new Response(null, {
status: 301,
headers: {
"Location": '/',
"Location": subDirectory,
},
})
}
8 changes: 7 additions & 1 deletion middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ export function middleware(request: NextRequest) {

// check if next path starts with any of the protected paths or root
if (protectedPaths.some(path => request.nextUrl.pathname.startsWith(path)) || request.nextUrl.pathname === '/') {
return NextResponse.redirect(new URL('/login', request.url));
const subDirectory = process.env.SUB_DIRECTORY
if (subDirectory) {
return NextResponse.redirect(new URL(subDirectory + '/login', request.url));
}
else {
return NextResponse.redirect(new URL('/login', request.url));
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib/ApiClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const ApiUrl = process.env.NEXT_PUBLIC_API_URL;
const ApiInternalUrl = process.env.NEXT_PUBLIC_API_INTERNAL_URL ? process.env.NEXT_PUBLIC_API_INTERNAL_URL : ApiUrl;

const headers = {
"Content-Type": "application/json",
Expand All @@ -24,7 +25,7 @@ async function fetchWithToken(url: string, options: RequestInit | undefined = {}
}

try {
const fullUrl = `${ApiUrl}${url}`
const fullUrl = typeof window === 'undefined' ? `${ApiInternalUrl}${url}` : `${ApiUrl}${url}`
const response = await fetch(fullUrl, mergedOptions);

// Check if the response was ok (status in the range 200-299)
Expand Down

0 comments on commit 2938b64

Please sign in to comment.