hermes-web/app/api/account/redemptions/route.ts

39 lines
1.6 KiB
TypeScript
Raw Permalink Normal View History

import { NextResponse } from "next/server";
import fetchUserWithImpersonation from '@/lib/fetch-user-impersonation';
import axios from "axios";
import { updateTwitchToken } from "@/data/twitch-reauthorize";
export async function GET(req: Request) {
try {
if (!process.env.TWITCH_BOT_CLIENT_ID)
2024-08-25 17:35:46 -04:00
return NextResponse.json({ message: 'Something went wrong.', error: null, value: null }, { status: 500 })
const user = await fetchUserWithImpersonation(req)
if (!user) {
2024-08-25 17:35:46 -04:00
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
}
const auth = await updateTwitchToken(user.id)
if (!auth)
2024-08-25 17:35:46 -04:00
return NextResponse.json({ message: 'Failed to authorize to Twitch.', error: null, value: null }, { status: 403 });
try {
const redemptions = await axios.get("https://api.twitch.tv/helix/channel_points/custom_rewards?broadcaster_id=" + auth.broadcaster_id,
{
headers: {
"Client-Id": process.env.TWITCH_BOT_CLIENT_ID,
"Authorization": "Bearer " + auth.access_token
}
}
)
return NextResponse.json(redemptions.data);
} catch (error: any) {
console.error('Fetching Twitch channel redemptions:', error.response.data)
}
return NextResponse.json([]);
} catch (error) {
console.log("[REDEMPTIONS/ACTIONS]", error);
2024-08-25 17:35:46 -04:00
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
}
}