Join Identity Data from Okta with Postgres Using Hasura Remote Joins
Tags: remotejoins, remoteschemas, graphql, webdev
Published: 2019-12-23 06:47:52

---
title: Join Identity Data from Okta with Postgres Using Hasura Remote Joins
published: true
date: 2019-08-27 06:35:59 UTC
tags: remote joins, remote schemas, graphql, webdev
canonical_url: https://blog.hasura.io/join-identity-data-from-okta-with-postgres-using-hasura-remote-joins/
---

## TL;DR

Use Hasura Remote Joins to join data from your Postgres database with user data in Okta.

This post is a part of our [Remote Joins](https://blog.hasura.io/remote-joins-a-graphql-api-to-join-database-and-other-data-sources/) (available in preview) series. Remote Joins in Hasura allows you to join data across tables and remote data sources. You can sign up [here](https://hasura.io/remote-joins) if you'd like to be notified when we launch. Head to [the PR](https://github.com/hasura/graphql-engine/pull/2395) to check out more details, read the preview docs, and to try out a test Hasura image that you can spin up. Jump on our [discord](https://discord.gg/hasura) or comment on [github](https://github.com/hasura/graphql-engine) and let us know what you think!

![](https://blog.hasura.io/content/images/2019/08/okta-remote-joins.png)

[Okta](https://www.okta.com) is a complete access management platform that provides secure identity management with Single Sign-On, Multi-factor Authentication, Lifecycle Management (Provisioning), and more. In a typical app, Okta is the gateway for connecting the app, devices and users via APIs.

In this example, we will make use of [Okta's API](https://developer.okta.com/docs/reference/api/users/) to fetch user's data mapping it with the user id in Postgres.

Let's create a new table in Hasura called `users` with just `id` and `created_at` columns:

![](https://blog.hasura.io/content/images/2019/08/create-table-users.png)

## Adding Okta API as Remote Schema

To be able to query Okta data via Hasura, it needs to be added as a `Remote Schema` using the Hasura Console.

### Deploy Custom Resolver
[![](https://raw.githubusercontent.com/hasura/graphql-engine/master/community/boilerplates/auth-webhooks/nodejs-express/assets/deploy-glitch.png)](http://glitch.com/edit/#!/import/github/praveenweb/okta-remote-schema)

Add the following environment variables in the `.env` file on glitch.

```
OKTA_API_KEY=xxx
OKTA_DOMAIN=xxx
PORT=3000
```

- Get the Okta API Key by visting the [create an API Token docs](https://developer.okta.com/docs/guides/create-an-api-token/overview/)
- Set the API key as `OKTA_API_KEY` environment variable.
- Get the Okta Domain by visiting the following [find your domain docs](https://developer.okta.com/docs/guides/find-your-domain/overview/)
- We need to input the Okta Domain as `OKTA_DOMAIN` environment variable.
- This custom resolver is used to accept a user's id as an argument and will return a data pertaining to that user.

Get the GraphQL API Endpoint from Glitch and add it as remote schema.

![](https://blog.hasura.io/content/images/2019/08/okta-add-remote-schema.png)

Now let's add the Remote Relationship called `userInfo`

![](https://blog.hasura.io/content/images/2019/08/okta-remote-relationship.png)

Now let's create a user on Okta. For simplicity, we use the API directly instead of building a client app. Note that, you have to replace OKTA\_API\_KEY and OKTA\_DOMAIN with the values generated above. Additionally you have to add profile details in the body of the request.

```
curl -X POST \
  'https://hasura.okta.com/api/v1/users?activate=true' \
  -H 'Authorization: SSWS OKTA_API_KEY’ \
  -H 'Content-Type: application/json' \
  -H 'Host: OKTA_DOMAIN’ \
  -d '{
  "profile": {
    "firstName": "",
    "lastName": "",
    "email": "",
    "login": ""
  },
  "credentials": {
    "password" : { "value": "" }
  }
}'
```

Once a user is created on Okta, let's add the user id into Hasura's `users` table.

Now the GraphQL query to fetch this data in a single API call would look like the following:

```
query {
  users {
    id
    created_at
    userInfo {
      email
      firstName
      lastName
      status
    }
  }
}
```

Notice that, the nested query `userInfo` comes from Okta API and it will apply the filter of users.id = userInfo.id, there by only giving data about the current user in Okta.

Checkout our other posts in the [Remote Joins series](https://blog.hasura.io/tagged/remote-joins/).

Remote Joins opens up a lot of exciting usecases and these boilerplates are simple examples to explore the possibilities. You can fork the original example to extend the functionality for your app's requirements.