Angular 2 – Building an app to demonstrate Oauth 2.

Hi Friends,

Hope you are all well. This post is a sequel to my previous post on building an oauth 2 server using nodeJS. If you hadn’t read that yet kindly click here.

Now that we have got the back-end up. We will focus on a front-end app using Angular 2.

First of all Angular 2 released their new RC5 version recently. Let’s focus on converting our rc.4 app into a rc.5 one.

A screencast of this part of the post

 

The complete code of this post – here.

Scaffold out an app using the angular-cli using the below command.

Once the app is created open it up in an editor of your choice and then open the package.json file and modify the dependencies as shown below.

run npm install once again to get the dependencies installed.

The breaking change between RC.4 and RC.5 versions is that the latter supports NgModules, a concept that allows you to write different pieces of functionality in separate modules and then bootstrap them in one file to run through the app.

Create an app.module.ts file in your app directory and type the below code into it.

As you can see, we have imported the BrowserModule, HttpModule, RouterModule, FormsModule into our app. This is similar to us importing the ROUTER_DIRECTIVES, HTTP_PROVIDERS etc., in the previous rc versions.

Now open main.ts file and modify the code as shown below.

We are just bootstrapping the AppModule here.

Now let’s get started with the actual app.

If you remember, in our Oauth flow we had 2 parts. The first one would be to register a client in the developer portal of the site and then second one would be to use the client id and secret in our apps.

So let’s first do the first part. Let’s quickly wire-up a few components and then a few routes to navigate through these components.

create a new file called routes.ts and add the below code in it.

Create another file called authmanager.ts and add the below code.

This part of the code is simply for authentication and I have already discussed a lot about authentication in my post here.

Now generate a new component called login component using the below command.

Open up login.component.html and add the below code in it.

Now open up login.component.ts and add the below code into it.

The code is pretty much self explanatory. Let’s move on and create the auth service.

Create it using the below command.

Now we have generated an auth service and let us use this to login.

Now this code is pretty much similar to the one which we saw previously in my post on Authentication using the new Router.

Let’s move on.

Let’s create a landing page or a dashboard for the user. Use the below command to create a new component.

Now open up dashboard.component.html and add the below code.

Now open up dashboard.component.ts and add the below code.

The next thing we will do is create an interface that allows us to add a client.

This will again be a simple crud system. First create a separate component called addclient using the below command.

Now let’s just use this to create a system that allows us to add a client.

Open up addclient.component.html and add the below code.

Open up addclient.component.ts and add the below code.

Open up auth.service.ts and add the below code.

As I mentioned before this is just code to add a client to our clients collection in the db. Note that we are sending the userId here along with the request. So if you are not logged in then you won’t be able to fill in this field.

Okay. Now this concludes the first part of our application. i.e the one wherein you add a client to the developer portal. This provision allows the user to do that.

Suppose the user has added a client successfully. He would have received the app id and secret id.

Now you could host this part of the app like a developer portal or something wherein you can tell the users that they could register their apps.

Part – 2:

From a user’s perspective.

A screencast of this part.

Now let us assume that a user has registered his app with the client and has an appid and a secret which he would like to use in his app.

For the sake of simplicity I will use the same project that I used above.

In the dashboard page, I am going to simply create a button called authorize user.

open up dashboard.component.html and add the below code.

Note that i have just commented out the Add client button.

This is becoz we are writing the app from the user perspective now. So the user has added a client and has the appid & secret now.

Open up dashboard.component.ts and add the below method as well.

Now open up auth.service.ts and modify it as shown below. The final auth.service.ts would look like this.

Okay, that was a bit long.

We have three new methods namely authorizeuser(), getAccessToken() and addbook().

The authorizeuser() method just opens the authorization url in a new window. If the user grants permission then it returns a code and closes.

This code is then sent in a request to be exchanged for an access token which is done by the getAccessToken() method.

Now once you have the token you can use it for making authorized requests.

Let’s just try creating an interface for adding a book.

Create a new book component using the below command.

Open up book.component.html and add the below code.

Open up book.componet.ts and add the below code.

As you can see, this is yet another CRUD system for adding a book, but please note that we send the accessToken along with the request in the addbook() method in the auth.service.

Don’t forget to add routes to all this components. The final routes.ts file would look as shown below.

That’s it guys.

Now if you everything goes fine you would have a fully functional Oauth 2 backend server.

How this is implemented in real-time ?

The backend nodejs server is hosted separately. The developer portal is hosted in a domain like developer.<yourcompanyname>.com

Then the users can build their own apps following the instructions as above.

If you find it confusing; kindly watch the video once. I believe I have explained it a bit clearly there.

Hope this helped you guys. Kindly share this with your friends and help them too.

Thanks for reading. Peace.. :)