On a website I’m developing, using Node, Express, and Backbone, I have the user login using a regular HTML form that, when successfully logged in, creates a user session. Accessing this session info is easy on the server-side, giving me access to the User ID, Username, etc., but I’m not sure on how to access this information in Backbone.
I want to acquire the UserID such that I can create a URL for a Collection like this:
url: '/api/users/'+ this.userID +'/movies';
But I’m not sure how to best go about acquiring the userID based upon the Session data that’s on the server-side. Would this require creating another model — say, a ‘User’ model that fetches the session data from the server, through a particular url/get request?
Thanks!
I came up with something, but would still be open to suggestions:
First, create a function in Express.js that returns the userId:
Second, on the client side, retrieve the ID using either a Backbone model or $.get request:
With your collection and model doing this:
myModel.save(); then works correctly.