Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: api routes should be able to return non-200 status #1228

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/app/api/mobile/login/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function postHandler (request: NextRequest): Promise<NextResponse> {
throw new Error('Invalid payload')
}
} catch (error) {
return NextResponse.json({ error: 'Unexpected error', status: 400 })
return NextResponse.json({ error: 'Unexpected error' }, { status: 400 })
}

let response: Auth0.JSONApiResponse<Auth0.TokenSet> | undefined
Expand All @@ -30,12 +30,21 @@ async function postHandler (request: NextRequest): Promise<NextResponse> {
audience: 'https://api.openbeta.io',
realm: 'Username-Password-Authentication'
})

return NextResponse.json({ data: response.data })
return NextResponse.json({ ...response.data }, { status: response.status })
} catch (error) {
console.error('#### Auth0 error ####', error)
return NextResponse.json({ error: 'Unexpected auth error', status: 403 })
return errorHandler(error)
}
}

export const POST = withMobileAuth(postHandler)

/**
* Handle Auth0 errors
*/
export const errorHandler = (error: any): NextResponse => {
console.error('#### Auth0 error ####', error)
if (error instanceof Auth0.AuthApiError) {
return NextResponse.json({ error: error?.error_description ?? '' }, { status: error?.statusCode ?? 401 })
}
return NextResponse.json({ error: 'Unexpected auth error' }, { status: 401 })
}
6 changes: 3 additions & 3 deletions src/app/api/mobile/refreshToken/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from 'next/server'
import * as Auth0 from 'auth0'
import { auth0Client, isNullOrEmpty } from '@/js/auth/mobile'
import { withMobileAuth } from '@/js/auth/withMobileAuth'
import { errorHandler } from '../login/route'

/**
* Mobile refresh token handler
Expand All @@ -27,10 +28,9 @@ async function postHandler (request: NextRequest): Promise<any> {
audience: 'https://api.openbeta.io'
})

return NextResponse.json({ data: response.data })
return NextResponse.json({ ...response.data }, { status: response.status })
} catch (error) {
console.error('#### Auth0 error ####', error)
return NextResponse.json({ error: 'Unexpected auth error', status: 403 })
return errorHandler(error)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/js/auth/withMobileAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ type Next13ApiHandler = (req: NextRequest) => Promise<NextResponse>
export const withMobileAuth = (handler: Next13ApiHandler): Next13ApiHandler => {
return async function (request: NextRequest) {
if (request.method !== 'POST') {
return NextResponse.json({ message: 'Must send POST request', status: 405 })
return NextResponse.json({ message: 'Must send POST request' }, { status: 405 })
}
const authHeader = request.headers.get('Secret')
if (mobileAuthSecret != null && authHeader === mobileAuthSecret) {
return await handler(request)
}
return NextResponse.json({ message: 'Unauthorized', status: 401 })
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 })
}
}
Loading