-
Notifications
You must be signed in to change notification settings - Fork 13
Feat/qr sheets #1394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Feat/qr sheets #1394
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d107403
qr sheets
Hugo0 9142aa5
temp
Hugo0 4b2eac0
chore: merge peanut-wallet-dev into feat/qr-sheets
Hugo0 2485019
chore: remove debug logs and restore QR feature implementation
Hugo0 1d1e570
ready for review
Hugo0 ee4db34
fixes
Hugo0 ecad685
fix formatting my friend
Hugo0 c371a62
fix
Hugo0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,3 +1,7 @@ | ||
| // use the stable v16 service worker path per onesignal docs | ||
| // note: onesignal does not publish minor-pinned sw paths; use v16 channel | ||
| // Version Pinning: OneSignal uses major version channels (v16) for their CDN. | ||
| // Per OneSignal documentation, they do not publish minor or patch-pinned service worker URLs. | ||
| // The v16 channel receives security updates and bug fixes automatically while maintaining | ||
| // API compatibility. This is the recommended approach per OneSignal's best practices. | ||
| // For stricter version control, we can consider self-hosting the SDK, but this requires manual updates. | ||
| // Reference: https://documentation.onesignal.com/docs/web-push-quickstart | ||
| importScripts('https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.sw.js') |
This file contains hidden or 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 hidden or 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,265 @@ | ||
| 'use client' | ||
|
|
||
| import { Button } from '@/components/0_Bruddle' | ||
| import Card from '@/components/Global/Card' | ||
| import NavHeader from '@/components/Global/NavHeader' | ||
| import { PEANUT_API_URL } from '@/constants' | ||
| import { useAuth } from '@/context/authContext' | ||
| import { useRouter, useParams } from 'next/navigation' | ||
| import { useCallback, useEffect, useState } from 'react' | ||
| import PeanutLoading from '@/components/Global/PeanutLoading' | ||
| import ErrorAlert from '@/components/Global/ErrorAlert' | ||
| import { Icon } from '@/components/Global/Icons/Icon' | ||
| import { saveRedirectUrl, generateInviteCodeLink, sanitizeRedirectURL } from '@/utils' | ||
| import { getShakeClass } from '@/utils/perk.utils' | ||
| import Cookies from 'js-cookie' | ||
| import { useRedirectQrStatus } from '@/hooks/useRedirectQrStatus' | ||
| import { useHoldToClaim } from '@/hooks/useHoldToClaim' | ||
|
|
||
| export default function RedirectQrClaimPage() { | ||
| const router = useRouter() | ||
| const params = useParams() | ||
| const code = params?.code as string | ||
| const { user } = useAuth() | ||
| const [isLoading, setIsLoading] = useState(false) | ||
| const [error, setError] = useState<string | null>(null) | ||
|
|
||
| // Fetch redirect QR status using shared hook | ||
| const { data: redirectQrData, isLoading: isCheckingStatus, error: redirectQrError } = useRedirectQrStatus(code) | ||
|
|
||
| // Handle redirects based on QR status and authentication | ||
| useEffect(() => { | ||
| // Wait for QR status to load | ||
| if (isCheckingStatus || !redirectQrData) { | ||
| return | ||
| } | ||
|
|
||
| // If QR is already claimed, redirect to the target URL | ||
| if (redirectQrData.claimed && redirectQrData.redirectUrl) { | ||
| // Sanitize redirect URL to prevent open redirect attacks | ||
| // For same-origin URLs, sanitizeRedirectURL returns safe path | ||
| // For external URLs, it returns null (we handle separately) | ||
| const sanitizedPath = sanitizeRedirectURL(redirectQrData.redirectUrl) | ||
|
|
||
| if (sanitizedPath) { | ||
| // Internal redirect - use sanitized path with Next.js router | ||
| router.push(sanitizedPath) | ||
| } else { | ||
| // External redirect - validate it's expected domain before redirecting | ||
| try { | ||
| const url = new URL(redirectQrData.redirectUrl) | ||
| // Allow external redirects ONLY for trusted domains (peanut.me) | ||
| // This relies on backend validation during QR claiming | ||
| if (url.hostname.includes('peanut.me') || url.hostname.includes('localhost')) { | ||
| window.location.href = redirectQrData.redirectUrl | ||
| } else { | ||
| console.error('Untrusted external redirect blocked:', redirectQrData.redirectUrl) | ||
| setError('Invalid QR code destination.') | ||
| } | ||
| } catch (error) { | ||
| console.error('Invalid redirect URL:', redirectQrData.redirectUrl) | ||
| setError('Invalid QR code destination.') | ||
| } | ||
| } | ||
| return | ||
| } | ||
|
|
||
| // QR is not claimed - check authentication | ||
| if (!user) { | ||
| // User not logged in - redirect to setup to create account/login | ||
| saveRedirectUrl() | ||
| router.push('/setup') | ||
| } | ||
| }, [isCheckingStatus, redirectQrData, user, router]) | ||
|
|
||
| const handleClaim = useCallback(async () => { | ||
| // Auth check is already handled by useEffect above | ||
| // If we reach here, user is authenticated | ||
| setIsLoading(true) | ||
| setError(null) | ||
|
|
||
| try { | ||
| // Generate invite link with correct 3-digit suffix | ||
| const username = user?.user?.username | ||
| if (!username) { | ||
| throw new Error('Username not found') | ||
| } | ||
|
|
||
| const { inviteLink } = generateInviteCodeLink(username) | ||
|
|
||
| const response = await fetch(`${PEANUT_API_URL}/qr/${code}/claim`, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| Authorization: `Bearer ${Cookies.get('jwt-token')}`, | ||
| }, | ||
| body: JSON.stringify({ | ||
| targetUrl: inviteLink, // Pass the correctly formatted invite link | ||
| }), | ||
| }) | ||
|
|
||
| const data = await response.json() | ||
|
|
||
| if (!response.ok) { | ||
| // Log backend error for debugging but show generic message to user | ||
| console.error('Backend claim error:', data.message || data.error) | ||
| throw new Error('Failed to claim QR code. Please try again.') | ||
| } | ||
|
|
||
| // Success! Show success page, then redirect to invite (which goes to profile for logged-in users) | ||
| router.push(`/qr/${code}/success`) | ||
| } catch (err: any) { | ||
| console.error('Error claiming QR:', err) | ||
| // Always show generic error message (don't expose backend details) | ||
| setError('Failed to claim QR code. Please try again.') | ||
| } finally { | ||
| setIsLoading(false) | ||
| } | ||
| }, [code, router, user]) | ||
|
|
||
| // Hold-to-claim mechanics with shake animation | ||
| const { holdProgress, isShaking, shakeIntensity, buttonProps } = useHoldToClaim({ | ||
| onComplete: handleClaim, | ||
| disabled: isLoading, | ||
| }) | ||
|
|
||
| // Show loading while checking status or if we're in the process of redirecting | ||
| if (isCheckingStatus || (redirectQrData?.claimed && redirectQrData?.redirectUrl)) { | ||
| return ( | ||
| <div className={`flex min-h-[inherit] flex-col gap-8 ${getShakeClass(isShaking, shakeIntensity)}`}> | ||
| <NavHeader title="Claim QR Code" /> | ||
| <div className="flex h-full items-center justify-center"> | ||
| <PeanutLoading /> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| // If not logged in and QR is unclaimed, the useEffect above will redirect to setup | ||
| // This loading screen will show briefly during that redirect | ||
| if (!user) { | ||
| return ( | ||
| <div className={`flex min-h-[inherit] flex-col gap-8 ${getShakeClass(isShaking, shakeIntensity)}`}> | ||
| <NavHeader title="Claim QR Code" /> | ||
| <div className="flex h-full items-center justify-center"> | ||
| <PeanutLoading /> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| // Show error only if there's an actual error or QR is not available (and not claimed) | ||
| if (redirectQrError || !redirectQrData || (!redirectQrData.available && !redirectQrData.claimed)) { | ||
| // Log error for debugging but don't expose details to user | ||
| if (redirectQrError) { | ||
| console.error('QR status check error:', redirectQrError) | ||
| } | ||
| return ( | ||
| <div className={`flex min-h-[inherit] flex-col gap-8 ${getShakeClass(isShaking, shakeIntensity)}`}> | ||
| <NavHeader title="Claim QR Code" /> | ||
| <div className="my-auto flex h-full flex-col justify-center space-y-4"> | ||
| <Card className="space-y-4 p-6"> | ||
| <div className="flex items-center justify-center"> | ||
| <div className="bg-red-100 flex h-16 w-16 items-center justify-center rounded-full"> | ||
| <Icon name="cancel" size={32} className="text-red-600" /> | ||
| </div> | ||
| </div> | ||
| <div className="space-y-2 text-center"> | ||
| <h1 className="text-2xl font-extrabold">QR Code Unavailable</h1> | ||
| <p className="text-base text-grey-1"> | ||
| {redirectQrData?.claimed | ||
| ? 'This QR code has already been claimed by another user.' | ||
| : 'This QR code is not available for claiming.'} | ||
| </p> | ||
| </div> | ||
| </Card> | ||
| <Button variant="purple" shadowSize="4" onClick={() => router.push('/home')} className="w-full"> | ||
| Go to Home | ||
| </Button> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| return ( | ||
| <div className={`flex min-h-[inherit] flex-col gap-8 ${getShakeClass(isShaking, shakeIntensity)}`}> | ||
| <NavHeader title="Claim Your QR Code" /> | ||
| <div className="my-auto flex h-full flex-col justify-center space-y-4"> | ||
| {/* QR Code Visual */} | ||
| <Card className="space-y-4 p-6"> | ||
| <div className="flex items-center justify-center"> | ||
| <div className="flex h-20 w-20 items-center justify-center rounded-full"> | ||
| <Icon name="qr-code" size={40} className="text-purple-600" /> | ||
| </div> | ||
| </div> | ||
| <div className="space-y-2 text-center"> | ||
| <h1 className="text-2xl font-extrabold">Claim Your Code</h1> | ||
| <p className="text-base text-grey-1"> | ||
| This QR code will be permanently linked to your Peanut profile. Anyone who scans it will be | ||
| able to use your invite. | ||
| </p> | ||
| </div> | ||
| </Card> | ||
|
|
||
| {/* How it works */} | ||
| <Card className="space-y-3 p-6"> | ||
| <h2 className="text-lg font-bold">How it works</h2> | ||
| <div className="space-y-3"> | ||
| <div className="flex gap-3"> | ||
| <div className="flex h-8 w-8 flex-shrink-0 items-center justify-center rounded-full bg-secondary-1 text-sm font-bold"> | ||
| 1 | ||
| </div> | ||
| <p className="text-sm text-grey-1">You claim this QR code and it becomes yours forever</p> | ||
| </div> | ||
| <div className="flex gap-3"> | ||
| <div className="flex h-8 w-8 flex-shrink-0 items-center justify-center rounded-full bg-secondary-1 text-sm font-bold"> | ||
| 2 | ||
| </div> | ||
| <p className="text-sm text-grey-1">Put the sticker anywhere you want people to find you</p> | ||
| </div> | ||
| <div className="flex gap-3"> | ||
| <div className="flex h-8 w-8 flex-shrink-0 items-center justify-center rounded-full bg-secondary-1 text-sm font-bold"> | ||
| 3 | ||
| </div> | ||
| <p className="text-sm text-grey-1"> | ||
| They can join Peanut with your invite and contribute towards your points, forever. | ||
| </p> | ||
| </div> | ||
| </div> | ||
| </Card> | ||
|
|
||
| {/* Important note */} | ||
| <Card className="border-2 border-secondary-1 bg-secondary-1/10 p-4"> | ||
| <div className="flex gap-3"> | ||
| <Icon name="info" size={20} className="flex-shrink-0 text-secondary-1" /> | ||
| <p className="text-sm font-medium"> | ||
| <strong>Important:</strong> Once claimed, this QR code cannot be transferred or changed. | ||
| </p> | ||
| </div> | ||
| </Card> | ||
|
|
||
| {/* Claim button - Hold to claim */} | ||
| <Button | ||
| {...buttonProps} | ||
| variant="purple" | ||
| shadowSize="4" | ||
| disabled={isLoading} | ||
| loading={isLoading} | ||
| className={`${buttonProps.className} w-full`} | ||
| > | ||
| {/* Black progress fill from left to right */} | ||
| <div | ||
| className="absolute inset-0 bg-black transition-all duration-100" | ||
| style={{ | ||
| width: `${holdProgress}%`, | ||
| left: 0, | ||
| }} | ||
| /> | ||
| <span className="relative z-10">{isLoading ? 'Claiming...' : 'Hold to Claim QR Code'}</span> | ||
| </Button> | ||
|
|
||
| {error && <ErrorAlert description={error} />} | ||
| </div> | ||
| </div> | ||
| ) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
version pinned this