-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
456 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use server" | ||
|
||
import { getServerSession } from "next-auth" | ||
|
||
import { createApiKey } from "@/lib/apikey" | ||
import { authOptions } from "@/lib/auth" | ||
import { TApiKeyCreateInput } from "@/lib/types/apiKey" | ||
|
||
export async function createApiKeyAction( | ||
accountId: string, | ||
apiKeyData: TApiKeyCreateInput | ||
) { | ||
const session = await getServerSession(authOptions) | ||
if (!session) return | ||
|
||
return await createApiKey(accountId, apiKeyData) | ||
} |
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,40 @@ | ||
import { headers } from "next/headers" | ||
import { NextResponse } from "next/server" | ||
|
||
import { hashApiKey } from "@/lib/apikey" | ||
import { db } from "@/lib/db" | ||
|
||
export async function GET() { | ||
const headersList = headers() | ||
const apiKey = headersList.get("x-api-key") | ||
if (apiKey) { | ||
const apiKeyData = await db.apiKey.findUnique({ | ||
where: { | ||
hashedKey: hashApiKey(apiKey), | ||
}, | ||
select: { | ||
environment: { | ||
select: { | ||
id: true, | ||
createdAt: true, | ||
updatedAt: true, | ||
type: true, | ||
product: { | ||
select: { | ||
id: true, | ||
name: true, | ||
}, | ||
}, | ||
widgetSetupCompleted: true, | ||
}, | ||
}, | ||
}, | ||
}) | ||
if (!apiKeyData) { | ||
return new Response("Not authenticated", { | ||
status: 401, | ||
}) | ||
} | ||
return NextResponse.json(apiKeyData.environment) | ||
} | ||
} |
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
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,75 @@ | ||
import "server-only" | ||
|
||
import { createHash, randomBytes } from "crypto" | ||
|
||
import { db } from "@/lib/db" | ||
|
||
import { TApiKey, TApiKeyCreateInput } from "./types/apiKey" | ||
|
||
export const getApiKey = async (apiKeyId: string): Promise<TApiKey | null> => { | ||
try { | ||
const apiKeyData = await db.apiKey.findUnique({ | ||
where: { | ||
id: apiKeyId, | ||
}, | ||
}) | ||
|
||
return apiKeyData | ||
} catch (error) { | ||
throw error | ||
} | ||
} | ||
export const getApiKeys = async ( | ||
respositoryId: string, | ||
page?: number | ||
): Promise<TApiKey[]> => { | ||
try { | ||
const apiKeys = await db.apiKey.findMany({ | ||
where: { | ||
respositoryId, | ||
}, | ||
}) | ||
return apiKeys | ||
} catch (error) { | ||
throw error | ||
} | ||
} | ||
|
||
export const hashApiKey = (key: string): string => | ||
createHash("sha256").update(key).digest("hex") | ||
|
||
export async function createApiKey( | ||
respositoryId: string, | ||
apiKeyData: TApiKeyCreateInput | ||
): Promise<TApiKey> { | ||
try { | ||
const key = randomBytes(16).toString("hex") | ||
const hashedKey = hashApiKey(key) | ||
|
||
const result = await db.apiKey.create({ | ||
data: { | ||
...apiKeyData, | ||
hashedKey, | ||
respositoryId: { connect: { id: respositoryId } }, | ||
}, | ||
}) | ||
console.log({ ...result, apiKey: key }) | ||
return { ...result, apiKey: key } | ||
} catch (error) { | ||
throw error | ||
} | ||
} | ||
|
||
export const deleteApiKey = async (id: string): Promise<TApiKey | null> => { | ||
try { | ||
const deletedApiKeyData = await db.apiKey.delete({ | ||
where: { | ||
id: id, | ||
}, | ||
}) | ||
|
||
return deletedApiKeyData | ||
} catch (error) { | ||
throw error | ||
} | ||
} |
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,65 @@ | ||
import { db } from "@/lib/db" | ||
|
||
import { TRepository, TRepositoryCreateInput } from "./types/repository" | ||
|
||
export const getRepository = async ( | ||
repositoryId: string | ||
): Promise<TRepository | null> => { | ||
try { | ||
const repositoryData = await db.repository.findUnique({ | ||
where: { | ||
id: repositoryId, | ||
}, | ||
}) | ||
|
||
return repositoryData | ||
} catch (error) { | ||
throw error | ||
} | ||
} | ||
|
||
export const getRepositories = async ( | ||
ownerId: string | ||
): Promise<TRepository[]> => { | ||
try { | ||
const repositories = await db.repository.findMany({ | ||
where: { | ||
ownerId, | ||
}, | ||
}) | ||
|
||
return repositories | ||
} catch (error) { | ||
throw error | ||
} | ||
} | ||
|
||
export const createRepository = async ( | ||
repositoryData: TRepositoryCreateInput | ||
): Promise<TRepository> => { | ||
try { | ||
const newRepository = await db.repository.create({ | ||
data: repositoryData, | ||
}) | ||
|
||
return newRepository | ||
} catch (error) { | ||
throw error | ||
} | ||
} | ||
|
||
export const deleteRepository = async ( | ||
repositoryId: string | ||
): Promise<TRepository | null> => { | ||
try { | ||
const deletedRepository = await db.repository.delete({ | ||
where: { | ||
id: repositoryId, | ||
}, | ||
}) | ||
|
||
return deletedRepository | ||
} catch (error) { | ||
throw error | ||
} | ||
} |
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,37 @@ | ||
import { z } from "zod" | ||
|
||
import { ZApiKey } from "./apiKey" | ||
|
||
export const ZAccountInput = z.object({ | ||
userId: z.string(), | ||
type: z.string(), | ||
provider: z.string(), | ||
providerAccountId: z.string(), | ||
access_token: z.string().nullish(), | ||
refresh_token: z.string().nullish(), | ||
expires_at: z.number().nullish(), | ||
scope: z.string().nullish(), | ||
token_type: z.string().nullish(), | ||
id_token: z.string().nullish(), | ||
}) | ||
|
||
export type TAccountInput = z.infer<typeof ZAccountInput> | ||
|
||
export const ZAccount = z.object({ | ||
id: z.string(), | ||
createdAt: z.date(), | ||
updatedAt: z.date(), | ||
userId: z.string(), | ||
type: z.string(), | ||
provider: z.string(), | ||
providerAccountId: z.string(), | ||
access_token: z.string().nullable(), | ||
refresh_token: z.string().nullable().optional(), | ||
expires_at: z.number().nullable(), | ||
scope: z.string().nullable(), | ||
token_type: z.string().nullable(), | ||
id_token: z.string().nullable(), | ||
apiKeys: z.array(ZApiKey), | ||
}) | ||
|
||
export type TAccount = z.infer<typeof ZAccount> |
Oops, something went wrong.