Skip to content

Commit

Permalink
add dev login feature
Browse files Browse the repository at this point in the history
  • Loading branch information
recursiveforte committed Jun 26, 2024
1 parent d4786aa commit 0eb1456
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ PUBLIC_MAX_LOOP_TIME_MS=
GRAPHITE_HOST=
STUCK_AIRTABLE_BASE=
SENDGRID_API_KEY=
LOOPS_API_KEY=
LOOPS_API_KEY=
DEV_CODE=
4 changes: 2 additions & 2 deletions src/components/big-interactive-pages/login.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useSignalEffect } from '@preact/signals'
import { IoPaperPlaneOutline } from 'react-icons/io5'
import { SessionInfo } from '../../lib/game-saving/account'
import { useAuthHelper } from '../../lib/game-saving/auth-helper'
import {DevEmail, useAuthHelper} from '../../lib/game-saving/auth-helper'
import Button from '../design-system/button'
import Input from '../design-system/input'
import LinkButton from '../design-system/link-button'
Expand Down Expand Up @@ -56,7 +56,7 @@ export default function Login({ session, email, to }: LoginProps) {
</p>

<label for='code'>Code:</label>
<Input onChange={() => undefined} value={auth.code.value} id='code' type='text' maxLength={6} placeholder='123456' bind={auth.code} />
<Input onChange={() => undefined} value={auth.code.value} id='code' type='text' maxLength={auth.email.value == DevEmail ? 70 : 6} placeholder='123456' bind={auth.code} />
{auth.state.value === 'CODE_INCORRECT' && <p class={styles.error}>Incorrect login code.</p>}

<Button class={styles.submit} accent type='submit' disabled={!auth.codeValid.value} loading={auth.isLoading.value}>
Expand Down
4 changes: 3 additions & 1 deletion src/lib/game-saving/auth-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export type AuthState =
| 'LOGGED_IN'

export type AuthStage = 'IDLE' | 'EMAIL' | 'CODE' | 'LOGGED_IN'

export const DevEmail = "development@hackclub.com"

export const useAuthHelper = (initialState: AuthState = 'IDLE', initialEmail: string = '') => {
const state = useSignal(initialState)
Expand All @@ -31,7 +33,7 @@ export const useAuthHelper = (initialState: AuthState = 'IDLE', initialEmail: st
const emailValid = useComputed(() => isValidEmail(email.value))

const code = useSignal('')
const codeValid = useComputed(() => code.value.length === 6)
const codeValid = useComputed(() => code.value.length === 6 || email.value === DevEmail)
useSignalEffect(() => { code.value = code.value.replace(/[^0-9]/g, '') })

const startEmailEntry = () => { state.value = 'EMAIL_ENTRY' }
Expand Down
8 changes: 7 additions & 1 deletion src/pages/api/auth/submit-code.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { APIRoute } from 'astro'
import { getSession, getUserByEmail, makeOrUpdateSession, findDocument } from '../../../lib/game-saving/account'
import { isValidEmail } from '../../../lib/game-saving/email'
import {DevEmail} from "../../../lib/game-saving/auth-helper";

export const post: APIRoute = async ({ request, cookies }) => {
const session = await getSession(cookies)
Expand All @@ -16,14 +17,19 @@ export const post: APIRoute = async ({ request, cookies }) => {

if (typeof body.code !== 'string') throw 'Missing/invalid code'
const trimmed = body.code.replace(/[^0-9]/g, '')
if (trimmed.length !== 6) throw 'Code must be 6 digits long'
if (trimmed.length !== 6 && email !== DevEmail) throw 'Code must be 6 digits long'
code = trimmed
} catch (error) {
return new Response(typeof error === 'string' ? error : 'Bad request body', { status: 400 })
}

const user = await getUserByEmail(email!) ?? session?.user
if (!user) return new Response('Invalid email or session', { status: 401 })

if (email === DevEmail && code == import.meta.env.DEV_CODE) {
await makeOrUpdateSession(cookies, user.id, 'code')
return new Response(JSON.stringify({ user }), { status: 200 })
}

const _codes = await findDocument('loginCodes', [
['code', '==', code],
Expand Down

0 comments on commit 0eb1456

Please sign in to comment.