Node server authenticate for iOS client using token

Authentication – POST for /api/authenticate using parameters ‘name’ and ‘password’

Here’s we authenticate by giving the server a name and password. If the name and password match the ones stored in the database, then we return it a token for the iOS client to use. Whenever that the iOS client needs to access a protected URL, we use that token.

Server side

/config.js

First notice that in the beginning of the file, we use set to have
superSecret name as an application setting name.

/server.js

We set the key ‘superSecret’ to match config.secret which is ‘ilovescotchyscotch’ in /config.js

Later we will use jwt’s sign function on this string along with the user object in order to create a token for the iOS client to use.

We use express’s router function to get variable apiRoutes. We prefix all routes with ‘/api’.

Then for POST on url /authenticate, we use mongoose’s model User to find if the user exist. If it does, it will then run the async method to see if the passwords match.

If the passwords match, then we use jwt’s sign function to return a token for the user to use. We wrap that token along with other string info in a json object and return it via the response object.

iOS client

We create NSDictionary with name and password key. Then we encode it into a NSString.

Notice we then call the URL ‘http://192.168.1.102:8080/api/authenticate’ with POST, and the json string in the body of the request.

After we POST this request, our server will hit the apiRoutes.post(‘/authenticate’, function(req, res) {…}) function, which uses jwt.sign(user, app.get(‘superSecret’)) to return a token for this particular user.

It returns:

res.json({
success: true,
message: ‘Enjoy your token!’,
token: token
});

Hence the server code, we see that the returned json object has 3 keys:
success, message, and token. Use the value from key token in order to authenticate yourself when doing updates and other restricted activities.

Using Token to authenticate itself and access protected URLs

Reading list of users using approved authentication

Now that we have the authentication, we need to use it to read protected data

In your server.js, put this code to protect your URLs:

So that when you hit

http://localhost:8080/api/users

You’ll get:

{“success”:false,”message”:”No token provided.”}

So as you can see a client that does not provide the correct token and user name cannot access that what http://localhost:8080/api/users has to offer.

Thus, we access it by using the token received earlier when we authenticated with our user name and password. We set the token and our username into the header of the request packet.

AppDelegate.m

As you can see, once you log in, use the token string to get info.

GetInfoConnection.h

GetInfoConnection.m

Notice how we set the request’s header with the user name and token value in the method getUserInfo.

These values are then sent to the server where they get processed by apiRoutes.use(function(req, res, next) {…}), which uses jwt.verify to verify if this username and token is valid.

Then the server passes it onto the next middleware layer, namely, apiRoutes.get(‘/users’, function(req, res) {…}) to have the database retrieve the valid user.

Full server code