Possibly Odd Approach for Quick Authentication - User Registry API and Passport
Tags: node, javascript, authentication, security
Published: 2019-05-15 00:46:56

---
title: Possibly Odd Approach for Quick Authentication - User Registry API and Passport
published: true
description: Free user authentication API project
tags: node, javascript, authentication, security
---

:hibiscus: So I woke up last night thinking "This might be a bit weird." :hibiscus:

![I love mermaids](https://media2.giphy.com/media/e7f0kJekLyFFe/giphy.gif?cid=790b76115cdae8865535536973d9856f&rid=giphy.gif)
<hr>
**Article**

I want to walk through what I did and why. So this article will go over integrating my existing app with sample code generated by creating the API on the platform.

My Cloud Foundry application is Node with Express framework routing, .ejs view engine. This API called `App ID` has a user registry that comes with sample code | SDKs for Swift, Node, Java and Android. The API has a free tier for up to 1000 users and 1000 events (an 'event' being like someone logging in).

Best way to see all features of the app is the [API Docs](https://tinyurl.com/y34yy59z). 

![api video](https://media.giphy.com/media/lPRJm0Y6QWnDUaY6ba/giphy.gif)
<hr>

**Friday Night**

I was on my beanbag in my office using [Passport-Local Mongoose](https://tinyurl.com/kwehabu) for a quick login by email and password for this application.

 ![colorful login](https://media.giphy.com/media/kEX6BLa5F2VofquFQt/giphy.gif) 
 
Do I really need to store user information in MongoDB? 
Nobody will need to sign-up for this application. 

![jff](https://media2.giphy.com/media/3ohhwF1sQvpS2XgPu0/giphy.gif?cid=790b76115cdc2d9d6d4130554571380b&rid=giphy.gif)

Currently, I am giving out the link to the application for a small pool of people that it could help. The application serves one purpose and zero APIs are used in it. Nothing sensitive really. 

Since I was using Passport.js already, I faintly recalled that there was a service on IBM Cloud I might've seen that used Passport somehow - this API ended up being App ID. It has a [Node SDK for mobile and web](https://tinyurl.com/y2bxcvpo). 

I was looking into the App ID documentation, and took note of something that might be really helpful: The [Cloud Directory](https://tinyurl.com/y5g6yhk8). 

So, I decided to use this API to register my users for my application.
And this move saved me a heck of a lot of time on Friday evening.

Weird part is that I have to populate the passwords for users...

**Time Spent - My First Time Using This API** ![doodle](https://media.giphy.com/media/dXG2oppbjzs6PgWSKZ/giphy.gif)

Let's get into it. Here are the steps. Shouldn't take much.

 <hr>

:page_facing_up: [Register for IBM Cloud](https://tinyurl.com/y5ma4aj3)
:e-mail: Confirm Registration by email 
:computer: Login to Account
:smile: Configure [IBM Cloud CLI](https://tinyurl.com/y46m227u)
:guitar: [Create App ID Service](https://tinyurl.com/y2f7rtvq)
:art: Download Sample Code 
:ocean: Integrate to Existing App
:alien: Setup and Redirect from Login
:ribbon: Add users in Cloud Directory
:crown: Deploy Application

<hr>

 **Register**

Does not require anything but name | email. No cards.

<hr>

**Confirmation**

I did this sign up process in writing this to make sure I go through everything, took less than five minutes to get the confirmation email per registering to the platform.

<hr> 
**Login**

Login to your account.

![stars](https://media2.giphy.com/media/l378bM6SldIuqFJtK/giphy.gif?cid=790b76115cdd80752e4f38744d5de6db&rid=giphy.gif)

<hr>
**CLI**

You will need this [command line interface for IBM Cloud](https://tinyurl.com/y46m227u). This is for when you build locally and then want to re-deploy the sample code with regards to this tutorial. 

![cli](https://media.giphy.com/media/YSHzjFJXGdMwiz1Llp/giphy.gif)

Commands that you need to know (remember post-install to have a new terminal session to start using the CLI) => 

`ibmcloud login`
`ibmcloud target --cf`

<hr>
**API**

Create the API for your own use. You can use my [link](https://tinyurl.com/y2f7rtvq) or find App ID under the Security section of the Catalog. You can do this all by the [IBM Cloud CLI](https://tinyurl.com/yx9s3naw). 

![catalog](https://media.giphy.com/media/fVWConMBT2Pjunb93l/giphy.gif)

<hr>

**Click**

Once you reach the landing page for the service, there is a little button labeled 'Create' in furthest right-hand corner. Click that to create an instance of App ID for your own use.

<hr>
**Sample Code**

Download the sample code for Swift, Node, Java or Android. 
![sample code](https://media.giphy.com/media/QWXb7zdirSYT5w7oF3/giphy.gif)
<hr>
**Integrate**

You can use this service while hosting your application on other platforms. It does not have to be hosted on IBM Cloud. 

Remember that sample code? It is really useful here. 
![downloaded code](https://media.giphy.com/media/eJA5Yrl4OcehkwvVcw/giphy.gif)

I opened the /protected.ejs in Sublime and placed the code of my existing application's home page here.

Then I made sure all the paths to the files were correct so it renders properly.
<hr>
**Setup**

For when you want to re-deploy this sample application to IBM Cloud with your integrations made, you will need to look at the alias connection for the sample code that is at the bottom of the `manifest.yml`.

![manifest](https://i.imgur.com/4lcPmV9.png)

This alias creates a connection between your App ID service and the sample code being a Cloud Foundry application. So, the IBM Cloud CLI needs to be configured for you to do this. The documentation on this part is actually very good.

BUT there is *one funky thing* to deal with that will save you some time. 

If you look in your account's `Resource List` in the side panel:

![resource list](https://i.imgur.com/Dj4ziDf.png)

Are there whitespaces in your service name? Upon re-deployment, having whitespace will mess things up. If your service name for App ID has whitespace, for example `App ID-4343` then use the ellipsis on the right to `Rename` it to `AppID-4343` to correct this. 

![nospace](https://i.imgur.com/dwjELhH.png)

Then go to your sample code locally and look at your manifest.yml file. Does the service name with the word `alias` in it have any whitespace?

Lets go take off that empty space that will only make for errors in our terminal, who has always been by our side! 

Then follow the commands [here](https://tinyurl.com/yy2gk59k) and create that alias properly to bind to that service name. It should not spit out any errors now.

![yay](https://media0.giphy.com/media/l4pT3EOebQHolKkkU/giphy.gif?cid=790b76115cdc291d4347744e5166463a&rid=giphy.gif)

And one small note - this particular command in those instructions? 

`ibmcloud resource service-alias-create {ALIAS_NAME} --instance-name {SERVICE_INSTANCE_NAME}`

You can take off those curly brackets when you make this command for those names.

And if you mess up, you can delete the alias. To do this - you go into your IBM Cloud "Resource List" in your left-side navigation bar, find the instance of App ID with the word "alias" in the title of the service, click the ellipsis and select "Delete" in the drop-down.
<hr>
**Redirect**
 
The manifest.yml is necessary for deploying the app with your modifications from the command line. You can read about its purpose in regards to the platform in the [Cloud Foundry documentation](https://tinyurl.com/ya7d3fbw) if you would like to know more.

Lets go back to the App ID service home page on IBM Cloud.

We need to add the proper callback for your application.

Click on `Manage Authentication` in the service navigation bar.

![Auth manage](https://i.imgur.com/4cy4hyR.png)

And open the `Authentication Settings` tab.

![image for redirect](https://i.imgur.com/giIZAce.png)

Look at your application's name in the manifest.yml file. What is your domain name? 

Right now you probably have not routed this to being a custom domain, so let's use the `mybluemix.net` domain and add `/ibm/bluemix/appid/callback` to the end of that. 

You can add any other paths here too. In full, you are providing here the `nameofyourapp.mybluemix.net/ibm/bluemix/appid/callback` for that kosher callback.

**Cloud Directory**

Finally, now we can populate your Cloud Directory with users!

Make sure that your Cloud Directory is toggled `On` within the `Identity Providers` tab of the `Manage Authentication` management section.

![turn on](https://i.imgur.com/DCEA4EJ.png)


![user registry](https://i.imgur.com/Mhou8XR.png)

And add your users within the `Users` section!

<hr>

**Live**

Once you are ready to have a live link by configuration across these above steps, you can go into the root directory locally for your sample code app. Type the command `ibmcloud app push` to trigger the deployment of your application. 

This process will give you a link to the live application when the deployment process has completed.

![goodjob](https://i.imgur.com/5CwVima.png)

Try logging in with one of the emails/usernames to test it out.

There are a ton of great features to this service. Remember, you don't have to link your credit card here to make the service. But it will stop functioning per the 1000 free events occurring. 

I think this threshold would be the first upper limit hit unless you can add multiple users in a batch by the API. This is due to my testing the login making for 6 events in itself. But for a demo | PoC or the secure login of an app with light amounts of web traffic, this is a possible temporary free solution.

Hope you liked this tutorial. Thanks!

**Some Helpful Links**
* [App ID landing page](https://tinyurl.com/y663blec)
* [App ID video overview](https://tinyurl.com/yyl4fr5x)
* [API reference](https://tinyurl.com/y5ph6a28)
* [Securing Angular+Node.js Applications using App ID](https://tinyurl.com/yy4zx22e)

![pixel computer ftw bc i love it](https://media0.giphy.com/media/ao9DUiTKH60XS/giphy.gif?cid=790b76115cdc2e3c6b677859458c010f&rid=giphy.gif)