generated from taylorbryant/gatsby-starter-tailwind
-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use a common secret checker
- Loading branch information
Showing
4 changed files
with
87 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { NextRequest, NextResponse } from 'next/server' | ||
import * as Auth0 from 'auth0' | ||
import { auth0Client, isNullOrEmpty } from '@/js/auth/mobile' | ||
import { withMobileAuth } from '@/js/auth/withMobileAuth' | ||
|
||
/** | ||
* Mobile refresh token handler | ||
*/ | ||
async function postHandler (request: NextRequest): Promise<any> { | ||
let refreshToken: string | ||
try { | ||
const data = await request.json() | ||
refreshToken = data.refreshToken | ||
|
||
if (isNullOrEmpty(refreshToken)) { | ||
console.error('Empty refreshToken!') | ||
throw new Error('Invalid payload') | ||
} | ||
} catch (error) { | ||
return NextResponse.json({ error: 'Unexpected error', status: 400 }) | ||
} | ||
|
||
let response: Auth0.JSONApiResponse<Auth0.TokenSet> | undefined | ||
try { | ||
response = await auth0Client.oauth.refreshTokenGrant({ | ||
refresh_token: refreshToken, | ||
audience: 'https://api.openbeta.io' | ||
}) | ||
|
||
return NextResponse.json({ data: response.data }) | ||
} catch (error) { | ||
console.error('#### Auth0 error ####', error) | ||
return NextResponse.json({ error: 'Unexpected auth error', status: 403 }) | ||
} | ||
} | ||
|
||
export const POST = withMobileAuth(postHandler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as Auth0 from 'auth0' | ||
|
||
import { AUTH_CONFIG_SERVER } from '../../Config' | ||
|
||
if (AUTH_CONFIG_SERVER == null) throw new Error('AUTH_CONFIG_SERVER not defined') | ||
|
||
if (process.env.MOBILE_AUTH_SECRET == null) { | ||
console.warn('Mobile auth secret not found') | ||
} | ||
|
||
const { clientSecret, clientId, issuer } = AUTH_CONFIG_SERVER | ||
|
||
// Set up Auth0 client | ||
export const auth0Client = new Auth0.AuthenticationClient({ | ||
domain: issuer.replace('https://', ''), | ||
clientId, | ||
clientSecret | ||
}) | ||
|
||
export const isNullOrEmpty = (str: string | null | undefined): boolean => { | ||
return str == null || str?.trim() === '' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { NextRequest, NextResponse } from 'next/server' | ||
|
||
const mobileAuthSecret = process.env.MOBILE_AUTH_SECRET | ||
|
||
type Next13ApiHandler = (req: NextRequest) => Promise<NextResponse> | ||
|
||
/** | ||
* A high-order function to protect mobile-related auth endpoints. | ||
* Do not use elsewhere. | ||
*/ | ||
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 }) | ||
} | ||
const authHeader = request.headers.get('Secret') | ||
if (mobileAuthSecret != null && authHeader === mobileAuthSecret) { | ||
return await handler(request) | ||
} | ||
return NextResponse.json({ message: 'Unauthorized', status: 401 }) | ||
} | ||
} |