diff --git a/setup.sh b/setup.sh index a9b5a99da8..544b505aa3 100755 --- a/setup.sh +++ b/setup.sh @@ -1,9 +1,22 @@ #!/bin/bash cd "$(dirname "$0")" || exit -echo -n "Enter your OpenAI Key (eg: sk...): " +is_valid_sk_key() { + local api_key=$1 + local pattern="^sk-[a-zA-Z0-9]{48}$" + [[ $api_key =~ $pattern ]] && return 0 || return 1 +} + +echo -n "Enter your OpenAI Key (eg: sk...) or press enter to continue with no key: " read OPENAI_API_KEY +if is_valid_sk_key $OPENAI_API_KEY || [ -z "$OPENAI_API_KEY" ]; then + echo "Valid API key" +else + echo "Invalid API key. Please ensure that you have billing set up on your OpenAI account" + exit +fi + NEXTAUTH_SECRET=$(openssl rand -base64 32) ENV="NODE_ENV=development\n\ @@ -24,4 +37,4 @@ else ./prisma/useSqlite.sh npm install npm run dev -fi \ No newline at end of file +fi diff --git a/src/components/SettingsDialog.tsx b/src/components/SettingsDialog.tsx index 39c717b1ff..114a51351a 100644 --- a/src/components/SettingsDialog.tsx +++ b/src/components/SettingsDialog.tsx @@ -45,9 +45,19 @@ export default function SettingsDialog({ close(); }; + function is_valid_key(key: string) { + const pattern = /^sk-[a-zA-Z0-9]{48}$/; + return pattern.test(key); + } + const handleSave = () => { - setCustomApiKey(key); - close(); + if (is_valid_key(key)) { + setCustomApiKey(key); + close(); + } + else { + alert("key is invalid, please ensure that you have set up billing in your OpenAI account") + } }; React.useEffect(() => {