Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

feat(auth): 添加不安全连接下的cookie选项支持

Timothy Yincb7921a74c6c
diff --git a/.env.example b/.env.example
index 336b5ff3..8267fa77 100644
--- a/.env.example
+++ b/.env.example
@@ -30,3 +30,7 @@ NUXT_SESSION_PASSWORD=
# GitHub OAuth app credentials
NUXT_OAUTH_GITHUB_CLIENT_ID=
NUXT_OAUTH_GITHUB_CLIENT_SECRET=
+
+# INSECURE Options - DO NOT use these options in production or on public networks
+# Allow cookies to be transmitted over insecure connections, effects login session security
+NUXT_ALLOW_INSECURE_COOKIE=false
diff --git a/nuxt.config.ts b/nuxt.config.ts
index b2c5ddb0..67db5f16 100644
--- a/nuxt.config.ts
+++ b/nuxt.config.ts
@@ -44,6 +44,7 @@ export default defineNuxtConfig({
PROVIDER_S3_SECRET_ACCESS_KEY: '',
PROVIDER_S3_PREFIX: '',
PROVIDER_S3_CDN_URL: '',
+ ALLOW_INSECURE_COOKIE: false,
},
nitro: {
diff --git a/server/api/auth/github.get.ts b/server/api/auth/github.get.ts
index 629009fd..eda64508 100644
--- a/server/api/auth/github.get.ts
+++ b/server/api/auth/github.get.ts
@@ -35,7 +35,15 @@ export default defineOAuthGitHubEventHandler({
} else if (userFromEmail.isAdmin === 0) {
throw _accessDeniedError
} else {
- await setUserSession(event, { user: userFromEmail })
+ await setUserSession(
+ event,
+ { user: userFromEmail },
+ {
+ cookie: {
+ secure: !useRuntimeConfig().ALLOW_INSECURE_COOKIE,
+ },
+ },
+ )
}
return sendRedirect(event, '/')
},
diff --git a/server/api/login.post.ts b/server/api/login.post.ts
index ff9ac84a..bb7a8605 100644
--- a/server/api/login.post.ts
+++ b/server/api/login.post.ts
@@ -29,7 +29,15 @@ export default eventHandler(async (event) => {
throw _invalidCredentialsError
}
- await setUserSession(event, { user })
+ await setUserSession(
+ event,
+ { user },
+ {
+ cookie: {
+ secure: !useRuntimeConfig().ALLOW_INSECURE_COOKIE,
+ },
+ },
+ )
return setResponseStatus(event, 201)
})