From 378b9efc0eeba6cf43741f762a45b6fc54bc40af Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Mon, 28 Oct 2019 12:18:05 -0400 Subject: [PATCH] users: handle getting new user if old one is gone --- frontend/src/api.js | 14 ++++++++++--- frontend/src/components/UserInfo.vue | 4 ++++ frontend/src/store.js | 8 +++++++- serve.go | 30 +++++++++++++++++++++++++++- 4 files changed, 51 insertions(+), 5 deletions(-) diff --git a/frontend/src/api.js b/frontend/src/api.js index c2c338f..9792a15 100644 --- a/frontend/src/api.js +++ b/frontend/src/api.js @@ -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 + } + }) } } diff --git a/frontend/src/components/UserInfo.vue b/frontend/src/components/UserInfo.vue index 2b6cc4f..ba51497 100644 --- a/frontend/src/components/UserInfo.vue +++ b/frontend/src/components/UserInfo.vue @@ -24,6 +24,10 @@ }); } else { this.$store.commit('setUser', userInfo); + this.$store.dispatch('getUserInfo', userInfo) + .catch(() => { + this.logOut(); + }) } }, logOut: function () { diff --git a/frontend/src/store.js b/frontend/src/store.js index 2d361be..2663b09 100644 --- a/frontend/src/store.js +++ b/frontend/src/store.js @@ -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) }, diff --git a/serve.go b/serve.go index f9f72d9..cb92828 100644 --- a/serve.go +++ b/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 }