Skip to content

Commit aa35dd8

Browse files
authored
🎨 Add a 'remember me' checkbox when logging in to save a session for 30 days (#14964)
1 parent dae6158 commit aa35dd8

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

app/appearance/langs/en_US.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,7 @@
15961596
"253": "Compressing file [%s], please wait...",
15971597
"254": "[Region ID] Incorrect Region ID, please refer to the S3 service provider's documentation to configure the Region ID",
15981598
"255": "The target site has enabled hotlink protection, so it is not possible to download [%d] resources",
1599-
"256": "The specified path [%s] has a parent workspace path [%s]"
1599+
"256": "The specified path [%s] has a parent workspace path [%s]",
1600+
"257": "Remember me for 30 days"
16001601
}
16011602
}

app/stage/auth.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ <h1 style="margin-bottom: 48px;color:var(--b3-theme-on-background)">{{.workspace
184184
<img id="captchaImg" style="top: 1px;position: absolute;height: 26px;right: 1px;cursor: pointer">
185185
<input id="captcha" class="b3-text-field" placeholder="{{.l3}}">
186186
</div>
187+
<div style="width: 240px; margin: 8px auto; text-align: left;">
188+
<input type="checkbox" id="rememberMe" style="margin-right: 8px;">
189+
<label for="rememberMe" style="color: var(--b3-theme-on-surface); font-size: 14px;">{{.l10}}</label>
190+
</div>
187191
<button class="b3-button" onclick="submitAuth()">{{.l1}}</button>
188192
<div class="ft__on-surface">
189193
{{.l2}}
@@ -475,6 +479,7 @@ <h1 style="margin-bottom: 48px;color:var(--b3-theme-on-background)">{{.workspace
475479
const submitAuth = () => {
476480
const inputElement = document.getElementById('authCode')
477481
const captchaElement = document.getElementById('captcha')
482+
const rememberMeElement = document.getElementById('rememberMe')
478483
let code = inputElement.value.trim();
479484
if ("" === code) {
480485
showMessage({{.l9}})
@@ -489,6 +494,7 @@ <h1 style="margin-bottom: 48px;color:var(--b3-theme-on-background)">{{.workspace
489494
body: JSON.stringify({
490495
authCode: code,
491496
captcha: captchaElement.value,
497+
rememberMe: rememberMeElement.checked
492498
}),
493499
}).then((response) => {
494500
return response.json()

kernel/model/session.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/gorilla/websocket"
3232
"github.com/siyuan-note/logging"
3333
"github.com/siyuan-note/siyuan/kernel/util"
34+
ginSessions "github.com/gin-contrib/sessions"
3435
"github.com/steambap/captcha"
3536
)
3637

@@ -121,6 +122,26 @@ func LoginAuth(c *gin.Context) {
121122
workspaceSession.AccessAuthCode = authCode
122123
util.WrongAuthCount = 0
123124
workspaceSession.Captcha = gulu.Rand.String(7)
125+
126+
// Handle remember me preference
127+
if rememberMe, ok := arg["rememberMe"].(bool); ok && rememberMe {
128+
// Set session cookie to expire in 30 days
129+
ginSessions.Default(c).Options(ginSessions.Options{
130+
Path: "/",
131+
Secure: util.SSL,
132+
MaxAge: 30 * 24 * 60 * 60, // 30 days in seconds
133+
HttpOnly: true,
134+
})
135+
} else {
136+
// Default session expiration (browser session)
137+
ginSessions.Default(c).Options(ginSessions.Options{
138+
Path: "/",
139+
Secure: util.SSL,
140+
MaxAge: 0, // Session cookie
141+
HttpOnly: true,
142+
})
143+
}
144+
124145
logging.LogInfof("auth success [ip=%s]", util.GetRemoteAddr(c.Request))
125146
if err := session.Save(c); err != nil {
126147
logging.LogErrorf("save session failed: " + err.Error())

kernel/server/serve.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ func serveAuthPage(c *gin.Context) {
452452
"l7": template.HTML(model.Conf.Language(184)),
453453
"l8": model.Conf.Language(95),
454454
"l9": model.Conf.Language(83),
455+
"l10": model.Conf.Language(257),
455456
"appearanceMode": model.Conf.Appearance.Mode,
456457
"appearanceModeOS": model.Conf.Appearance.ModeOS,
457458
"workspace": util.WorkspaceName,

0 commit comments

Comments
 (0)