users: handle getting new user if old one is gone

This commit is contained in:
Chris Sexton 2019-10-28 12:18:05 -04:00
parent 12f550a138
commit 378b9efc0e
4 changed files with 51 additions and 5 deletions

View File

@ -4,8 +4,8 @@ export default {
getMoods(userInfo) { getMoods(userInfo) {
return axios.get("/v1/moods", { return axios.get("/v1/moods", {
headers: { headers: {
'X-user-id': userInfo ? userInfo.ID : null, 'X-user-id': userInfo.ID,
'X-user-validation': userInfo ? userInfo.Validation : null 'X-user-validation': userInfo.Validation
} }
}) })
}, },
@ -18,6 +18,14 @@ export default {
}) })
}, },
getNewUser() { 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
}
})
} }
} }

View File

@ -24,6 +24,10 @@
}); });
} else { } else {
this.$store.commit('setUser', userInfo); this.$store.commit('setUser', userInfo);
this.$store.dispatch('getUserInfo', userInfo)
.catch(() => {
this.logOut();
})
} }
}, },
logOut: function () { logOut: function () {

View File

@ -8,7 +8,7 @@ Vue.use(Vuex);
const store = new Vuex.Store({ const store = new Vuex.Store({
state: { state: {
errs: [], errs: [],
userInfo: null, userInfo: {ID: "", Validation: ""},
moods: [], moods: [],
}, },
actions: { actions: {
@ -24,6 +24,12 @@ const store = new Vuex.Store({
commit('setUser', resp.data); commit('setUser', resp.data);
}) })
}, },
getUserInfo({state}, userInfo) {
if (userInfo)
return api.getUserInfo(userInfo)
else
return api.getUserInfo(state.userInfo)
},
setMood({state}, mood) { setMood({state}, mood) {
return api.setMood(state.userInfo, mood) return api.setMood(state.userInfo, mood)
}, },

View File

@ -187,6 +187,9 @@ func (s *server) NewUserID() (UserID, error) {
func (s *server) FromStr(uid, verification string) (UserID, error) { func (s *server) FromStr(uid, verification string) (UserID, error) {
id := UserID{db: s.db} id := UserID{db: s.db}
if uid == "" || verification == "" {
return id, fmt.Errorf("user ID and verification not given.")
}
idInt, err := s.h.DecodeInt64WithError(uid) idInt, err := s.h.DecodeInt64WithError(uid)
if err != nil { if err != nil {
return id, err return id, err
@ -289,6 +292,30 @@ func (s *server) getMoods(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s", string(resp)) 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) { func (s *server) handleMood(w http.ResponseWriter, r *http.Request) {
uid := r.Header.Get("X-user-id") uid := r.Header.Get("X-user-id")
verify := r.Header.Get("X-user-validation") verify := r.Header.Get("X-user-validation")
@ -364,7 +391,8 @@ func (s *server) routeSetup() *mux.Router {
api := r.PathPrefix("/v1/").Subrouter() api := r.PathPrefix("/v1/").Subrouter()
api.HandleFunc("/moods", s.getMoods).Methods("GET") api.HandleFunc("/moods", s.getMoods).Methods("GET")
api.HandleFunc("/moods", s.handleMood).Methods("POST") 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")) r.PathPrefix("/").HandlerFunc(s.indexHandler("/index.html"))
return r return r
} }