chore: more work on the app

This commit is contained in:
2024-02-24 15:28:23 +00:00
parent 32771c7422
commit ce866725ff
10 changed files with 544 additions and 38 deletions

View File

@@ -471,11 +471,19 @@ func (x Handle) createContext(handler *Handle, mode AnswerType, r *http.Request)
Prefix: r.URL.Path,
})
for _, r := range r.Cookies() {
if r.Name == "auth" {
token = &r.Value
}
}
if mode != JSON {
for _, r := range r.Cookies() {
if r.Name == "auth" {
token = &r.Value
}
}
} else {
t := r.Header.Get("token")
if t != "" {
token = &t
}
}
// TODO check that the token is still valid
@@ -512,14 +520,19 @@ func Redirect(path string, mode AnswerType, w http.ResponseWriter, r *http.Reque
}
func Logoff(mode AnswerType, w http.ResponseWriter, r *http.Request) {
// Delete cookie
cookie := &http.Cookie{
Name: "auth",
Value: "",
Expires: time.Unix(0, 0),
}
http.SetCookie(w, cookie)
Redirect("/login", mode, w, r)
if (mode == JSON) {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("\"Not Authorized\""))
} else {
// Delete cookie
cookie := &http.Cookie{
Name: "auth",
Value: "",
Expires: time.Unix(0, 0),
}
http.SetCookie(w, cookie)
Redirect("/login", mode, w, r)
}
}
func notAuth(mode AnswerType, w http.ResponseWriter, r *http.Request) {