users: handle getting new user if old one is gone
This commit is contained in:
parent
12f550a138
commit
378b9efc0e
|
@ -4,8 +4,8 @@ export default {
|
|||
getMoods(userInfo) {
|
||||
return axios.get("/v1/moods", {
|
||||
headers: {
|
||||
'X-user-id': userInfo ? userInfo.ID : null,
|
||||
'X-user-validation': userInfo ? userInfo.Validation : null
|
||||
'X-user-id': userInfo.ID,
|
||||
'X-user-validation': userInfo.Validation
|
||||
}
|
||||
})
|
||||
},
|
||||
|
@ -18,6 +18,14 @@ export default {
|
|||
})
|
||||
},
|
||||
getNewUser() {
|
||||
return axios.get("/v1/register/code")
|
||||
return axios.get("/v1/user/code")
|
||||
},
|
||||
getUserInfo(userInfo) {
|
||||
return axios.get("/v1/user/info", {
|
||||
headers: {
|
||||
'X-user-id': userInfo.ID,
|
||||
'X-user-validation': userInfo.Validation
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,6 +24,10 @@
|
|||
});
|
||||
} else {
|
||||
this.$store.commit('setUser', userInfo);
|
||||
this.$store.dispatch('getUserInfo', userInfo)
|
||||
.catch(() => {
|
||||
this.logOut();
|
||||
})
|
||||
}
|
||||
},
|
||||
logOut: function () {
|
||||
|
|
|
@ -8,7 +8,7 @@ Vue.use(Vuex);
|
|||
const store = new Vuex.Store({
|
||||
state: {
|
||||
errs: [],
|
||||
userInfo: null,
|
||||
userInfo: {ID: "", Validation: ""},
|
||||
moods: [],
|
||||
},
|
||||
actions: {
|
||||
|
@ -24,6 +24,12 @@ const store = new Vuex.Store({
|
|||
commit('setUser', resp.data);
|
||||
})
|
||||
},
|
||||
getUserInfo({state}, userInfo) {
|
||||
if (userInfo)
|
||||
return api.getUserInfo(userInfo)
|
||||
else
|
||||
return api.getUserInfo(state.userInfo)
|
||||
},
|
||||
setMood({state}, mood) {
|
||||
return api.setMood(state.userInfo, mood)
|
||||
},
|
||||
|
|
30
serve.go
30
serve.go
|
@ -187,6 +187,9 @@ func (s *server) NewUserID() (UserID, error) {
|
|||
|
||||
func (s *server) FromStr(uid, verification string) (UserID, error) {
|
||||
id := UserID{db: s.db}
|
||||
if uid == "" || verification == "" {
|
||||
return id, fmt.Errorf("user ID and verification not given.")
|
||||
}
|
||||
idInt, err := s.h.DecodeInt64WithError(uid)
|
||||
if err != nil {
|
||||
return id, err
|
||||
|
@ -289,6 +292,30 @@ func (s *server) getMoods(w http.ResponseWriter, r *http.Request) {
|
|||
fmt.Fprintf(w, "%s", string(resp))
|
||||
}
|
||||
|
||||
func (s *server) checkUser(w http.ResponseWriter, r *http.Request) {
|
||||
uid := r.Header.Get("X-user-id")
|
||||
verify := r.Header.Get("X-user-validation")
|
||||
log.Debug().
|
||||
Str("uid", uid).
|
||||
Str("verify", verify).
|
||||
Msg("checkUser")
|
||||
user, err := s.FromStr(uid, verify)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("user not known")
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
fmt.Fprint(w, "User not known")
|
||||
return
|
||||
}
|
||||
j, err := json.Marshal(user)
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
log.Error().Err(err).Msg("could not marshal user")
|
||||
fmt.Fprintf(w, "%s", err)
|
||||
return
|
||||
}
|
||||
w.Write(j)
|
||||
}
|
||||
|
||||
func (s *server) handleMood(w http.ResponseWriter, r *http.Request) {
|
||||
uid := r.Header.Get("X-user-id")
|
||||
verify := r.Header.Get("X-user-validation")
|
||||
|
@ -364,7 +391,8 @@ func (s *server) routeSetup() *mux.Router {
|
|||
api := r.PathPrefix("/v1/").Subrouter()
|
||||
api.HandleFunc("/moods", s.getMoods).Methods("GET")
|
||||
api.HandleFunc("/moods", s.handleMood).Methods("POST")
|
||||
api.HandleFunc("/register/code", s.handlerRegisterCode).Methods("GET")
|
||||
api.HandleFunc("/user/code", s.handlerRegisterCode).Methods("GET")
|
||||
api.HandleFunc("/user/info", s.checkUser).Methods("GET")
|
||||
r.PathPrefix("/").HandlerFunc(s.indexHandler("/index.html"))
|
||||
return r
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue