-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use random token for claiming scratches (#945)
* Use random token for claiming scratches * Fix migration dep * Fix claim_token serialization & address PR comments * Improve error handling * Update backend tests * reformat * Update another test * Minor update to ClaimableScratchSerializer
- Loading branch information
Showing
11 changed files
with
135 additions
and
21 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Generated by Django 5.0 on 2024-01-17 00:43 | ||
|
||
import coreapp.models.scratch | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("coreapp", "0049_alter_assembly_source_asm"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="scratch", | ||
name="claim_token", | ||
field=models.CharField( | ||
default=coreapp.models.scratch.gen_claim_token, max_length=64, null=True | ||
), | ||
), | ||
] |
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
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
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
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,46 @@ | ||
"use client" | ||
|
||
import { useEffect, useRef, useState } from "react" | ||
|
||
import { useRouter } from "next/navigation" | ||
|
||
import LoadingSkeleton from "@/app/scratch/[slug]/loading" | ||
import { post } from "@/lib/api/request" | ||
|
||
export default function Page({ params, searchParams }: { | ||
params: { slug: string } | ||
searchParams: { token: string } | ||
}) { | ||
const router = useRouter() | ||
|
||
// The POST request must happen on the client so | ||
// that the Django session cookie is present. | ||
const effectRan = useRef(false) | ||
const [error, setError] = useState(null) | ||
useEffect(() => { | ||
if (!effectRan.current) { | ||
post(`/scratch/${params.slug}/claim`, { token: searchParams.token }) | ||
.then(data => { | ||
if (data.success) { | ||
router.replace(`/scratch/${params.slug}`) | ||
} else { | ||
throw new Error("Unable to claim scratch") | ||
} | ||
}) | ||
.catch(err => { | ||
console.error("Failed to claim scratch", err) | ||
setError(err) | ||
}) | ||
} | ||
|
||
return () => { | ||
effectRan.current = true | ||
} | ||
}, [params.slug, router, searchParams.token]) | ||
|
||
if (error) { | ||
// Rely on error boundary to catch and display error | ||
throw error | ||
} | ||
return <LoadingSkeleton/> | ||
} |
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
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