Giter Site home page Giter Site logo

Comments (8)

elithrar avatar elithrar commented on June 8, 2024

from sessions.

darienmiller88 avatar darienmiller88 commented on June 8, 2024

Ahh, I forgot to add the first part to my original question. And I didn't realize that the random key was problematic in that way, thanks. Changing this results in the ViewsController referencing an existing session created in the UserController instead of a new one previously, but all of the values added to the "Values" map by that same Controller are still not there for whatever reason. I have added an "isAuth" method to both the ViewsController and UserController to check to see the users that were added the "Values" map :

func  isAuth(res http.ResponseWriter, req *http.Request){
	newSession, _ := session.Store.Get(req, "cookie-name")
	 user, ok := newSession.Values["user"].(models.User)

	if !user.Authenticated || !ok{
		newSession.Save(req, res)
		res.WriteHeader(http.StatusUnauthorized)
		render.JSON(res, req,  m{"error": "not authorized})
		return
	}

	fmt.Println("values:", newSession.Values, "and user", user)//Prints expected values in the UserController, but not for the ViewsController
	render.JSON(res, req, user)
}

When this method is hit in the UserController, it returns session values as expected, but when the same method is hit in the ViewsController, the session values map is still empty.

from sessions.

darienmiller88 avatar darienmiller88 commented on June 8, 2024

Okay, so I noticed that when session values are saved on a particular path prefix, they can ONLY be accessed on those path prefixes, and not anywhere else. I'm not sure if that's by design, or if there's something I have to change in my code to give every path access to the users in the session, but I was considering wrapping the *sessions.CookieStore object in a struct with a map[interface{}]interface{} field as an accompanying field to store the session values to allow for persistence.

from sessions.

elithrar avatar elithrar commented on June 8, 2024

from sessions.

darienmiller88 avatar darienmiller88 commented on June 8, 2024

Sorry for the absence, I ran into a few hurdles along the way. So in my "session" package where I have my "Store" object of type *sessions.CookieStore, I also have a "Ctx" object of type context.Context which both my ViewsController and UserController access, with the UserController sending user data into the context, and the ViewsController accessing them to render the pages using that data. Is this way concurrency safe?

from sessions.

elithrar avatar elithrar commented on June 8, 2024

from sessions.

darienmiller88 avatar darienmiller88 commented on June 8, 2024

I'm storing values in a global context object in my session package, and I have both my Views and User controllers store and retrieve values from it, so I appear to be doing the latter. The challenge I'm having then is how to I send a request scope context from one controller to another? On the front end, after the user signs in, Javascript sends the data to my UserController via fetch request, which is then intercepted by a middleware that parses the data into a User object, which is then put into a request scoped context and sent to the /api/users/signin route. That route then stores the user in a session, and sends JSON back to the frontend, which then reroutes to the / route. I'm not sure how to restructure my code to allow requests scoped contexts to be sent from one controller serving one path prefix to another controller serving another. Is there a particular model or pattern I have to follow?

from sessions.

stale avatar stale commented on June 8, 2024

This issue has been automatically marked as stale because it hasn't seen a recent update. It'll be automatically closed in a few days.

from sessions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.