Skip to content

Commit e6ee45c

Browse files
Generate AUTH_SECRET if not provided (#189)
1 parent 19780aa commit e6ee45c

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

entrypoint.sh

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ if [ ! -d "$DB_DATA_DIR" ]; then
2727
fi
2828

2929
if [ -z "$SOURCEBOT_ENCRYPTION_KEY" ]; then
30-
echo -e "\e[31m[Error] SOURCEBOT_ENCRYPTION_KEY is not set.\e[0m"
30+
echo -e "\e[33m[Warning] SOURCEBOT_ENCRYPTION_KEY is not set.\e[0m"
3131

3232
if [ -f "$DATA_CACHE_DIR/.secret" ]; then
3333
echo -e "\e[34m[Info] Loading environment variables from $DATA_CACHE_DIR/.secret\e[0m"
@@ -42,6 +42,23 @@ if [ -z "$SOURCEBOT_ENCRYPTION_KEY" ]; then
4242
set +a
4343
fi
4444

45+
# @see : https://authjs.dev/getting-started/deployment#auth_secret
46+
if [ -z "$AUTH_SECRET" ]; then
47+
echo -e "\e[33m[Warning] AUTH_SECRET is not set.\e[0m"
48+
49+
if [ -f "$DATA_CACHE_DIR/.authjs-secret" ]; then
50+
echo -e "\e[34m[Info] Loading environment variables from $DATA_CACHE_DIR/.authjs-secret\e[0m"
51+
else
52+
echo -e "\e[34m[Info] Generating a new encryption key...\e[0m"
53+
AUTH_SECRET=$(openssl rand -base64 33)
54+
echo "AUTH_SECRET=\"$AUTH_SECRET\"" >> "$DATA_CACHE_DIR/.authjs-secret"
55+
fi
56+
57+
set -a
58+
. "$DATA_CACHE_DIR/.authjs-secret"
59+
set +a
60+
fi
61+
4562
# In order to detect if this is the first run, we create a `.installed` file in
4663
# the cache directory.
4764
FIRST_RUN_FILE="$DATA_CACHE_DIR/.installedv2"

packages/web/src/auth.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@ declare module 'next-auth/jwt' {
2424
}
2525

2626
const providers: Provider[] = [
27-
GitHub({
28-
clientId: AUTH_GITHUB_CLIENT_ID,
29-
clientSecret: AUTH_GITHUB_CLIENT_SECRET,
30-
}),
31-
Google({
32-
clientId: AUTH_GOOGLE_CLIENT_ID!,
33-
clientSecret: AUTH_GOOGLE_CLIENT_SECRET!,
34-
})
27+
...(AUTH_GITHUB_CLIENT_ID && AUTH_GITHUB_CLIENT_SECRET ? [
28+
GitHub({
29+
clientId: AUTH_GITHUB_CLIENT_ID,
30+
clientSecret: AUTH_GITHUB_CLIENT_SECRET,
31+
}),
32+
] : []),
33+
...(AUTH_GOOGLE_CLIENT_ID && AUTH_GOOGLE_CLIENT_SECRET ? [
34+
Google({
35+
clientId: AUTH_GOOGLE_CLIENT_ID,
36+
clientSecret: AUTH_GOOGLE_CLIENT_SECRET,
37+
}),
38+
] : []),
3539
];
3640

3741
// @see: https://authjs.dev/guides/pages/signin
@@ -56,6 +60,7 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
5660
session: {
5761
strategy: "jwt",
5862
},
63+
trustHost: true,
5964
callbacks: {
6065
async jwt({ token, user: _user }) {
6166
const user = _user as User | undefined;

0 commit comments

Comments
 (0)