How to implement user session for logged in user?


(cinematik) #1

For example I have a rest endpoint where user logs into system

func Login(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	rName := r.URL.Query().Get("username")
	rPass := r.URL.Query().Get("password")
	for _, u := range users {
		if u.Username == rName {
			if u.Password == rPass {
				loggedIn[u.ID] = struct{}{} // I suppose this is not a prefferable solution to implement user session
				return
			}
		}
	}
	http.Error(w, "Invalid username/password supplied", 400)
	return
}

Is there a way how save that user into session and later have the ability to check wheither a user logged in or nor just by checking something in session? By the way I’m using gorilla mux so maybe there is a gorilla mux specific solution for that?


(Yamil Bracho) #2

Yes, Gorilla has session management. Please check, https://www.gorillatoolkit.org/pkg/sessions


(Steve) #3

I currently use:

Thanks!