We'll start in userCtrl.js
.
- Create a method on our exports object named
login
, this method should loop through the users array, find the user that matchesreq.body.name
and confirm that thereq.body.password
matches the user's password. - If we find a match we need to set
req.session.currentUser
equal to to the correct user object andres.send({ userFound: true });
. - If we don't find the user, we will need to
res.send({ userFound: false });
. -->
On to profileCtrl.js
Here we will need a simple method on our exports object that pushes every profile that is in the req.session.currentUser
's friends
array. Then res.send
's
an object back containing our new array and the current user. The response object should be structured something like this:
{
currentUser: req.session.currentUser,
friends: yourArrayOfFriendObjects
}
This function will need an accompanying endpoint in your server.js
, so add an app.get
endpoint with a path of `'/api/profiles'.
## Step 3: Serving static files
<!-- Now you may have noticed that there was some front-end code included with the project, but at the beginning of the project it was mentioned that we would no
longer need to use `http-server` or `live-server`. We are going to send all of our static front-end files from our server.
This functionality is built into express with the `express.static()` method. All we need to do to begin sending our static files is add this
line to our `server.js`. -->
```javascript
app.use(express.static(__dirname + '/public'));
Take a few minutes to browse through the current .js
files in your public folder, you'll notice there are several areas containing FIX ME
's. Let's move
through and set up our front end so it is actually functional!
To start, you'll notice that our mainCtrl.js
is calling a function inside of our friendService.js
that contains a FIX ME
. This function should post to
your login
endpoint, sending the user
object we recieved from our controller.
Next we will need to fix the resolve inside of our app.js
. This resolve should return the result of our friendService.getFriends
method sending a 'GET'
request to our /api/profiles
endpoint.
Lastly you will need to inject that resolve into your profileCtrl.js
and assign the correct values to $scope.currentUser
and $scope.friends
.
Well done! Try logging in as several different users and seeing the different friend lists, all with very minimal front-end code. This was all done simply by tracking our user's session on the back-end.
- Allow users to add or remove friends.
- Add a settings view specific to the current user, where they can change their name or password.
© DevMountain LLC, 2015. Unauthorized use and/or duplication of this material without express and written permission from DevMountain, LLC is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to DevMountain with appropriate and specific direction to the original content.