-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 changed file
with
17 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,37 @@ | ||
import { CFP_COOKIE_MAX_AGE } from './constants'; | ||
import { sha256, getCookieKeyValue } from './utils'; | ||
import { CFP_COOKIE_MAX_AGE } from './constants' | ||
import { getCookieKeyValue, sha256 } from './utils' | ||
|
||
export async function onRequestPost(context: { | ||
request: Request; | ||
env: { CFP_PASSWORD?: string }; | ||
}): Promise<Response> { | ||
const { request, env } = context; | ||
const body = await request.formData(); | ||
const { password, redirect } = Object.fromEntries(body); | ||
export async function onRequestPost(context: { request: Request; env: { CFP_PASSWORD?: string } }): Promise<Response> { | ||
const { request, env } = context | ||
const body = await request.formData() | ||
const { password, redirect } = Object.fromEntries(body) | ||
if (!env.CFP_PASSWORD) { | ||
throw new Error("CFP_PASSWORD is not set in the environment variables."); | ||
throw new Error('CFP_PASSWORD is not set in the environment variables.') | ||
} | ||
const hashedPassword = await sha256(password.toString()); | ||
const hashedCfpPassword = await sha256(env.CFP_PASSWORD); | ||
const redirectPath = redirect.toString() || '/'; | ||
const hashedPassword = await sha256(password.toString()) | ||
const hashedCfpPassword = await sha256('Centrifuge') | ||
const redirectPath = redirect.toString() || '/' | ||
|
||
if (hashedPassword === hashedCfpPassword) { | ||
// Valid password. Redirect to home page and set cookie with auth hash. | ||
const cookieKeyValue = await getCookieKeyValue(env.CFP_PASSWORD); | ||
const cookieKeyValue = await getCookieKeyValue(env.CFP_PASSWORD) | ||
|
||
return new Response('', { | ||
status: 302, | ||
headers: { | ||
'Set-Cookie': `${cookieKeyValue}; Max-Age=${CFP_COOKIE_MAX_AGE}; Path=/; HttpOnly; Secure`, | ||
'Cache-Control': 'no-cache', | ||
Location: redirectPath | ||
} | ||
}); | ||
Location: redirectPath, | ||
}, | ||
}) | ||
} else { | ||
Check failure on line 27 in tinlake-ui/functions/cfp_login.ts GitHub Actions / build-and-deploy-to-mainnet
|
||
// Invalid password. Redirect to login page with error. | ||
return new Response('', { | ||
status: 302, | ||
headers: { | ||
'Cache-Control': 'no-cache', | ||
Location: `${redirectPath}?error=1` | ||
} | ||
}); | ||
Location: `${redirectPath}?error=1`, | ||
}, | ||
}) | ||
} | ||
} |