Skip to content

Commit bbee80b

Browse files
authored
Merge pull request #158 from Dogtiti/feature/auth-script
feat: add auth bash
2 parents c923950 + 5450466 commit bbee80b

File tree

1 file changed

+203
-0
lines changed

1 file changed

+203
-0
lines changed

setup-auth.sh

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
#!/bin/bash
2+
3+
# Color escape sequences
4+
RED='\033[0;31m'
5+
GREEN='\033[0;32m'
6+
NC='\033[0m' # No Color
7+
8+
# Check if we are running in a terminal that supports colors
9+
if [ -t 1 ]; then
10+
# Use colors
11+
RED=$(printf '\033[31m')
12+
GREEN=$(printf '\033[32m')
13+
NC=$(printf '\033[0m')
14+
fi
15+
16+
# Check if a string matches the "sk-" key pattern
17+
is_valid_sk_key() {
18+
local api_key=$1
19+
local pattern="^sk-[a-zA-Z0-9]{48}$"
20+
[[ $api_key =~ $pattern ]] && return 0 || return 1
21+
}
22+
23+
# Set value for NEXT_PUBLIC_WEB_SEARCH_ENABLED
24+
select_web_search_enabled() {
25+
PS3="${GREEN}Do you want to enable web search?${NC} "
26+
options=("true" "false")
27+
select opt in "${options[@]}"; do
28+
case $opt in
29+
"true")
30+
NEXT_PUBLIC_WEB_SEARCH_ENABLED=true
31+
read_variable "${GREEN}Enter your SERP API Key (required):${NC} " "^.+$" "SERP_API_KEY"
32+
break
33+
;;
34+
"false")
35+
NEXT_PUBLIC_WEB_SEARCH_ENABLED=false
36+
break
37+
;;
38+
*) echo "${RED}Please enter a valid option.${NC}" ;;
39+
esac
40+
done
41+
}
42+
43+
# Ask for user input and validate variable values
44+
read_variable() {
45+
local prompt="$1"
46+
local pattern="$2"
47+
local var_name="$3"
48+
local var_val=""
49+
while true; do
50+
read -p "$prompt" var_val
51+
if [[ -z "$var_val" ]]; then
52+
echo -e "${RED}Error: Please enter a valid value for $var_name.${NC}"
53+
elif [[ ! $var_val =~ $pattern ]]; then
54+
echo -e "${RED}Error: Invalid format for $var_name.${NC}"
55+
else
56+
eval "$var_name=$var_val"
57+
echo -e "${GREEN}$var_name set. ✔${NC}"
58+
break
59+
fi
60+
done
61+
}
62+
63+
# Get user input for OPENAI_API_KEY
64+
read_variable "${GREEN}Enter your OpenAI API Key (required):${NC} " "^sk-[a-zA-Z0-9]{48}$" "OPENAI_API_KEY"
65+
66+
# Get user input for DATABASE_URL
67+
read_variable "${GREEN}Enter your database URL (required):${NC} " "^.+$" "DATABASE_URL"
68+
69+
# Get user input for GITHUB_CLIENT_ID
70+
read_variable "${GREEN}Enter your Github Client ID (required):${NC} " "^.+$" "GITHUB_CLIENT_ID"
71+
72+
# Get user input for GITHUB_CLIENT_SECRET
73+
read_variable "${GREEN}Enter your Github Client Secret (required):${NC} " "^.+$" "GITHUB_CLIENT_SECRET"
74+
75+
# Get user input for NEXT_PUBLIC_WEB_SEARCH_ENABLED
76+
select_web_search_enabled
77+
78+
echo -e "${GREEN}All required variables set. ✔${NC}"
79+
80+
# Generate a random string for NEXTAUTH_SECRET
81+
NEXTAUTH_SECRET=$(openssl rand -base64 32)
82+
83+
# Disable guest mode when auth enable
84+
NEXT_PUBLIC_GUEST_KEY=''
85+
86+
ENV="NEXTAUTH_SECRET=$NEXTAUTH_SECRET\n\
87+
OPENAI_API_KEY=$OPENAI_API_KEY\n\
88+
DATABASE_URL=${DATABASE_URL}\n\
89+
GITHUB_CLIENT_ID=${GITHUB_CLIENT_ID}\n\
90+
GITHUB_CLIENT_SECRET=${GITHUB_CLIENT_SECRET}\n\
91+
NEXT_PUBLIC_GUEST_KEY=${NEXT_PUBLIC_GUEST_KEY}\n\
92+
NEXTAUTH_SECRET=${NEXTAUTH_SECRET}\n\
93+
SERP_API_KEY=$SERP_API_KEY\n\
94+
NEXT_PUBLIC_WEB_SEARCH_ENABLED=$NEXT_PUBLIC_WEB_SEARCH_ENABLED\n"
95+
96+
printf $ENV > .env.auth
97+
98+
99+
# 模型定义
100+
modelDefinitions="generator client {
101+
provider = \"prisma-client-js\"
102+
}
103+
104+
datasource db {
105+
provider = \"mysql\"
106+
url = env(\"DATABASE_URL\")
107+
relationMode = \"prisma\"
108+
}
109+
110+
// Necessary for Next auth
111+
model Account {
112+
id String @id @default(cuid())
113+
userId String
114+
type String
115+
provider String
116+
providerAccountId String
117+
refresh_token String? @db.Text
118+
access_token String? @db.Text
119+
expires_at Int?
120+
token_type String?
121+
scope String?
122+
id_token String? @db.Text
123+
session_state String?
124+
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
125+
126+
@@unique([provider, providerAccountId])
127+
@@index([userId])
128+
}
129+
130+
model Session {
131+
id String @id @default(cuid())
132+
sessionToken String @unique
133+
userId String
134+
expires DateTime
135+
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
136+
137+
@@index([userId])
138+
}
139+
140+
model User {
141+
id String @id @default(cuid())
142+
name String?
143+
email String? @unique
144+
emailVerified DateTime?
145+
image String?
146+
role String?
147+
subscriptionId String? @db.Text
148+
customerId String? @db.Text
149+
150+
createDate DateTime @default(now())
151+
152+
accounts Account[]
153+
sessions Session[]
154+
Agent Agent[]
155+
156+
@@index([email])
157+
}
158+
159+
model VerificationToken {
160+
identifier String
161+
token String @unique
162+
expires DateTime
163+
164+
@@unique([identifier, token])
165+
}
166+
167+
model Agent {
168+
id String @id @default(cuid())
169+
userId String
170+
name String @db.Text
171+
goal String @db.Text
172+
173+
deleteDate DateTime?
174+
createDate DateTime @default(now())
175+
176+
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
177+
tasks AgentTask[]
178+
179+
@@index([userId, deleteDate, createDate])
180+
}
181+
182+
model AgentTask {
183+
id String @id @default(cuid())
184+
taskId String?
185+
parentTaskId String?
186+
agentId String
187+
type String
188+
status String?
189+
value String @db.Text
190+
info String? @db.Text
191+
sort Int
192+
193+
deleteDate DateTime?
194+
createDate DateTime @default(now())
195+
196+
agent Agent @relation(fields: [agentId], references: [id], onDelete: Cascade)
197+
198+
@@index([agentId])
199+
@@index([type])
200+
}"
201+
202+
# Generate prisma schema
203+
echo "$modelDefinitions" > prisma/schema.prisma

0 commit comments

Comments
 (0)