mirror of
https://github.com/sqlchat/sqlchat.git
synced 2025-09-25 09:03:43 +08:00
feat: add auth api key header
This commit is contained in:
@ -1,5 +1,6 @@
|
|||||||
# Do not share your OpenAI API key with anyone! It should remain a secret.
|
# Do not share your OpenAI API key with anyone! It should remain a secret.
|
||||||
OPENAI_API_KEY=YOUR_API_KEY
|
OPENAI_API_KEY=YOUR_OPENAI_API_KEY
|
||||||
# Optional.
|
# Optional.
|
||||||
# OPENAI_API_ENDPOINT=YOUR_API_ENDPOINT
|
# OPENAI_API_ENDPOINT=YOUR_OPENAI_API_ENDPOINT
|
||||||
# USAGE_DATABASE_URL=postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=sqlchat_usage
|
# USAGE_DATABASE_URL=postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=sqlchat_usage
|
||||||
|
# API_KEY=YOUR_API_KEY
|
||||||
|
@ -2,6 +2,7 @@ import axios from "axios";
|
|||||||
import { first, head, last } from "lodash-es";
|
import { first, head, last } from "lodash-es";
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
import { toast } from "react-hot-toast";
|
import { toast } from "react-hot-toast";
|
||||||
|
import { API_KEY } from "@/env";
|
||||||
import {
|
import {
|
||||||
getAssistantById,
|
getAssistantById,
|
||||||
getPromptGeneratorOfAssistant,
|
getPromptGeneratorOfAssistant,
|
||||||
@ -171,12 +172,17 @@ const ConversationView = () => {
|
|||||||
content: prompt,
|
content: prompt,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const requestHeaders: any = {};
|
||||||
|
if (API_KEY) {
|
||||||
|
requestHeaders["Authorization"] = `Bearer ${API_KEY}`;
|
||||||
|
}
|
||||||
const rawRes = await fetch("/api/chat", {
|
const rawRes = await fetch("/api/chat", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
messages: formatedMessageList,
|
messages: formatedMessageList,
|
||||||
openAIApiConfig: settingStore.setting.openAIApiConfig,
|
openAIApiConfig: settingStore.setting.openAIApiConfig,
|
||||||
}),
|
}),
|
||||||
|
headers: requestHeaders,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!rawRes.ok) {
|
if (!rawRes.ok) {
|
||||||
|
2
src/env.ts
Normal file
2
src/env.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
// API_KEY is using to limit those authorized to use the API and protect the API endpoint.
|
||||||
|
export const API_KEY = process.env.API_KEY || "";
|
@ -1,5 +1,6 @@
|
|||||||
import { createParser, ParsedEvent, ReconnectInterval } from "eventsource-parser";
|
import { createParser, ParsedEvent, ReconnectInterval } from "eventsource-parser";
|
||||||
import { NextRequest } from "next/server";
|
import { NextRequest } from "next/server";
|
||||||
|
import { API_KEY } from "@/env";
|
||||||
import { openAIApiEndpoint, openAIApiKey } from "@/utils";
|
import { openAIApiEndpoint, openAIApiKey } from "@/utils";
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
@ -13,6 +14,15 @@ const getApiEndpoint = (apiEndpoint: string) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handler = async (req: NextRequest) => {
|
const handler = async (req: NextRequest) => {
|
||||||
|
if (API_KEY) {
|
||||||
|
const auth = req.headers.get("Authorization");
|
||||||
|
if (!auth || auth !== `Bearer ${API_KEY}`) {
|
||||||
|
return new Response("Unauthorized", {
|
||||||
|
status: 401,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const reqBody = await req.json();
|
const reqBody = await req.json();
|
||||||
const openAIApiConfig = reqBody.openAIApiConfig;
|
const openAIApiConfig = reqBody.openAIApiConfig;
|
||||||
const apiKey = openAIApiConfig?.key || openAIApiKey;
|
const apiKey = openAIApiConfig?.key || openAIApiKey;
|
||||||
|
Reference in New Issue
Block a user