Repository navigation
feat(billing): /upgrade naar Stripe + Mijn Ribba met portal-endpoint #32
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
Changes from all commits
3469e50
dbacbbe
fee605a
78fe2c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,136 @@ | ||||||
| 'use client'; | ||||||
|
|
||||||
| // Mijn Ribba — permanent klantportaal van Ribba (S4-ontwerp). Bewust | ||||||
| // minimaal: Ribba authenticeert, en "Abonnement & facturatie" opent per | ||||||
| // bezoek een VERSE Stripe Customer Portal-sessie via de Supabase edge | ||||||
| // function stripe-portal-session (B5: alle Stripe-secrets blijven in de | ||||||
| // projectbrede Supabase-set; hier alleen de user-JWT). Stripe blijft één | ||||||
| // onderdeel achter deze pagina; support en licenties blijven bij Ribba. | ||||||
|
|
||||||
| import { useEffect, useRef, useState } from 'react'; | ||||||
| import { useRouter } from 'next/navigation'; | ||||||
| import { createBrowserClient } from '@supabase/ssr'; | ||||||
| import RibbaLogo from '../components/RibbaLogo'; | ||||||
| import { createPortalGate, openStripePortal } from '@/lib/stripe-upgrade'; | ||||||
|
|
||||||
| const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL!; | ||||||
| const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!; | ||||||
|
|
||||||
| function getSupabaseBrowser() { | ||||||
| return createBrowserClient(supabaseUrl, supabaseAnonKey); | ||||||
| } | ||||||
|
|
||||||
| export default function MijnRibbaPage() { | ||||||
| const router = useRouter(); | ||||||
| const [loading, setLoading] = useState(true); | ||||||
| const [schoolId, setSchoolId] = useState<string | null>(null); | ||||||
| const [schoolName, setSchoolName] = useState<string | null>(null); | ||||||
| const [userEmail, setUserEmail] = useState<string | null>(null); | ||||||
| const [portalBusy, setPortalBusy] = useState(false); | ||||||
| const [error, setError] = useState<string | null>(null); | ||||||
| // Synchrone request-level dubbelklikblokkade (correctieronde 21 jul): | ||||||
| // portalBusy/disabled is UI-bescherming, maar twee click-events kunnen | ||||||
| // dezelfde render-state zien; deze ref-gate niet. | ||||||
| const portalGateRef = useRef(createPortalGate()); | ||||||
|
|
||||||
| useEffect(() => { | ||||||
| const supabase = getSupabaseBrowser(); | ||||||
| supabase.auth.getSession().then(async ({ data }) => { | ||||||
| if (!data.session) { | ||||||
| router.replace('/login'); | ||||||
| return; | ||||||
| } | ||||||
| setUserEmail(data.session.user.email ?? null); | ||||||
| try { | ||||||
| const meRes = await fetch('/api/me', { | ||||||
| headers: { Authorization: `Bearer ${data.session.access_token}` }, | ||||||
| }); | ||||||
| if (meRes.ok) { | ||||||
| const me = await meRes.json(); | ||||||
| if (me.school_id) setSchoolId(me.school_id); | ||||||
| if (me.school_name) setSchoolName(me.school_name); | ||||||
| } | ||||||
| } catch { | ||||||
| // foutafhandeling hieronder via ontbrekende schoolId | ||||||
| } finally { | ||||||
| setLoading(false); | ||||||
| } | ||||||
| }); | ||||||
| }, [router]); | ||||||
|
|
||||||
| const openPortal = async () => { | ||||||
| if (!schoolId) return; | ||||||
| // Lock vóór getSession/fetch; een tweede aanroep stopt hier synchroon. | ||||||
| if (!portalGateRef.current.begin()) return; | ||||||
| setPortalBusy(true); | ||||||
| setError(null); | ||||||
|
|
||||||
| try { | ||||||
| // Refresh-veilige token-fetch (zelfde patroon als /upgrade). | ||||||
| const supabase = getSupabaseBrowser(); | ||||||
| const { data: sessionData } = await supabase.auth.getSession(); | ||||||
| const token = sessionData.session?.access_token; | ||||||
| if (!token) { | ||||||
| router.replace('/login'); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| const result = await openStripePortal({ | ||||||
| supabaseUrl, | ||||||
| accessToken: token, | ||||||
| schoolId, | ||||||
| }); | ||||||
| if (!result.ok) { | ||||||
| setError(result.error); | ||||||
| setPortalBusy(false); | ||||||
| return; | ||||||
| } | ||||||
| // Succes: portalBusy blijft staan tot de navigatie de pagina verlaat; | ||||||
| // de gate komt in finally vrij, dus een bewuste nieuwe sessie kan. | ||||||
| window.location.href = result.url; | ||||||
| } finally { | ||||||
| portalGateRef.current.end(); | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| return ( | ||||||
| <main className="page-wrapper"> | ||||||
| <div className="card"> | ||||||
| <div className="logo"><RibbaLogo height={36} /></div> | ||||||
| <h1>Mijn Ribba</h1> | ||||||
|
|
||||||
| {loading ? ( | ||||||
| <p className="subtitle">Laden…</p> | ||||||
| ) : ( | ||||||
| <> | ||||||
| <p className="subtitle"> | ||||||
| {schoolName ? <strong>{schoolName}</strong> : 'Je rijschool'} | ||||||
| {userEmail ? <> · ingelogd als {userEmail}</> : null} | ||||||
| </p> | ||||||
|
|
||||||
| {error && <div className="checkout-error">{error}</div>} | ||||||
|
|
||||||
| <button | ||||||
| type="button" | ||||||
| className="btn-primary" | ||||||
| onClick={openPortal} | ||||||
| disabled={!schoolId || portalBusy} | ||||||
| > | ||||||
| {portalBusy ? 'Bezig…' : 'Abonnement & facturatie beheren'} | ||||||
| </button> | ||||||
| <p className="footer-text" style={{ marginTop: 12 }}> | ||||||
| Facturen downloaden, betaalmethode wijzigen of opzeggen — veilig via Stripe. | ||||||
| </p> | ||||||
|
|
||||||
| <div className="divider" /> | ||||||
|
|
||||||
| <p className="footer-text"> | ||||||
| Abonnement kiezen of wijzigen? <a href="/upgrade">Bekijk de plannen</a>.<br /> | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Use The raw ♻️ Suggested change-import { useRouter } from 'next/navigation';
+import { useRouter } from 'next/navigation';
+import Link from 'next/link';- Abonnement kiezen of wijzigen? <a href="/upgrade">Bekijk de plannen</a>.<br />
+ Abonnement kiezen of wijzigen? <Link href="/upgrade">Bekijk de plannen</Link>.<br />📝 Committable suggestion
Suggested change
🧰 Tools🪛 ESLint[error] 123-123: Do not use an ( 🤖 Prompt for AI AgentsSource: Linters/SAST tools |
||||||
| Vragen? <a href="mailto:hallo@ribba.app">hallo@ribba.app</a> | ||||||
| </p> | ||||||
| </> | ||||||
| )} | ||||||
| </div> | ||||||
| </main> | ||||||
| ); | ||||||
| } | ||||||
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.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
getSession()rejection leaves the page stuck on "Laden…".The
.then(...)chain has no rejection handler, andsetLoading(false)runs only inside the innertry/finally. IfgetSession()itself rejects (network/session error),loadingnever clears and the user sees an indefinite spinner. Add a.catchthat clears loading (and surfaces an error).🛡️ Suggested guard
📝 Committable suggestion
🤖 Prompt for AI Agents