// authUser authenticates a user and adds to in memory cache
authUser authenticates a user and adds to in memory cache
// authUser authenticates a user and adds to in memory cache
func (s *Srv) authUser(w http.ResponseWriter, r *http.Request) {
	var u user.User
	err := u.ParseUser(r)
	if err != nil {
		log.Fatal(err)
	}
	var result user.User
	err = s.db.Database("podoloff").Collection("users").FindOne(context.TODO(), bson.D{primitive.E{Key: "email", Value: u.Email}}).Decode(&result)
	if err != nil {
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode("No user found with that email address.")
		return
	}
	err = bcrypt.CompareHashAndPassword([]byte(result.Password), []byte(u.Password))
	if err != nil {
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode("Passwords do not match.")
		return
	}
	http.SetCookie(w, u.IssueCookie())
	log.Print(result.ID)
	s.cache[u.LiveToken] = u.Email
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode("User authenticated.")
	return
}
func ( s * Srv ) authUser ( w http . ResponseWriter , r * http . Request ) { var u user . User err := u . ParseUser ( r ) if err != nil { log . Fatal ( err ) } var result user . User err = s . db . Database ( " " ) . Collection ( " " ) . FindOne ( context . TODO ( ) , bson . D { primitive . E { Key : " " , Value : u . Email } } ) . Decode ( & result ) if err != nil { w . Header ( ) . Set ( " " , " " ) json . NewEncoder ( w ) . Encode ( " " ) return } err = bcrypt . CompareHashAndPassword ( [ ] byte ( result . Password ) , [ ] byte ( u . Password ) ) if err != nil { w . Header ( ) . Set ( " " , " " ) json . NewEncoder ( w ) . Encode ( " " ) return } http . SetCookie ( w , u . IssueCookie ( ) ) log . Print ( result . ID ) s . cache [ u . LiveToken ] = u . Email w . Header ( ) . Set ( " " , " " ) json . NewEncoder ( w ) . Encode ( " " ) return }
authUser authenticates a user and adds to in memory cache
authUser authenticates a user and adds to in memory cache