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.
- Loading branch information
Showing
3 changed files
with
85 additions
and
508 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,63 @@ | ||
import { NextRequest, NextResponse } from 'next/server' | ||
import * as Auth0 from 'auth0' | ||
|
||
import { AUTH_CONFIG_SERVER } from '../../../../Config' | ||
|
||
if (AUTH_CONFIG_SERVER == null) throw new Error('AUTH_CONFIG_SERVER not defined') | ||
|
||
const mobileAuthSecret = process.env.MOBILE_AUTH_SECRET | ||
if (mobileAuthSecret == null) { | ||
console.warn('Mobile auth secret not found') | ||
} | ||
|
||
const { clientSecret, clientId, issuer } = AUTH_CONFIG_SERVER | ||
|
||
// Set up Auth0 client | ||
const auth = new Auth0.AuthenticationClient({ | ||
domain: issuer.replace('https://', ''), | ||
clientId, | ||
clientSecret | ||
}) | ||
|
||
/** | ||
* Mobile login handler | ||
*/ | ||
export async function POST (request: NextRequest): Promise<any> { | ||
const authHeader = request.headers.get('User-Agent') | ||
if (mobileAuthSecret != null && authHeader !== mobileAuthSecret) { | ||
return NextResponse.json({ message: 'Unauthorized', status: 401 }) | ||
} | ||
|
||
let username, password: string | ||
try { | ||
const data = await request.json() | ||
username = data.username | ||
password = data.password | ||
|
||
if (isNullOrEmpty(username) || isNullOrEmpty(password)) { | ||
console.error('Empty username/password!') | ||
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 auth.oauth.passwordGrant({ | ||
username, | ||
password, | ||
scope: 'openid profile email offline_access', | ||
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 }) | ||
} | ||
} | ||
|
||
function isNullOrEmpty (str: string | null | undefined): boolean { | ||
return str?.trim() === '' | ||
} |
Oops, something went wrong.