Skip to content

Commit

Permalink
fix: remember me session duration (go-shiori#346)
Browse files Browse the repository at this point in the history
- Default session duration is 1h
- Increased session duration (when remember me is checked) is
  increased to 30d.
  • Loading branch information
fmartingr authored Feb 11, 2022
1 parent f1c98a8 commit d569932
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 69 deletions.
6 changes: 3 additions & 3 deletions internal/view/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,22 @@

// Send request
this.loading = true;
var sessionAge = this.remember ? 12 : 1;

fetch(new URL("api/login", document.baseURI), {
method: "post",
body: JSON.stringify({
username: this.username,
password: this.password,
remember: sessionAge,
remember: this.remember == 1 ? true : false,
}),
headers: { "Content-Type": "application/json" },
}).then(response => {
if (!response.ok) throw response;
return response.json();
}).then(json => {
// Save session id
var expTime = new Date(Date.now() + sessionAge * 3600 * 1000).toUTCString();
var sessionAge = this.remember == 1 ? 60 * 60 * 24 * 30 : 10
var expTime = new Date(Date.now() + sessionAge * 1000).toUTCString();
document.cookie = `session-id=${json.session}; Path=${new URL(document.baseURI).pathname}; Expires=${expTime}`;

// Save account data
Expand Down
124 changes: 62 additions & 62 deletions internal/webserver/assets-prod.go

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions internal/webserver/handler-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (h *handler) apiLogin(w http.ResponseWriter, r *http.Request, ps httprouter
request := struct {
Username string `json:"username"`
Password string `json:"password"`
Remember int `json:"remember"`
Remember bool `json:"remember"`
Owner bool `json:"owner"`
}{}

Expand Down Expand Up @@ -101,10 +101,10 @@ func (h *handler) apiLogin(w http.ResponseWriter, r *http.Request, ps httprouter

// Calculate expiration time
expTime := time.Hour
if request.Remember > 0 {
expTime = time.Duration(request.Remember) * time.Hour
if request.Remember {
expTime = time.Hour * 24 * 30
} else {
expTime = -1
expTime = time.Second * 10
}

// Create session
Expand Down

0 comments on commit d569932

Please sign in to comment.