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

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

Hello @jared.modugno welcome to the community!

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:

Hope this helps!

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:

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);
  });
};
1 Like

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.

1 Like

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