Skip to content

Commit

Permalink
Merge pull request #50 from hinagiku-dev/New-code
Browse files Browse the repository at this point in the history
Join session with code
  • Loading branch information
180079995 authored Dec 22, 2024
2 parents 329cb67 + b400cc6 commit c6347bc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/routes/api/session/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ export const POST: RequestHandler = async ({ request, locals }) => {
const sessionRef = adminDb.collection('sessions').doc();
await sessionRef.set(result.data);

const code = Math.floor(100000 + Math.random() * 900000);
let Codes = adminDb.collection('temp_codes').doc(code.toString());
await Codes.set({
sessionId: sessionRef.id
});

Codes = adminDb.collection('temp_codes').doc(sessionRef.id);
await Codes.set({
code: code
});

return json({
success: true,
sessionId: sessionRef.id
Expand Down
44 changes: 44 additions & 0 deletions src/routes/join/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { goto } from '$app/navigation';
import QrScanner from '$lib/components/QrScanner.svelte';
import { notifications } from '$lib/stores/notifications';
import { getDoc, doc } from 'firebase/firestore';
import { db } from '$lib/firebase';
async function handleScan(code: string) {
try {
Expand All @@ -16,6 +18,28 @@
console.error(e);
}
}
let code = $state('');
async function joinwithcode() {
if (code.length != 6 || !/^\d{6}$/.test(code)) {
notifications.error('Code must be 6 digits');
return;
}
try {
const codeDoc = await getDoc(doc(db, 'temp_codes', code));
const sessionid = codeDoc.data()?.sessionId;
if (!sessionid) {
notifications.error('Invalid session code');
return;
}
const url = new URL(`/session/${sessionid}`, window.location.href);
await goto(url);
} catch (e) {
notifications.error('Invalid session code');
console.error(e);
}
}
</script>

<svelte:head>
Expand All @@ -32,4 +56,24 @@

<QrScanner onScan={handleScan} />
</div>
<!--Input Code-->
<div class="mt-8 flex flex-col gap-4">
<p class="text-gray-600">Or please enter the 6-digits code to join.</p>
<input
type="text"
placeholder="Enter code"
maxlength="6"
pattern="\d{6}"
class="w-full rounded-lg border px-6 py-2"
bind:value={code}
required
/>
<button
type="button"
class="w-full rounded-lg border px-6 py-2 hover:bg-gray-50"
onclick={joinwithcode}
>
Join Session
</button>
</div>
</main>

0 comments on commit c6347bc

Please sign in to comment.