Progress so far

This commit is contained in:
Tom
2023-12-30 10:56:40 +00:00
parent d191f8fb6f
commit 8eb9b9096f
41 changed files with 1984 additions and 6018 deletions

View File

@ -0,0 +1,42 @@
import { db } from "@/lib/db"
import fetchUserUsingAPI from "@/lib/validate-api";
import { NextResponse } from "next/server";
export async function GET(req: Request, { params } : { params: { id: string } }) {
try {
let id = req.headers?.get('x-api-key')
if (id == null) {
return NextResponse.json(null);
}
const tokens = await db.apiKey.findFirst({
where: {
id: id
}
});
return NextResponse.json(tokens);
} catch (error) {
console.log("[TOKEN/GET]", error);
return new NextResponse("Internal Error", { status: 500});
}
}
export async function DELETE(req: Request, { params } : { params: { id: string } }) {
try {
const { id } = params
const user = await fetchUserUsingAPI(req)
const token = await db.apiKey.delete({
where: {
id,
userId: user?.id
}
});
return NextResponse.json(token);
} catch (error) {
console.log("[TOKEN/DELETE]", error);
return new NextResponse("Internal Error", { status: 500});
}
}

View File

@ -0,0 +1,34 @@
import { db } from "@/lib/db"
import fetchUserUsingAPI from "@/lib/validate-api";
import { NextResponse } from "next/server";
export async function GET(req: Request) {
try {
const user = await fetchUserUsingAPI(req);
if (!user) {
return new NextResponse("Unauthorized", { status: 401 });
}
const api = await db.twitchConnection.findFirst({
where: {
userId: user.id
}
})
if (!api) {
return new NextResponse("Forbidden", { status: 403 });
}
const data = {
client_id: process.env.TWITCH_BOT_CLIENT_ID,
client_secret: process.env.TWITCH_BOT_CLIENT_SECRET,
access_token: api.accessToken,
refresh_token: api.refreshToken,
broadcaster_id: api.broadcasterId
}
return NextResponse.json(data);
} catch (error) {
console.log("[TOKENS/GET]", error);
return new NextResponse("Internal Error", { status: 500 });
}
}

63
app/api/token/route.ts Normal file
View File

@ -0,0 +1,63 @@
import fetchUserUsingAPI from "@/lib/validate-api";
import { db } from "@/lib/db"
import { NextResponse } from "next/server";
export async function POST(req: Request) {
try {
let { userId, label } = await req.json();
if (userId == null) {
const user = await fetchUserUsingAPI(req);
if (user != null) {
userId = user.id;
}
}
const id = generateToken()
const token = await db.apiKey.create({
data: {
id,
label,
userId: userId as string
}
});
return NextResponse.json(token);
} catch (error) {
console.log("[TOKEN/POST]", error);
return new NextResponse("Internal Error", { status: 500});
}
}
export async function DELETE(req: Request) {
try {
let { id } = await req.json();
const user = await fetchUserUsingAPI(req);
if (!id || !user) {
return NextResponse.json(null)
}
const token = await db.apiKey.delete({
where: {
id,
userId: user?.id
}
});
return NextResponse.json(token);
} catch (error) {
console.log("[TOKEN/DELETE]", error);
return new NextResponse("Internal Error", { status: 500});
}
}
export function generateToken() {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 32;
var randomstring = '';
for (var i = 0; i < string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars[rnum];
}
return randomstring;
}