Beispiel #1
0
func SignOut(w http.ResponseWriter, r *http.Request) {
	userToken := model.UserToken{}
	userToken.Token = context.Get(r, "user_token").(string)
	if err := userToken.GetUserIdFromToken(); err != nil {
		reply.Err(w, ae.DB("", err))
		return
	}
	if err := userToken.Delete(); err != nil {
		reply.Err(w, ae.DB("", err))
		return
	}
	reply.OK(w, userToken)
}
func CheckOnlyToken() Adapter {
	return func(h http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			token := r.Header.Get("X-TOKEN")
			if token == "" {
				reply.Err(w, ae.Forbidden(""))
				return
			}
			userToken := model.UserToken{Token: token}
			if err := userToken.GetUserIdFromToken(); err != nil || userToken.UserID == 0 {
				reply.Err(w, ae.Forbidden(""))
				return
			}
			context.Set(r, "user_id", userToken.UserID)
			context.Set(r, "user_token", userToken.Token)
			h.ServeHTTP(w, r)
		})
	}
}