generated from worldcoin/minikit-react-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from evgongora/fix/flow-errors
fix: flow errors and new features
- Loading branch information
Showing
12 changed files
with
494 additions
and
139 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
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,52 @@ | ||
import { getXataClient } from "@/lib/utils"; | ||
import { NextResponse } from "next/server"; | ||
import { getServerSession } from "next-auth"; | ||
import { cookies } from 'next/headers'; | ||
import { jwtVerify } from 'jose'; | ||
|
||
const JWT_SECRET = process.env.JWT_SECRET; | ||
if (!JWT_SECRET) { | ||
throw new Error('JWT_SECRET environment variable is required'); | ||
} | ||
|
||
const secret = new TextEncoder().encode(JWT_SECRET); | ||
|
||
export async function GET() { | ||
try { | ||
// Try NextAuth session first | ||
const session = await getServerSession(); | ||
const xata = getXataClient(); | ||
let user; | ||
|
||
if (session?.user?.email) { | ||
user = await xata.db.Users.filter({ email: session.user.email }).getFirst(); | ||
} else { | ||
// Try JWT session from wallet auth | ||
const token = cookies().get('session')?.value; | ||
|
||
if (token) { | ||
const { payload } = await jwtVerify(token, secret); | ||
if (payload.address) { | ||
user = await xata.db.Users.filter({ wallet_address: payload.address }).getFirst(); | ||
} | ||
} | ||
} | ||
|
||
if (!user) { | ||
return NextResponse.json({ error: "User not found" }, { status: 404 }); | ||
} | ||
|
||
return NextResponse.json({ | ||
user: { | ||
name: user.name, | ||
last_name: user.last_name, | ||
level: "Coming Soon", | ||
points: 45, | ||
maxPoints: 100 | ||
} | ||
}); | ||
} catch (error) { | ||
console.error('Error in home API route:', error); | ||
return NextResponse.json({ error: "Internal server error" }, { status: 500 }); | ||
} | ||
} |
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,79 @@ | ||
import { getXataClient } from "@/lib/utils"; | ||
import { NextResponse } from "next/server"; | ||
import { Question } from "@/app/types"; | ||
|
||
export async function GET( | ||
request: Request, | ||
{ params }: { params: { testId: string } } | ||
) { | ||
try { | ||
const testId = parseInt(params.testId); | ||
|
||
if (isNaN(testId) || testId <= 0) { | ||
return NextResponse.json( | ||
{ error: "Invalid test ID" }, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
const xata = getXataClient(); | ||
|
||
// First, verify the total questions in the test | ||
const test = await xata.db.Tests | ||
.filter({ test_id: testId }) | ||
.getFirst(); | ||
|
||
if (!test) { | ||
return NextResponse.json( | ||
{ error: "Test not found" }, | ||
{ status: 404 } | ||
); | ||
} | ||
|
||
console.log(`Expected questions for test ${testId}:`, test.total_questions); | ||
|
||
// Fetch questions with explicit pagination | ||
const questions = await xata.db.Questions | ||
.filter({ | ||
"test.test_id": testId | ||
}) | ||
.sort("sort_order", "asc") | ||
.getPaginated({ | ||
pagination: { | ||
size: 100 // Increase the limit to ensure we get all questions | ||
} | ||
}); | ||
|
||
console.log(`Actually fetched questions:`, questions.records.length); | ||
|
||
if (!questions.records || questions.records.length === 0) { | ||
return NextResponse.json( | ||
{ error: "No questions found for this test" }, | ||
{ status: 404 } | ||
); | ||
} | ||
|
||
// Transform the questions to match the expected format | ||
const formattedQuestions = questions.records.map(q => ({ | ||
id: q.question_id, | ||
question: q.question, | ||
effect: { | ||
econ: 0, // Add your actual effect values here | ||
dipl: 0, | ||
govt: 0, | ||
scty: 0 | ||
} | ||
})); | ||
|
||
return NextResponse.json({ | ||
questions: formattedQuestions | ||
}); | ||
|
||
} catch (error) { | ||
console.error("Error in questions API:", error); | ||
return NextResponse.json( | ||
{ error: "Internal server error" }, | ||
{ status: 500 } | ||
); | ||
} | ||
} |
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
Oops, something went wrong.