commit e1402224e79724f409d6276c9860504448c2b30b
parent bd6745fb31e2faa4344142e294478a0eda1fb253
Author: vh <vetle.haflan@gmail.com>
Date: Wed, 29 Apr 2020 13:44:38 +0200
Set page title and allow logouts
Diffstat:
4 files changed, 37 insertions(+), 13 deletions(-)
diff --git a/internal/http/auth.go b/internal/http/auth.go
@@ -22,7 +22,7 @@ const (
// initHandler checks two things:
// 1. If a CSRF token exists for the given session. Otherwise it creates it
// 2. Whether the session is authenticated
-// It then returns an object on the form {"auth": true, "csrftoken": "<long string>"}
+// It then returns an object on the form {"auth": true, "csrftoken": "<long string>", "pagetitle": "Page title"}
// This is requested immediately when the website is loaded.
func initHandler(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, keySessionName)
@@ -34,9 +34,11 @@ func initHandler(w http.ResponseWriter, r *http.Request) {
session.Save(r, w) // TODO: Error handling, as always
}
b, _ := json.Marshal(struct {
+ PageTitle string `json:"pagetitle"`
CSRFToken string `json:"csrftoken"`
Authenticated bool `json:"authenticated"`
}{
+ string(conf.PageTitle),
token,
authorized(r),
})
@@ -70,7 +72,10 @@ func logoutHandler(w http.ResponseWriter, r *http.Request) {
session.Values[keyAuthenticated] = false
err := session.Save(r, w)
check(err)
- w.Write([]byte("Logged out"))
+ b, _ := json.Marshal(struct {
+ Info string `json:"info"`
+ }{"Logged out"})
+ w.Write(b)
}
func authorized(r *http.Request) bool {
@@ -104,11 +109,11 @@ func csrfCheckPassed(r *http.Request) bool {
if !ok {
panic("no CSRF token found")
}
- if tokenHeader := r.Header[headerCSRFToken]; tokenHeader == nil {
+ tokenHeader := r.Header[headerCSRFToken]
+ if tokenHeader == nil {
return false
- } else {
- return tokenHeader[0] == rightToken
}
+ return tokenHeader[0] == rightToken
}
func generateCSRFToken() string {
diff --git a/internal/http/html_dev.go b/internal/http/html_dev.go
@@ -4,10 +4,10 @@ package http
import "io/ioutil"
-// Can't use secure cookies in dev mode
-const PRODUCTION = false;
+// PRODUCTION is set to false because secure cookies can't be used in dev mode
+const PRODUCTION = false
-// In production mode, the website is embedded in (generated) code
+// In production mode, the website is embedded in (generated) code
// In dev mode it's more useful to read the html file on every request
func getWebsite() []byte {
htmlData, err := ioutil.ReadFile("ui/dist/index.html")
diff --git a/ui/src/App.vue b/ui/src/App.vue
@@ -2,8 +2,10 @@
<div>
<header :style="headerStyle">
<div id="bar">
- <div style="font-size: 1.5em; color: #bbf">> sermoni</div>
- <div @click="togglePage" style="margin-left: auto;">
+ <div @click="logout" style="font-size:1.5em;color:#bbf;cursor:pointer">
+ {{ pageTitle }}
+ </div>
+ <div v-show="loggedIn" @click="togglePage" style="margin-left: auto;">
<Eye :service-view="this.serviceView" style="cursor: pointer;"/>
</div>
</div>
@@ -28,15 +30,29 @@
components: {Login, Eye, Events, Services},
data() {
return {
+ pageTitle: "",
page: null,
serviceView: false,
- error: false
+ error: false,
+ loggedIn: false
};
},
methods: {
login() {
this.page = Events;
this.error = false;
+ this.loggedIn = true;
+ },
+ logout() {
+ if (!this.loggedIn) {
+ return;
+ }
+ api.logout(
+ success => {
+ this.loggedIn = false;
+ this.page = Login;
+ }
+ );
},
togglePage() {
// Should do nothing when on login page
@@ -60,7 +76,9 @@
mounted() {
api.init(
successData => {
- if (successData.authenticated) {
+ this.pageTitle = successData.pagetitle;
+ this.loggedIn = successData.authenticated;
+ if (this.loggedIn) {
this.page = Events;
} else {
this.page = Login;
diff --git a/ui/src/requests.js b/ui/src/requests.js
@@ -3,6 +3,7 @@
export default {
init,
login,
+ logout,
getServices,
postService,
deleteService,
@@ -37,7 +38,7 @@ function login(passphrase, successHandler, errorHandler) {
});
}
-function logout(passphrase, successHandler, errorHandler) {
+function logout(successHandler, errorHandler) {
request({
url: "/logout",
method: "POST",