Updated list of commands to v4.3. Added groups & permissions. Added connections. Updated redemptions and actions to v4.3.
This commit is contained in:
92
app/api/connection/authorize/route.ts
Normal file
92
app/api/connection/authorize/route.ts
Normal file
@ -0,0 +1,92 @@
|
||||
import { db } from "@/lib/db"
|
||||
import { NextResponse } from "next/server";
|
||||
import fetchUserWithImpersonation from '@/lib/fetch-user-impersonation';
|
||||
import axios from "axios";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
const user = await fetchUserWithImpersonation(req);
|
||||
if (!user)
|
||||
return NextResponse.json({ error: null, message: "Unauthorized", success: false }, { status: 401 });
|
||||
|
||||
let { access_token, expires_in, token_type, scope, state } = await req.json();
|
||||
|
||||
if (!token_type)
|
||||
return NextResponse.json({ error: null, message: 'No token type given for the authorization.', success: false }, { status: 400 })
|
||||
|
||||
if (!access_token)
|
||||
return NextResponse.json({ error: null, message: 'No access token given for the authorization.', success: false }, { status: 400 })
|
||||
|
||||
if (!scope)
|
||||
return NextResponse.json({ error: null, message: 'No scope given for the authorization.', success: false }, { status: 400 })
|
||||
|
||||
if (!state)
|
||||
return NextResponse.json({ error: null, message: 'No state given for the authorization.', success: false }, { status: 400 })
|
||||
|
||||
// Fetch connection state data
|
||||
const info = await db.connectionState.findUnique({
|
||||
where: {
|
||||
state: state
|
||||
}
|
||||
})
|
||||
if (!info)
|
||||
return NextResponse.json({ error: null, message: 'No authorization code was received previously.', success: false }, { status: 400 })
|
||||
|
||||
if (info.type == "twitch") {
|
||||
const response = await axios.get("https://id.twitch.tv/oauth2/validate", {
|
||||
headers: {
|
||||
Authorization: 'OAuth ' + access_token
|
||||
}
|
||||
})
|
||||
expires_in = response.data.expires_in
|
||||
}
|
||||
if (!expires_in)
|
||||
return NextResponse.json({ error: null, message: 'No expiration given for the authorization.', success: false }, { status: 400 })
|
||||
|
||||
let expiration = new Date()
|
||||
expiration.setSeconds(expiration.getSeconds() + parseInt(expires_in) - 600);
|
||||
|
||||
await db.connection.upsert({
|
||||
where: {
|
||||
userId_name: {
|
||||
userId: info.userId,
|
||||
name: info.name
|
||||
}
|
||||
},
|
||||
create: {
|
||||
userId: info.userId,
|
||||
name: info.name,
|
||||
type: info.type,
|
||||
clientId: info.clientId,
|
||||
accessToken: access_token,
|
||||
scope,
|
||||
grantType: token_type,
|
||||
expiresAt: expiration
|
||||
},
|
||||
update: {
|
||||
clientId: info.clientId,
|
||||
accessToken: access_token,
|
||||
scope,
|
||||
grantType: token_type,
|
||||
expiresAt: expiration
|
||||
}
|
||||
})
|
||||
|
||||
await db.connectionState.delete({
|
||||
where: {
|
||||
userId_name: {
|
||||
userId: user.id,
|
||||
name: info.name
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({ error: null, message: "", success: true }, { status: 200 });
|
||||
} catch (error: any) {
|
||||
if (error.name == 'PrismaClientKnownRequestError') {
|
||||
if (error.code == 'P2002')
|
||||
return NextResponse.json({ error, message: "Connection already saved.", success: false }, { status: 500 });
|
||||
}
|
||||
return NextResponse.json({ error, message: "Failed to save connection", success: false }, { status: 500 });
|
||||
}
|
||||
}
|
75
app/api/connection/default/route.ts
Normal file
75
app/api/connection/default/route.ts
Normal file
@ -0,0 +1,75 @@
|
||||
import { db } from "@/lib/db"
|
||||
import { NextResponse } from "next/server";
|
||||
import fetchUserWithImpersonation from '@/lib/fetch-user-impersonation';
|
||||
|
||||
|
||||
export async function GET(req: Request) {
|
||||
try {
|
||||
const user = await fetchUserWithImpersonation(req);
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: null, message: "Unauthorized", success: false }, { status: 401 });
|
||||
}
|
||||
|
||||
const data = await db.connection.findMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
default: true
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({ error: null, message: "", success: true, data }, { status: 200 });
|
||||
} catch (error: any) {
|
||||
return NextResponse.json({ error, message: "Failed to get default connection", success: false }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(req: Request) {
|
||||
try {
|
||||
const user = await fetchUserWithImpersonation(req);
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: null, message: "Unauthorized", success: false }, { status: 401 });
|
||||
}
|
||||
|
||||
const { name } = await req.json();
|
||||
if (!name)
|
||||
return NextResponse.json({ error: null, message: 'Requires "name" param to be passed in - name of the connection.', success: false }, { status: 400 })
|
||||
|
||||
const connection = await db.connection.findFirst({
|
||||
where: {
|
||||
userId: user.id,
|
||||
name
|
||||
}
|
||||
})
|
||||
|
||||
if (!connection) {
|
||||
return NextResponse.json({ error: null, message: 'Connection with that name does not exist.', success: false }, { status: 400 })
|
||||
}
|
||||
|
||||
await db.connection.updateMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
type: connection.type,
|
||||
default: true
|
||||
},
|
||||
data: {
|
||||
default: false
|
||||
}
|
||||
})
|
||||
|
||||
const data = await db.connection.update({
|
||||
where: {
|
||||
userId_name: {
|
||||
userId: user.id,
|
||||
name,
|
||||
}
|
||||
},
|
||||
data: {
|
||||
default: true as boolean
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({ error: null, message: "", success: true, data }, { status: 200 });
|
||||
} catch (error: any) {
|
||||
return NextResponse.json({ error, message: "Failed to update default connection", success: false }, { status: 500 });
|
||||
}
|
||||
}
|
59
app/api/connection/prepare/route.ts
Normal file
59
app/api/connection/prepare/route.ts
Normal file
@ -0,0 +1,59 @@
|
||||
import { db } from "@/lib/db"
|
||||
import { NextResponse } from "next/server";
|
||||
import fetchUserWithImpersonation from '@/lib/fetch-user-impersonation';
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
const user = await fetchUserWithImpersonation(req);
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: null, message: "Unauthorized", success: false }, { status: 401 });
|
||||
}
|
||||
|
||||
let { state, name, type, grantType, clientId } = await req.json();
|
||||
|
||||
if (!clientId)
|
||||
return NextResponse.json({ error: null, message: 'No client id given for the authorization.', success: false }, { status: 400 })
|
||||
|
||||
if (!type)
|
||||
return NextResponse.json({ error: null, message: 'No type given for the authorization.', success: false }, { status: 400 })
|
||||
|
||||
if (!state)
|
||||
return NextResponse.json({ error: null, message: 'No state given for the authorization.', success: false }, { status: 400 })
|
||||
|
||||
if (!name)
|
||||
return NextResponse.json({ error: null, message: 'No name given for the authorization.', success: false }, { status: 400 })
|
||||
|
||||
if (!grantType)
|
||||
return NextResponse.json({ error: null, message: 'No grant type given for the authorization.', success: false }, { status: 400 })
|
||||
|
||||
await db.connectionState.upsert({
|
||||
where: {
|
||||
userId_name: {
|
||||
userId: user.id,
|
||||
name
|
||||
}
|
||||
},
|
||||
create: {
|
||||
userId: user.id,
|
||||
name,
|
||||
type,
|
||||
state,
|
||||
grantType,
|
||||
clientId
|
||||
},
|
||||
update: {
|
||||
name,
|
||||
state,
|
||||
grantType,
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({ error: null, message: "", success: true }, { status: 200 });
|
||||
} catch (error: any) {
|
||||
if (error.name == 'PrismaClientKnownRequestError') {
|
||||
if (error.code == 'P2002')
|
||||
return NextResponse.json({ error, message: "Connection already prepared.", success: false }, { status: 500 });
|
||||
}
|
||||
return NextResponse.json({ error, message: "Failed to prepare connection", success: false }, { status: 500 });
|
||||
}
|
||||
}
|
74
app/api/connection/route.ts
Normal file
74
app/api/connection/route.ts
Normal file
@ -0,0 +1,74 @@
|
||||
import { db } from "@/lib/db"
|
||||
import { NextResponse } from "next/server";
|
||||
import fetchUserWithImpersonation from '@/lib/fetch-user-impersonation';
|
||||
|
||||
|
||||
export async function GET(req: Request) {
|
||||
try {
|
||||
const user = await fetchUserWithImpersonation(req);
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: null, message: "Unauthorized", success: false }, { status: 401 });
|
||||
}
|
||||
|
||||
//const { searchParams } = new URL(req.url)
|
||||
const data = await db.connection.findMany({
|
||||
where: {
|
||||
userId: user.id
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({ error: null, message: "", success: true, data }, { status: 200 });
|
||||
} catch (error: any) {
|
||||
return NextResponse.json({ error, message: "Failed to fetch connections", success: false }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(req: Request) {
|
||||
try {
|
||||
const user = await fetchUserWithImpersonation(req);
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: null, message: "Unauthorized", success: false }, { status: 401 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(req.url)
|
||||
const name = searchParams.get('name') as string
|
||||
|
||||
if (!name)
|
||||
return NextResponse.json({ error: null, message: 'Requires "name" param to be passed in - name of the service.', success: false }, { status: 400 })
|
||||
|
||||
const data = await db.connection.delete({
|
||||
where: {
|
||||
userId_name: {
|
||||
userId: user.id,
|
||||
name: name
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const connections = await db.connection.findMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
type: data.type
|
||||
}
|
||||
})
|
||||
|
||||
if (connections.length > 0 && connections.every(c => !c.default)) {
|
||||
const connectionName = connections[0].name
|
||||
await db.connection.update({
|
||||
where: {
|
||||
userId_name: {
|
||||
userId: user.id,
|
||||
name: connectionName
|
||||
}
|
||||
},
|
||||
data: {
|
||||
default: true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return NextResponse.json({ error: null, message: "", success: true, data }, { status: 200 });
|
||||
} catch (error: any) {
|
||||
return NextResponse.json({ error, message: "Failed to delete connection", success: false }, { status: 500 });
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user