Skip to content

Commit b336919

Browse files
committed
fix: updates for self-hosting
1 parent eb1d05e commit b336919

File tree

6 files changed

+139
-123
lines changed

6 files changed

+139
-123
lines changed

src/.env.selfhost.template

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
11
# Root environment variables for docker-compose.selfhost.yaml
22
# Copy this file to .env in the same directory (src/.env) and fill in the values.
33

4-
# --- Client (Frontend) Variables ---
5-
# The public-facing URL of your client application.
6-
# [BUILD-TIME & RUNTIME] Crucial for OAuth redirects and server-side requests.
4+
# --- Public URLs ---
5+
# These are the URLs that users will use in their browser to access the application.
6+
# For a standard local setup, these defaults are correct.
7+
# If you expose this on the internet, change localhost to your domain.
78
APP_BASE_URL=http://localhost:3000
89
NEXT_PUBLIC_APP_BASE_URL=http://localhost:3000
9-
10-
# The URL where the backend server will be accessible from the user's browser
11-
# [BUILD-TIME & RUNTIME] Used by the client to make API calls.
1210
NEXT_PUBLIC_APP_SERVER_URL=http://localhost:5000
1311

14-
# The internal URL for the backend server, used for server-to-server communication inside Docker.
15-
# [BUILD-TIME & RUNTIME] Used by the client's Next.js server to talk to the backend server.
12+
# --- Internal Docker URLs ---
13+
# These are for container-to-container communication and should not be changed.
1614
INTERNAL_APP_SERVER_URL=http://server:80
17-
18-
# The internal URL for the client container, used for server-side self-requests
19-
# [BUILD-TIME & RUNTIME] Used for OAuth callbacks within the Docker network.
2015
INTERNAL_CLIENT_URL=http://client:3000
2116

22-
# The mode to run the application in
23-
# [BUILD-TIME & RUNTIME] Switches between Auth0 and self-host auth mode.
17+
# --- Authentication ---
18+
# Sets the application to run in self-hosted mode. Do not change.
2419
NEXT_PUBLIC_ENVIRONMENT=selfhost
25-
26-
# [BUILD-TIME & RUNTIME] A long, random, secret string. It must match SELF_HOST_AUTH_SECRET in server/.env.selfhost.template
20+
# A long, random, secret string that acts as your master password for the app.
21+
# It must be the same in all three .env files where it appears.
22+
# Generate a strong secret here (e.g., using a password manager or `openssl rand -hex 32`).
2723
SELF_HOST_AUTH_TOKEN=<generate_a_strong_secret_here>
2824

2925
# --- Server (Backend) Build-Time Variables ---
3026
# [BUILD-TIME] Set OPENAI_API_KEY to "ollama" to install Ollama in the server container.
31-
# Otherwise, provide your key for a remote service.
27+
# This allows you to run a local LLM without needing an external API key for chat.
28+
# To use a remote service (like OpenAI or another provider via LiteLLM), provide your key here.
3229
OPENAI_API_KEY=ollama
33-
# [BUILD-TIME] The model to pull if Ollama is being installed. This should match the model in the server's Modelfile.
30+
# [BUILD-TIME] The model to pull if Ollama is being installed. This should match the model
31+
# in the server's Modelfile and the OPENAI_MODEL_NAME in server/.env.selfhost.
3432
OPENAI_MODEL_NAME=qwen3:4b
3533

36-
# --- Gemini API Key (for Server - Memory MCP & optional LiteLLM) ---
37-
# [RUNTIME] Required for memory embeddings and can be used for chat via LiteLLM.
34+
# --- Gemini API Key (for Server - Memory & optional LiteLLM) ---
35+
# [RUNTIME] Required for memory embeddings and can be used for chat via the LiteLLM proxy.
36+
# Get this from Google AI Studio.
3837
GEMINI_API_KEY=<your-gemini-api-key>
3938

4039
# --- MongoDB Credentials (for Server) ---
41-
MONGO_USER=test
40+
MONGO_USER=sentient
4241
MONGO_PASS=<generate_a_strong_password_for_mongo>
4342

4443
# --- PostgreSQL Credentials (for Server - Memory MCP) ---
@@ -51,6 +50,7 @@ REDIS_PASSWORD=<generate_a_strong_password_for_redis>
5150

5251
# --- WhatsApp (WAHA) Credentials (for WAHA Service) ---
5352
# These are used by the WAHA container for WhatsApp integration.
53+
# You can leave these as default for a local setup.
5454
WAHA_API_KEY=admin
5555
WAHA_DASHBOARD_USERNAME=admin
5656
WAHA_DASHBOARD_PASSWORD=admin

src/client/.env.selfhost.template

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,28 @@ NEXT_PUBLIC_APP_SERVER_URL=http://localhost:5000
99

1010
# The internal URL for the backend server, used for server-to-server communication inside Docker.
1111
# This is passed at build time and used by the Next.js server part of the client.
12-
INTERNAL_APP_SERVER_URL=http://server:80 # Must match the root .env
12+
# It MUST match INTERNAL_APP_SERVER_URL in the root src/.env file.
13+
INTERNAL_APP_SERVER_URL=http://server:80
1314

1415
# The public-facing base URL of the client application.
16+
# These MUST match the corresponding variables in the root src/.env file.
1517
APP_BASE_URL=http://localhost:3000
1618
NEXT_PUBLIC_APP_BASE_URL=http://localhost:3000
1719

1820
# The static token for authenticating with the backend.
19-
# This MUST match the `SELF_HOST_AUTH_SECRET` in the server's .env.selfhost file.
21+
# This MUST match the `SELF_HOST_AUTH_TOKEN` in the root src/.env file.
2022
SELF_HOST_AUTH_TOKEN=<use_the_same_strong_secret_as_in_src/.env>
2123

2224
# --- Database (for Server Actions) ---
2325
# This is the internal URI for the MongoDB service within Docker.
2426
# It MUST include the credentials defined in the root .env file.
25-
MONGO_URI=mongodb://test:<pass_from_src/.env>@mongodb:27017/
26-
MONGO_DB_NAME=development
27+
MONGO_URI=mongodb://sentient:<pass_from_src/.env>@mongodb:27017/
28+
MONGO_DB_NAME=sentient_selfhost_db
2729

2830
# Auth0 variables are not used in selfhost mode, but are kept here
2931
# to avoid breaking any code that might reference them before a check.
3032
# The build process requires them to be present in some form.
3133
AUTH0_SECRET=""
32-
APP_BASE_URL="http://localhost:3000"
3334
AUTH0_ISSUER_BASE_URL=""
3435
AUTH0_CLIENT_ID=""
3536
AUTH0_CLIENT_SECRET=""

src/client/components/LayoutWrapper.js

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ export default function LayoutWrapper({ children }) {
550550
const [isSearchOpen, setSearchOpen] = useState(false)
551551
const [isMobileNavOpen, setMobileNavOpen] = useState(false)
552552
const [unreadCount, setUnreadCount] = useState(0)
553+
const [user, setUser] = useState(null) // Unified user state
553554
const [notifRefreshKey, setNotifRefreshKey] = useState(0)
554555
const [userDetails, setUserDetails] = useState(null)
555556
const wsRef = useRef(null)
@@ -566,7 +567,7 @@ export default function LayoutWrapper({ children }) {
566567
isHighlightPaused: false
567568
})
568569
const chatActionsRef = useRef(null)
569-
const pathname = usePathname()
570+
const pathname = usePathname() // eslint-disable-line
570571
const router = useRouter()
571572

572573
const skipTour = useCallback(() => {
@@ -820,7 +821,11 @@ export default function LayoutWrapper({ children }) {
820821
}
821822
]
822823

823-
const { user, error: authError, isLoading: isAuthLoading } = useUser()
824+
const {
825+
user: auth0User,
826+
error: authError,
827+
isLoading: isAuthLoading
828+
} = useUser()
824829

825830
const [isLoading, setIsLoading] = useState(true)
826831
const [isAllowed, setIsAllowed] = useState(false)
@@ -912,10 +917,10 @@ export default function LayoutWrapper({ children }) {
912917
const showNav = !["/", "/onboarding"].includes(pathname)
913918

914919
useEffect(() => {
915-
if (user && posthog) {
916-
posthog.identify(user.sub, {
917-
name: user.name,
918-
email: user.email
920+
if (auth0User && posthog) {
921+
posthog.identify(auth0User.sub, {
922+
name: auth0User.name,
923+
email: auth0User.email
919924
})
920925

921926
// --- NEW: Fetch custom properties and set PostHog groups ---
@@ -967,7 +972,7 @@ export default function LayoutWrapper({ children }) {
967972
fetchAndSetUserGroups()
968973
// --- END NEW ---
969974
}
970-
}, [user, posthog])
975+
}, [auth0User, posthog])
971976

972977
useEffect(() => {
973978
const paymentStatus = searchParams.get("payment_status")
@@ -1031,39 +1036,55 @@ export default function LayoutWrapper({ children }) {
10311036
return
10321037
}
10331038

1034-
if (isAuthLoading) return
1039+
const checkStatus = async () => {
1040+
// --- SELF-HOST AUTH LOGIC ---
1041+
if (process.env.NEXT_PUBLIC_ENVIRONMENT === "selfhost") {
1042+
try {
1043+
const res = await fetch("/api/user/profile")
1044+
if (res.ok) {
1045+
const selfHostUser = await res.json()
1046+
setUser(selfHostUser) // Set the unified user state
1047+
setIsAllowed(true)
1048+
} else {
1049+
throw new Error(
1050+
"Failed to fetch self-host user profile."
1051+
)
1052+
}
1053+
} catch (error) {
1054+
toast.error(error.message)
1055+
setIsAllowed(false)
1056+
} finally {
1057+
setIsLoading(false)
1058+
}
1059+
return
1060+
}
10351061

1036-
if (authError) {
1037-
toast.error("Session error. Redirecting to login.", {
1038-
id: "auth-error"
1039-
})
1040-
router.push("/auth/login")
1041-
return
1042-
}
1062+
// --- AUTH0 AUTH LOGIC ---
1063+
if (isAuthLoading) return
10431064

1044-
if (!user) {
1045-
// This handles the case where the user logs out.
1046-
router.push("/auth/login")
1047-
return
1048-
}
1065+
if (authError) {
1066+
toast.error(
1067+
`Session error: ${authError.message}. Redirecting to login.`,
1068+
{ id: "auth-error" }
1069+
)
1070+
router.push("/api/auth/login")
1071+
return
1072+
}
1073+
1074+
if (!auth0User) {
1075+
router.push("/api/auth/login")
1076+
return
1077+
}
1078+
1079+
setUser(auth0User) // Set the unified user state
10491080

1050-
const checkStatus = async () => {
1051-
// No need to set isLoading(true) here, it's already true by default.
10521081
try {
10531082
const res = await fetch("/api/user/data", { method: "POST" })
10541083
if (!res.ok) throw new Error("Could not verify user status.")
10551084
const result = await res.json()
1056-
const data = result?.data || {}
1057-
1058-
const onboardingComplete = data.onboardingComplete
1059-
1060-
if (!onboardingComplete) {
1061-
toast.error("Please complete onboarding first.", {
1062-
id: "onboarding-check"
1063-
})
1085+
if (!result?.data?.onboardingComplete) {
10641086
router.push("/onboarding")
10651087
} else {
1066-
// User is fully onboarded and profile is complete.
10671088
setIsAllowed(true)
10681089
}
10691090
} catch (error) {
@@ -1075,7 +1096,7 @@ export default function LayoutWrapper({ children }) {
10751096
}
10761097

10771098
checkStatus()
1078-
}, [pathname, router, showNav, user, authError, isAuthLoading]) // Reruns on navigation
1099+
}, [showNav, auth0User, isAuthLoading, authError, router])
10791100

10801101
const handleNotificationsOpen = useCallback(() => {
10811102
setNotificationsOpen(true)
@@ -1294,8 +1315,8 @@ export default function LayoutWrapper({ children }) {
12941315
}, [])
12951316

12961317
useEffect(() => {
1297-
if (showNav && userDetails?.sub) subscribeToPushNotifications()
1298-
}, [showNav, userDetails, subscribeToPushNotifications])
1318+
if (showNav && user?.sub) subscribeToPushNotifications()
1319+
}, [showNav, user, subscribeToPushNotifications])
12991320

13001321
// Define shortcuts after all their callback dependencies are defined
13011322
useGlobalShortcuts(
@@ -1325,14 +1346,14 @@ export default function LayoutWrapper({ children }) {
13251346
<PlanContext.Provider
13261347
value={{
13271348
plan: (
1328-
user?.[
1349+
auth0User?.[
13291350
`${process.env.NEXT_PUBLIC_AUTH0_NAMESPACE}/roles`
13301351
] || []
13311352
).includes("Pro")
13321353
? "pro"
13331354
: "free",
13341355
isPro: (
1335-
user?.[
1356+
auth0User?.[
13361357
`${process.env.NEXT_PUBLIC_AUTH0_NAMESPACE}/roles`
13371358
] || []
13381359
).includes("Pro"),

src/docker-compose.selfhost.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ services:
102102

103103
# WAHA (WhatsApp HTTP API) Service
104104
waha:
105-
image: itsskofficial/waha-multisession
105+
image: devlikeapro/waha
106106
container_name: sentient-waha-selfhost
107107
restart: unless-stopped
108108
volumes:

0 commit comments

Comments
 (0)