Skip to content

Commit

Permalink
feat: session expire time from backend (#437)
Browse files Browse the repository at this point in the history
* feat: session expire time from backend

Deduplicated logic from backend and frontend where we needed to take the
login checkbox into account for both back and front codebases. Now the
backend will send the frontend the expire time calculated only in one
place.

This is part of a multi-step process that will store sessions in
database, but this will ease the maintenance of this section for now.

* chore: remove test logger
  • Loading branch information
fmartingr committed Jul 23, 2022
1 parent 7e1824a commit cba5046
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
4 changes: 1 addition & 3 deletions internal/view/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@
return response.json();
}).then(json => {
// Save session id
var sessionAge = this.remember == 1 ? 60 * 60 * 24 * 30 : 60 * 60
var expTime = new Date(Date.now() + sessionAge * 1000).toUTCString();
document.cookie = `session-id=${json.session}; Path=${new URL(document.baseURI).pathname}; Expires=${expTime}`;
document.cookie = `session-id=${json.session}; Path=${new URL(document.baseURI).pathname}; Expires=${json.expires}`;

// Save account data
localStorage.setItem("shiori-account", JSON.stringify(json.account));
Expand Down
3 changes: 2 additions & 1 deletion internal/webserver/handler-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ func (h *handler) apiLogin(w http.ResponseWriter, r *http.Request, ps httprouter
loginResult := struct {
Session string `json:"session"`
Account model.Account `json:"account"`
}{strSessionID, account}
Expires string `json:"expires"`
}{strSessionID, account, time.Now().Add(expTime).Format(time.RFC1123)}

w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(&loginResult)
Expand Down

0 comments on commit cba5046

Please sign in to comment.