How to implement user session for logged in user?

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?

2 Likes

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

2 Likes

I currently use:

Thanks!

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.