mirror of
https://github.com/neynarxyz/create-farcaster-mini-app.git
synced 2025-12-07 18:02:33 -05:00
Cleanup
This commit is contained in:
99
src/app/api/auth/session-signers/route.ts
Normal file
99
src/app/api/auth/session-signers/route.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getServerSession } from 'next-auth';
|
||||
import { authOptions } from '~/auth';
|
||||
import { getNeynarClient } from '~/lib/neynar';
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
if (!session?.user?.fid) {
|
||||
return NextResponse.json(
|
||||
{ error: 'No authenticated session found' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const message = searchParams.get('message');
|
||||
const signature = searchParams.get('signature');
|
||||
|
||||
if (!message || !signature) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Message and signature are required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const client = getNeynarClient();
|
||||
const data = await client.fetchSigners({ message, signature });
|
||||
const signers = data.signers;
|
||||
|
||||
// Fetch user data if signers exist
|
||||
let user = null;
|
||||
if (signers && signers.length > 0) {
|
||||
try {
|
||||
const userResponse = await fetch(
|
||||
`${process.env.NEXTAUTH_URL}/api/users?fids=${signers[0].fid}`
|
||||
);
|
||||
if (userResponse.ok) {
|
||||
const userDataResponse = await userResponse.json();
|
||||
user = userDataResponse.users?.[0] || null;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching user data:', error);
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
signers,
|
||||
user,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error in session-signers API:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch signers' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
if (!session?.user?.fid) {
|
||||
return NextResponse.json(
|
||||
{ error: 'No authenticated session found' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { message, signature, signers, user } = body;
|
||||
|
||||
if (!message || !signature || !signers) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Message, signature, and signers are required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Since we can't directly modify the session token here,
|
||||
// we'll return the data and let the client trigger a session update
|
||||
// The client will need to call getSession() to refresh the session
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Session data prepared for update',
|
||||
signers,
|
||||
user,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error updating session signers:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to update session' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
46
src/app/api/auth/update-session/route.ts
Normal file
46
src/app/api/auth/update-session/route.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getServerSession } from 'next-auth';
|
||||
import { authOptions } from '~/auth';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
if (!session?.user?.fid) {
|
||||
return NextResponse.json(
|
||||
{ error: 'No authenticated session found' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { signers, user } = body;
|
||||
|
||||
if (!signers || !user) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Signers and user are required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// For NextAuth to update the session, we need to trigger the JWT callback
|
||||
// This is typically done by calling the session endpoint with updated data
|
||||
// However, we can't directly modify the session token from here
|
||||
|
||||
// Instead, we'll store the data temporarily and let the client refresh the session
|
||||
// The session will be updated when the JWT callback is triggered
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Session update prepared',
|
||||
signers,
|
||||
user,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error preparing session update:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to prepare session update' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user