# Can I connect to a database in the Post-login Flows actions script?

**URL:** <https://community.auth0.com/t/can-i-connect-to-a-database-in-the-post-login-flows-actions-script/109811>\
**Category:** Get Help\
**Tags:** identifier-first, login-experience\
**Created:** [June 22, 2023, 7:54pm UTC](https://community.auth0.com/t/can-i-connect-to-a-database-in-the-post-login-flows-actions-script/109811 "2023-06-22T19:54:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jared.modugno](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jared.modugno/32/71057_2.png) [@jared.modugno](https://community.auth0.com/u/jared.modugno)\
**Post date:** [June 22, 2023, 7:54pm UTC](https://community.auth0.com/t/can-i-connect-to-a-database-in-the-post-login-flows-actions-script/109811/1 "2023-06-22T19:54:03Z")

</div>

Hi, I need to add metadata to a user after they login with an identity provider (or an Enterprise Connection). The metadata “data” is on my database, is there a way I can utilize the Post-login script, connect to my sql database, and add the metadata?  
I’m able to add this metadata in the login script when a user doesn’t use an Enterprise connection

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [June 22, 2023, 9:34pm UTC](https://community.auth0.com/t/can-i-connect-to-a-database-in-the-post-login-flows-actions-script/109811/3 "2023-06-22T21:34:21Z")

</div>

Hello @jared.modugno welcome to the community!

> [@jared.modugno](#):
>
> The metadata “data” is on my database, is there a way I can utilize the Post-login script, connect to my sql database, and add the metadata?

You can definitely connect to your own API/database from within a post-login action - You will need to write the necessary action code (Hopefully a npm package available for your DB) that connects to your database and performs the desired operations. The following thread may be of use as well:

> [@How do I call my API from an Action?](https://community.auth0.com/t/how-do-i-call-my-api-from-an-action/69041):
>
> Hey, So I’m in the process of trying to add a user to my database using an API call from a “Post User Registration” Action. I have followed this blog post here: and seem to have everything looking good. On top of that when I run the test it says the action was successful. The only problem is that my API is not being called at all even though I have triple checked that the URL is correct. Here is the code I’m working with: import axios from "axios"; exports.onExecutePostUserRegistration = …

Hope this helps!

---

<div class="post-metadata">

**Author:** ![jared.modugno](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jared.modugno/32/71057_2.png) [@jared.modugno](https://community.auth0.com/u/jared.modugno)\
**Post date:** [June 26, 2023, 1:14pm UTC](https://community.auth0.com/t/can-i-connect-to-a-database-in-the-post-login-flows-actions-script/109811/4 "2023-06-26T13:14:38Z")

</div>

Thanks for the link. Can I use my database connection instead of an API? I’ve been trying this but it doesn’t seem to be running in my post-login action. (I have something similar running in the login script). This is my code, with the connecting info redacted:

```auto
exports.onExecutePostLogin = async (event, api) => {
  const sqlserver = require('tedious@1.11.0');
  const Connection = sqlserver.Connection;
  const Request = sqlserver.Request;
  const TYPES = sqlserver.Types;

  const connection = new Connection({
      server: ********** ,
      userName: ********** ,
      password: ********** ,
      options: {
          instanceName: ********** ,
          rowCollectionOnRequestCompletion: true,
          useColumnNames: true
      },
  });

  const query =
  `select Institution from ************************* email = @Email`;

  connection.on('connect', function (err) {
    api.accessToken.setCustomClaim(`testingSqlConnection`, `a regular string`);
      if (err) return err;

      const request = new Request(query, function (err, rowCount, rows) {
          if (err || rowCount < 1)
              return err;

          const firstRow = rows[0];

          var institution = firstRow.Institution.value;
          api.accessToken.setCustomClaim(`InstitutionTestingClaim`, institution);
      });
	  
      request.addParameter('Email', TYPES.VarChar, event.user.email);
      connection.execSql(request);
  });
};

```

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [June 26, 2023, 8:34pm UTC](https://community.auth0.com/t/can-i-connect-to-a-database-in-the-post-login-flows-actions-script/109811/5 "2023-06-26T20:34:25Z")

</div>

I don’t believe this is possible due to the restricted nature of the Actions environment - After a bit of research internally, it seems a minimal wrapper API is recommended.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [July 10, 2023, 8:34pm UTC](https://community.auth0.com/t/can-i-connect-to-a-database-in-the-post-login-flows-actions-script/109811/6 "2023-07-10T20:34:34Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
