Integrate Okta to Node, Express, and Vue
Tags: webdev, node, vue, tutorial
Published: 2021-03-11 18:12:00

Hey Dev's

In today's tutorial let us implement Okta authentication to a Node and Vue application. Our applications require user management, authentication, and API authorization at some stage. There are three main options for implementing authentication in a Node application.

- Configure your own authentication
- Use Passport.js
- Third-party service.

Depending on which strategy your application requires we will handle all and ensure we can configure both to our application. In a previous article, I discussed passport.js [https://dev.to/kevin_odongo35/integrate-passport-js-to-node-express-and-vue-19ao](https://dev.to/kevin_odongo35/integrate-passport-js-to-node-express-and-vue-19ao).

OpenID Connect (often referred to as OIDC) is the recommended way to handle user authentication and authorization in modern web applications. In the third-party provider example below, Okta will act as the OIDC authorization server that stores your user credentials. Okta recently acquired Auth0.

In the next tutorial, we will handle how to integrate AWS Cognito to Node and Vue applications and implementing server-side authentication.

As you all know the practical approach is always the best. 

Okta is an API service that allows you to create users, handle user authentication and authorization, etc. By using Okta, you avoid writing most of the authentication/authorization/user management logic. To continue below and implement Okta, you’ll need to create a [developer account](https://developer.okta.com/signup/thank-you/).

Okta has several samples that can get you up and running very quickly. We are going to handle two samples and with those two samples, you will be able to decide which approach you would want to use.

# Vue sample

- Go to the applications section

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gppsdq6woh2vy5a4yv6o.png)

- Add an application

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/43ienloxbq6mrsz3ohe5.png)

- Create a new app

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/335tfm8nh86iwe6rx1cg.png)

- Select Single Page App
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wmamw21orvky8u6vnako.png)

- Configure OpenID 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1i086qukkkst4vxn16ly.png)

- Once down in the application's dashboard download the sample app.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pexjix78v4v34uf4ldnp.png)

- Select Vue or whichever framework you would want to test with.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9bikyjzoi57iyjtqzqag.png)

- Here is the application structure.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cjlmjcubs7tuu23bxe68.png)

- Run the following commands.
```
cd okta-sample-js-vue
// install all dependencies
yarn install
```

- There are two login options
1. Custom login
2. Okta hosted login

We will test both to understand what each does

```
// custom login
yarn custom-login-server
```

- One last thing before you test the application. Add http://localhost:8080 to Trusted Origins.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t27evh2zoqup1odkcr80.png)

- Go API section, select Trusted Origins and add http://localhost:8080 

- Here is the homepage for the application.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f5g2daoxts3g9mtmu1bw.png)

- To test use the login details you used in creating your developer account.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ds6mqp0y8mj43gytktj.png)

```
// provide developer login details
username: 
password:
```

To Test Okta hosted login run the following command. With this, you will be redirected to okta login and redirected back to your application. 

```
yarn run okta-hosted-login-server
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/elihl06gp1sz6adqz4av.png)

When you click login you will be redirected to okta login.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/84bssfbx1j5qwwwhifsd.png)

That is all you have to do to test the Vue sample application. Go through the Github code and customize your own. Remember with this approach your authentication logic will be in the front-end.

# Node

For node application, we will do some changes we will use the web and single-page app. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kexilicgg54twt2ltyyu.png)

- Once you have done that download the node sample application.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jqu9hufmsox3v7jl3u5c.png)

- Here is the application structure.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f2d9rdjwwaujts1pmy2o.png)

- Install all dependencies

```
yarn install
```

The logic is the same with node the only difference is the authentication logic is happening in the backend. This is quite ideal.

#### custom login
```
// custom login
yarn custom-login-server
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vgw0t9dezgag3wqhtarb.png)

#### okta hosted login
```
// custom login
yarn okta-hosted-login-server
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yixbqmfhr1xg6gcqdayb.png)

Test the application using the username and password that you used to signup with. Go through the application structure and replicate the logic you want in your application between the custom-login and okta-hosted.


That is all you have to do to get started with Okta and familiarize yourself with it.


# CONCLUSION

I am assembling the following authentication methods into a single application where you can test a working application and choose which one you would want to use in your application. All the documentation of how to configure each with be available.

- Passport.js
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vjou5g4vtqngli5hfa37.png)
- Cognito
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4786aevhwjgk0grrb7j3.png)
- Auth0
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l9uhx5cgaudpmku906ny.png)
- Okta
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jk05q1p2wptqeu9d307j.png)

My focus will be integration to node, express, and Mongo DB. For email service, we will use AWS SES as well as SendGrid.

I hope this tutorial will assist someone who wanted to start with Okta integration


Thank you