feat: Add JWT authentication to backend, configure frontend API client, fix streak layout
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { useState, useEffect } from "react"
|
||||
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card"
|
||||
import { Button } from "@/components/ui/button"
|
||||
@@ -25,7 +26,7 @@ export default function CouponsAdmin() {
|
||||
|
||||
const fetchCoupons = async () => {
|
||||
try {
|
||||
const res = await fetch('/api/admin/coupons')
|
||||
const res = await fetchWithAuth('/api/admin/coupons')
|
||||
const data = await res.json()
|
||||
setCoupons(data)
|
||||
} catch (e) {
|
||||
@@ -39,7 +40,7 @@ export default function CouponsAdmin() {
|
||||
if (!newCoupon.code) return
|
||||
setSubmitting(true)
|
||||
try {
|
||||
await fetch('/api/admin/coupons', {
|
||||
await fetchWithAuth('/api/admin/coupons', {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(newCoupon)
|
||||
@@ -56,7 +57,7 @@ export default function CouponsAdmin() {
|
||||
const handleDelete = async (id: string) => {
|
||||
if (!confirm("Delete this coupon?")) return
|
||||
try {
|
||||
await fetch(`/api/admin/coupons/${id}`, { method: "DELETE" })
|
||||
await fetchWithAuth(`/api/admin/coupons/${id}`, { method: "DELETE" })
|
||||
await fetchCoupons()
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { useState, useEffect } from "react";
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
||||
@@ -15,7 +16,7 @@ export default function SettingsPage() {
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
fetch("/api/admin/settings")
|
||||
fetchWithAuth("/api/admin/settings")
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
if(data && !data.detail) {
|
||||
@@ -32,7 +33,7 @@ export default function SettingsPage() {
|
||||
const saveSettings = async (section: string, overrides: Partial<typeof settings> = {}) => {
|
||||
try {
|
||||
const payload = { ...settings, ...overrides };
|
||||
const res = await fetch("/api/admin/settings", {
|
||||
const res = await fetchWithAuth("/api/admin/settings", {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { useEffect, useState } from "react"
|
||||
import { useParams, useRouter } from "next/navigation"
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
|
||||
@@ -46,7 +47,7 @@ export default function StudentKundliPage() {
|
||||
const [editExam, setEditExam] = useState("")
|
||||
|
||||
useEffect(() => {
|
||||
fetch(`/api/admin/students/${studentId}`)
|
||||
fetchWithAuth(`/api/admin/students/${studentId}`)
|
||||
.then(r => { if (!r.ok) throw new Error("Not found"); return r.json() })
|
||||
.then(d => {
|
||||
setData(d)
|
||||
@@ -62,7 +63,7 @@ export default function StudentKundliPage() {
|
||||
const handleSave = async () => {
|
||||
setSaving(true)
|
||||
try {
|
||||
const res = await fetch(`/api/admin/students/${studentId}`, {
|
||||
const res = await fetchWithAuth(`/api/admin/students/${studentId}`, {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
@@ -74,7 +75,7 @@ export default function StudentKundliPage() {
|
||||
})
|
||||
if (!res.ok) throw new Error("Failed to save")
|
||||
// Refresh data
|
||||
const fresh = await fetch(`/api/admin/students/${studentId}`).then(r => r.json())
|
||||
const fresh = await fetchWithAuth(`/api/admin/students/${studentId}`).then(r => r.json())
|
||||
setData(fresh)
|
||||
setEditing(false)
|
||||
} catch (e: any) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { useEffect, useState } from "react"
|
||||
import { Badge } from "@/components/ui/badge"
|
||||
import { Button } from "@/components/ui/button"
|
||||
@@ -25,7 +26,7 @@ export default function StudentsPage() {
|
||||
const [search, setSearch] = useState("")
|
||||
|
||||
useEffect(() => {
|
||||
fetch("/api/admin/students")
|
||||
fetchWithAuth("/api/admin/students")
|
||||
.then(r => r.json())
|
||||
.then(data => setStudents(data))
|
||||
.catch(console.error)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { useState, useEffect, useRef } from "react"
|
||||
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card"
|
||||
import { Button } from "@/components/ui/button"
|
||||
@@ -55,7 +56,7 @@ export default function AdminTestsPage() {
|
||||
|
||||
const fetchTests = async () => {
|
||||
try {
|
||||
const res = await fetch("/api/admin/tests")
|
||||
const res = await fetchWithAuth("/api/admin/tests")
|
||||
if (res.ok) {
|
||||
const data = await res.json()
|
||||
setTests(data)
|
||||
@@ -74,7 +75,7 @@ export default function AdminTestsPage() {
|
||||
|
||||
const handleToggle = async (testId: string) => {
|
||||
try {
|
||||
const res = await fetch(`/api/admin/tests/${testId}/toggle`, {
|
||||
const res = await fetchWithAuth(`/api/admin/tests/${testId}/toggle`, {
|
||||
method: "PUT"
|
||||
})
|
||||
if (res.ok) {
|
||||
@@ -88,7 +89,7 @@ export default function AdminTestsPage() {
|
||||
const handleDelete = async (testId: string) => {
|
||||
if (!confirm("Are you sure you want to delete this test and all its questions? This action cannot be undone.")) return
|
||||
try {
|
||||
const res = await fetch(`/api/admin/tests/${testId}`, {
|
||||
const res = await fetchWithAuth(`/api/admin/tests/${testId}`, {
|
||||
method: "DELETE"
|
||||
})
|
||||
if (res.ok) {
|
||||
@@ -191,7 +192,7 @@ export default function AdminTestsPage() {
|
||||
};
|
||||
|
||||
try {
|
||||
const res = await fetch("/api/admin/tests/create_manual", {
|
||||
const res = await fetchWithAuth("/api/admin/tests/create_manual", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload)
|
||||
|
||||
@@ -121,20 +121,20 @@ export async function GET(req: NextRequest) {
|
||||
</div>
|
||||
|
||||
<div style={{ display: 'flex', flexDirection: 'column' }}>
|
||||
<div style={{ fontSize: 44, fontWeight: 900, color: textColor, letterSpacing: '-0.02em' }}>{name}</div>
|
||||
<div style={{ fontSize: 20, color: textSec, marginTop: 4 }}>Generated: {exportDate}</div>
|
||||
<div style={{ display: 'flex', fontSize: 44, fontWeight: 900, color: textColor, letterSpacing: '-0.02em' }}>{name}</div>
|
||||
<div style={{ display: 'flex', fontSize: 20, color: textSec, marginTop: 4 }}>{`Generated: ${exportDate}`}</div>
|
||||
<div style={{ display: 'flex', marginTop: 8 }}>
|
||||
<div style={{ fontSize: 22, color: '#06b6d4', fontWeight: 700, marginRight: 16 }}>{`${target} Aspirant`}</div>
|
||||
<div style={{ fontSize: 22, color: textSec, fontWeight: 600 }}>•</div>
|
||||
<div style={{ fontSize: 22, color: textTert, fontWeight: 600, marginLeft: 16 }}>{`${plan} Plan`}</div>
|
||||
<div style={{ display: 'flex', fontSize: 22, color: '#06b6d4', fontWeight: 700, marginRight: 16 }}>{`${target} Aspirant`}</div>
|
||||
<div style={{ display: 'flex', fontSize: 22, color: textSec, fontWeight: 600 }}>•</div>
|
||||
<div style={{ display: 'flex', fontSize: 22, color: textTert, fontWeight: 600, marginLeft: 16 }}>{`${plan} Plan`}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-end', gap: '4px' }}>
|
||||
<div style={{ fontSize: 22, color: textTert, fontWeight: 600 }}>pragativeer.com</div>
|
||||
<div style={{ fontSize: 22, color: textSec, fontWeight: 600 }}>contact@pragativeer.com</div>
|
||||
<div style={{ fontSize: 22, color: textSec, fontWeight: 600 }}>+91 9079337961</div>
|
||||
<div style={{ display: 'flex', fontSize: 22, color: textTert, fontWeight: 600 }}>pragativeer.com</div>
|
||||
<div style={{ display: 'flex', fontSize: 22, color: textSec, fontWeight: 600 }}>contact@pragativeer.com</div>
|
||||
<div style={{ display: 'flex', fontSize: 22, color: textSec, fontWeight: 600 }}>+91 9079337961</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Card, CardContent } from "@/components/ui/card"
|
||||
import { BrainCircuit, Play, CheckCircle2, Download, Filter, Lock, FileText, Eye, ShieldAlert, X, Sparkles, Share2, Maximize2, Minimize2 } from "lucide-react"
|
||||
@@ -25,14 +26,14 @@ export default function AIDailyPracticePage() {
|
||||
try {
|
||||
const pattern = user.target_exam || "JEE_Main";
|
||||
// Also fetch from the correct Next.js proxied route instead of localhost:8000
|
||||
const res = await fetch(`/api/tests?type=DPS&exam_pattern=${pattern}`);
|
||||
const res = await fetchWithAuth(`/api/tests?type=DPS&exam_pattern=${pattern}`);
|
||||
if (res.ok) {
|
||||
const data = await res.json()
|
||||
setDpps(data)
|
||||
}
|
||||
|
||||
if (userId) {
|
||||
const attemptsRes = await fetch(`/api/users/${userId}/attempts`);
|
||||
const attemptsRes = await fetchWithAuth(`/api/users/${userId}/attempts`);
|
||||
if (attemptsRes.ok) {
|
||||
const attemptsData = await attemptsRes.json();
|
||||
setAttempts(attemptsData);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { useEffect, useState } from "react"
|
||||
import { MistakeHeatmap } from "@/components/AI/MistakeHeatmap"
|
||||
import { StudyPlanCard } from "@/components/AI/StudyPlanCard"
|
||||
@@ -39,7 +40,7 @@ export default function AICoachDashboard() {
|
||||
|
||||
useEffect(() => {
|
||||
if (!userLoading && userId) {
|
||||
fetch(`/api/users/${userId}/stats`)
|
||||
fetchWithAuth(`/api/users/${userId}/stats`)
|
||||
.then(res => res.ok ? res.json() : null)
|
||||
.then(data => setStats(data))
|
||||
.catch(() => setStats(null))
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { useEffect, useState } from "react";
|
||||
import Link from "next/link";
|
||||
import { Button } from "@/components/ui/button";
|
||||
@@ -37,7 +38,7 @@ export default function CBTPage() {
|
||||
|
||||
async function loadUser() {
|
||||
try {
|
||||
const userRes = await fetch(`/api/users/${userId}`);
|
||||
const userRes = await fetchWithAuth(`/api/users/${userId}`);
|
||||
if (!userRes.ok) throw new Error("Failed to load user target exam");
|
||||
const userData = await userRes.json();
|
||||
const pattern = userData.target_exam;
|
||||
@@ -75,7 +76,7 @@ export default function CBTPage() {
|
||||
setExams(testsData);
|
||||
}
|
||||
|
||||
const attemptsRes = await fetch(`/api/users/${userId}/attempts`);
|
||||
const attemptsRes = await fetchWithAuth(`/api/users/${userId}/attempts`);
|
||||
if (attemptsRes.ok) {
|
||||
const attemptsData = await attemptsRes.json();
|
||||
setAttempts(attemptsData);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Card, CardContent } from "@/components/ui/card"
|
||||
import { BrainCircuit, Play, CheckCircle2, Download, Filter, Lock, FileText, Eye, ShieldAlert, X, Sparkles, Share2, Maximize2, Minimize2 } from "lucide-react"
|
||||
@@ -25,14 +26,14 @@ export default function DPPPage() {
|
||||
try {
|
||||
const pattern = user.target_exam || "JEE_Main";
|
||||
// Also fetch from the correct Next.js proxied route instead of localhost:8000
|
||||
const res = await fetch(`/api/tests?type=DPS&exam_pattern=${pattern}`);
|
||||
const res = await fetchWithAuth(`/api/tests?type=DPS&exam_pattern=${pattern}`);
|
||||
if (res.ok) {
|
||||
const data = await res.json()
|
||||
setDpps(data)
|
||||
}
|
||||
|
||||
if (userId) {
|
||||
const attemptsRes = await fetch(`/api/users/${userId}/attempts`);
|
||||
const attemptsRes = await fetchWithAuth(`/api/users/${userId}/attempts`);
|
||||
if (attemptsRes.ok) {
|
||||
const attemptsData = await attemptsRes.json();
|
||||
setAttempts(attemptsData);
|
||||
|
||||
@@ -335,7 +335,7 @@ export default function DashboardLayout({ children }: { children: ReactNode }) {
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => window.dispatchEvent(new CustomEvent("pragativeer-show-streak-animation"))}
|
||||
className="hover:opacity-85 transition-opacity cursor-pointer border-none bg-transparent p-0"
|
||||
className="flex items-center justify-center hover:opacity-85 transition-opacity cursor-pointer border-none bg-transparent p-0"
|
||||
title="View Streak Celebration"
|
||||
>
|
||||
<StreakCounter streak={user.streak_count} />
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { useRef, useState, useEffect } from "react"
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
||||
import { Badge } from "@/components/ui/badge"
|
||||
@@ -26,7 +27,7 @@ export default function DashboardPage() {
|
||||
}
|
||||
async function fetchStats() {
|
||||
try {
|
||||
const res = await fetch(`/api/users/${currentUserId}/stats`)
|
||||
const res = await fetchWithAuth(`/api/users/${currentUserId}/stats`)
|
||||
if (res.ok) {
|
||||
const data = await res.json()
|
||||
setUserStats(data)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { useEffect, useState } from "react";
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { Button } from "@/components/ui/button";
|
||||
@@ -50,7 +51,7 @@ export default function SettingsPage() {
|
||||
formData.append("file", file);
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/users/${userId}/avatar`, {
|
||||
const res = await fetchWithAuth(`/api/users/${userId}/avatar`, {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
});
|
||||
@@ -72,7 +73,7 @@ export default function SettingsPage() {
|
||||
if (!userId) return;
|
||||
setSaving(true);
|
||||
try {
|
||||
const res = await fetch(`/api/users/${userId}`, {
|
||||
const res = await fetchWithAuth(`/api/users/${userId}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { useState, useEffect } from "react"
|
||||
import { Card, CardContent } from "@/components/ui/card"
|
||||
import { Flame, Gift, Star, Trophy, Target, ArrowRight, CheckCircle2 } from "lucide-react"
|
||||
@@ -20,7 +21,7 @@ export default function StreaksPage() {
|
||||
}
|
||||
async function loadStats() {
|
||||
try {
|
||||
const res = await fetch(`/api/users/${user?.id}/stats`)
|
||||
const res = await fetchWithAuth(`/api/users/${user?.id}/stats`)
|
||||
if (res.ok) {
|
||||
const stats = await res.json()
|
||||
if (!stats.is_new_user) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client"
|
||||
"use client"
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
||||
import { ArrowLeft, Eye, EyeOff } from "lucide-react"
|
||||
@@ -34,14 +35,14 @@ export default function LoginPage() {
|
||||
const cookieVal = data.role === "admin" ? "admin" : (data.token || "authenticated");
|
||||
document.cookie = `auth_token=${cookieVal}; path=/; max-age=2592000; SameSite=Lax`;
|
||||
|
||||
localStorage.setItem("activeUserId", data.token);
|
||||
// Create new session token for single-device login
|
||||
const sessionToken = crypto.randomUUID();
|
||||
localStorage.setItem("activeUserId", data.user_id || data.token);
|
||||
// Store the JWT as the active session token
|
||||
const sessionToken = data.token;
|
||||
localStorage.setItem("active_session_token", sessionToken);
|
||||
|
||||
if (data.token && data.token !== "admin") {
|
||||
try {
|
||||
await fetch(`/api/users/${data.token}/session`, {
|
||||
await fetchWithAuth(`/api/users/${data.user_id}/session`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ session_token: sessionToken })
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
||||
import { ArrowLeft, Eye, EyeOff, CheckCircle2 } from "lucide-react"
|
||||
@@ -61,10 +62,10 @@ export default function SignupPage() {
|
||||
|
||||
if (loginData.success && typeof window !== "undefined") {
|
||||
document.cookie = `auth_token=${loginData.token}; path=/; max-age=2592000; SameSite=Lax`;
|
||||
localStorage.setItem("activeUserId", loginData.token);
|
||||
const sessionToken = crypto.randomUUID();
|
||||
localStorage.setItem("activeUserId", loginData.user_id || loginData.token);
|
||||
const sessionToken = loginData.token;
|
||||
localStorage.setItem("active_session_token", sessionToken);
|
||||
await fetch(`/api/users/${loginData.token}/session`, {
|
||||
await fetchWithAuth(`/api/users/${loginData.user_id}/session`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ session_token: sessionToken })
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { useState, useRef, useEffect } from "react"
|
||||
import { Card, CardContent, CardHeader, CardTitle, CardFooter } from "@/components/ui/card"
|
||||
import { Button } from "@/components/ui/button"
|
||||
@@ -66,7 +67,7 @@ export function AITutorChat() {
|
||||
try {
|
||||
const activeId = user?.id || (typeof window !== "undefined" ? localStorage.getItem("activeUserId") : null) || DEFAULT_USER_ID;
|
||||
|
||||
const res = await fetch('/api/ai-coach/chat/stream', {
|
||||
const res = await fetchWithAuth('/api/ai-coach/chat/stream', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ user_id: activeId, message: userText })
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { useEffect, useState } from "react"
|
||||
import { Card, CardContent } from "@/components/ui/card"
|
||||
import { RefreshCw, TrendingUp, MapPin, Star } from "lucide-react"
|
||||
@@ -61,7 +62,7 @@ export function CollegePredictorCard() {
|
||||
if (userLoading || !userId) return
|
||||
async function fetchData() {
|
||||
try {
|
||||
const res = await fetch(`/api/ai-coach/readiness/${userId}`)
|
||||
const res = await fetchWithAuth(`/api/ai-coach/readiness/${userId}`)
|
||||
if (!res.ok) throw new Error("fallback")
|
||||
} catch {
|
||||
// Use mock data — no-op
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { useEffect, useState } from "react"
|
||||
import { Card, CardContent } from "@/components/ui/card"
|
||||
import { TrendingUp, Flame, RefreshCw } from "lucide-react"
|
||||
@@ -18,7 +19,7 @@ export function ReadinessGauge() {
|
||||
|
||||
async function fetchReadiness() {
|
||||
try {
|
||||
const res = await fetch(`/api/ai-coach/readiness/${userId}`)
|
||||
const res = await fetchWithAuth(`/api/ai-coach/readiness/${userId}`)
|
||||
if (!res.ok) throw new Error("Failed to fetch")
|
||||
const json = await res.json()
|
||||
setData(json)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { useEffect, useState } from "react"
|
||||
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card"
|
||||
import { Button } from "@/components/ui/button"
|
||||
@@ -16,7 +17,7 @@ export function ReferralCard() {
|
||||
if (userLoading || !userId) return;
|
||||
async function fetchReferral() {
|
||||
try {
|
||||
const res = await fetch(`/api/growth/referral/${userId}`)
|
||||
const res = await fetchWithAuth(`/api/growth/referral/${userId}`)
|
||||
if (res.ok) {
|
||||
const json = await res.json()
|
||||
setData(json)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { useEffect, useState } from "react"
|
||||
import { Card, CardContent } from "@/components/ui/card"
|
||||
import { Check, RefreshCw } from "lucide-react"
|
||||
@@ -17,7 +18,7 @@ export function StudyPlanCard() {
|
||||
|
||||
async function fetchPlan() {
|
||||
try {
|
||||
const res = await fetch(`/api/ai-coach/study-plan/today?user_id=${userId}`)
|
||||
const res = await fetchWithAuth(`/api/ai-coach/study-plan/today?user_id=${userId}`)
|
||||
if (!res.ok) {
|
||||
// If no plan is generated yet, fallback silently
|
||||
throw new Error("silent_fallback")
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { useState, useRef, useEffect } from "react"
|
||||
import { Card, CardContent } from "@/components/ui/card"
|
||||
import { Send, Loader2, BrainCircuit, Sparkles } from "lucide-react"
|
||||
@@ -49,7 +50,7 @@ export function TutorChat() {
|
||||
setStreaming(true)
|
||||
|
||||
try {
|
||||
const res = await fetch("/api/ai-coach/chat/stream", {
|
||||
const res = await fetchWithAuth("/api/ai-coach/chat/stream", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ user_id: userId, message: text.trim() })
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState, useCallback, useRef } from "react";
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
|
||||
export interface UserProfile {
|
||||
id: string;
|
||||
@@ -110,7 +111,7 @@ async function fetchSettings(): Promise<any> {
|
||||
if (cachedSettings && now < settingsCacheExpiry) return cachedSettings;
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/admin/settings`, { cache: "no-store" });
|
||||
const res = await fetchWithAuth(`/api/admin/settings`, { cache: "no-store" });
|
||||
if (res.ok) {
|
||||
cachedSettings = await res.json();
|
||||
settingsCacheExpiry = now + 60_000; // cache for 60 seconds
|
||||
@@ -147,7 +148,7 @@ export function useActiveUser() {
|
||||
try {
|
||||
// ── PERF FIX: run both fetches in PARALLEL instead of sequentially ──
|
||||
const [res, settingsData] = await Promise.all([
|
||||
fetch(`/api/users/${id}`),
|
||||
fetchWithAuth(`/api/users/${id}`),
|
||||
fetchSettings(),
|
||||
]);
|
||||
|
||||
@@ -226,48 +227,49 @@ export function useActiveUser() {
|
||||
if (typeof window === "undefined" || newId === activeUserIdRef.current) return;
|
||||
|
||||
setSwitching(true);
|
||||
|
||||
// ── Update cookie + localStorage immediately (optimistic) ───────────────
|
||||
document.cookie = `auth_token=${newId}; path=/; max-age=2592000; SameSite=Lax`;
|
||||
const sessionToken = crypto.randomUUID();
|
||||
localStorage.setItem("active_session_token", sessionToken);
|
||||
localStorage.setItem("activeUserId", newId);
|
||||
|
||||
// Update refs and state before awaiting network
|
||||
activeUserIdRef.current = newId;
|
||||
setUserIdState(newId);
|
||||
setUser(null); // Clear old user immediately so stale data isn't shown
|
||||
|
||||
// Notify listeners immediately of account switch
|
||||
if (typeof window !== "undefined") {
|
||||
window.dispatchEvent(new CustomEvent("pragativeer-user-switched", { detail: { userId: newId } }));
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
// ── Run session update + new user profile fetch IN PARALLEL ─────────
|
||||
const [_, newUserRes] = await Promise.all([
|
||||
// Fire-and-forget session token update
|
||||
fetch(`/api/users/${newId}/session`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ session_token: sessionToken }),
|
||||
}).catch(() => {}), // ignore failure — non-critical
|
||||
// Fetch the new user's profile immediately
|
||||
fetch(`/api/users/${newId}`),
|
||||
]);
|
||||
// First, get the JWT token for this demo user by calling the switch API
|
||||
const switchRes = await fetch(`/api/user/switch?id=${newId}`);
|
||||
const switchData = await switchRes.json();
|
||||
|
||||
if (!switchRes.ok || !switchData.token) {
|
||||
throw new Error("Switch failed");
|
||||
}
|
||||
|
||||
const sessionToken = switchData.token; // This is the JWT
|
||||
const actualUserId = switchData.user_id;
|
||||
|
||||
// ── Update cookie + localStorage immediately (optimistic) ───────────────
|
||||
document.cookie = `auth_token=${sessionToken}; path=/; max-age=2592000; SameSite=Lax`;
|
||||
localStorage.setItem("active_session_token", sessionToken);
|
||||
localStorage.setItem("activeUserId", actualUserId);
|
||||
|
||||
// Update refs and state before awaiting network
|
||||
activeUserIdRef.current = actualUserId;
|
||||
setUserIdState(actualUserId);
|
||||
setUser(null); // Clear old user immediately so stale data isn't shown
|
||||
|
||||
// Notify listeners immediately of account switch
|
||||
if (typeof window !== "undefined") {
|
||||
window.dispatchEvent(new CustomEvent("pragativeer-user-switched", { detail: { userId: actualUserId } }));
|
||||
}
|
||||
|
||||
// Fetch the new user's profile immediately using the new token
|
||||
const newUserRes = await fetchWithAuth(`/api/users/${actualUserId}`);
|
||||
|
||||
if (newUserRes && newUserRes.ok) {
|
||||
const data = await newUserRes.json();
|
||||
setUser(data);
|
||||
if (typeof window !== "undefined") {
|
||||
window.dispatchEvent(new CustomEvent("pragativeer-user-switched", { detail: { userId: newId, user: data } }));
|
||||
window.dispatchEvent(new CustomEvent("pragativeer-user-switched", { detail: { userId: actualUserId, user: data } }));
|
||||
}
|
||||
} else {
|
||||
// Fallback: use the regular fetch path
|
||||
await fetchUserProfile(newId);
|
||||
await fetchUserProfile(actualUserId);
|
||||
}
|
||||
} catch {
|
||||
// Network error — fall back to full fetch
|
||||
} catch (err) {
|
||||
// Network error
|
||||
console.error("Failed to switch user", err);
|
||||
await fetchUserProfile(newId);
|
||||
} finally {
|
||||
setSwitching(false);
|
||||
|
||||
27
src/lib/apiClient.ts
Normal file
27
src/lib/apiClient.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
export async function fetchWithAuth(url: string, options: RequestInit = {}): Promise<Response> {
|
||||
const isServer = typeof window === 'undefined';
|
||||
|
||||
if (isServer) {
|
||||
// If we're on the server-side, we should pass along headers from next/headers
|
||||
// or just let it fall back to standard fetch if no auth is needed for server-side fetches.
|
||||
// For this app, most data fetching seems to be client-side.
|
||||
return fetch(url, options);
|
||||
}
|
||||
|
||||
// Get token from localStorage
|
||||
const token = localStorage.getItem('active_session_token');
|
||||
|
||||
// Prepare headers
|
||||
const headers = new Headers(options.headers || {});
|
||||
|
||||
// Apply token if it exists
|
||||
if (token) {
|
||||
headers.set('Authorization', `Bearer ${token}`);
|
||||
}
|
||||
|
||||
// Perform fetch with updated headers
|
||||
return fetch(url, {
|
||||
...options,
|
||||
headers,
|
||||
});
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { create } from 'zustand'
|
||||
import { persist, StateStorage, createJSONStorage } from 'zustand/middleware'
|
||||
import { get, set, del } from 'idb-keyval'
|
||||
@@ -252,7 +253,7 @@ export const useCBTStore = create<CBTState>()(
|
||||
if (!payload) return null;
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/attempts/submit`, {
|
||||
const res = await fetchWithAuth(`/api/attempts/submit`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
|
||||
Reference in New Issue
Block a user