workos / workos-node

Official Node SDK for interacting with the WorkOS API
https://workos.com/docs/sdk/node
MIT License
120 stars 30 forks source link

feat: Add actions support #1161

Closed faroceann closed 5 days ago

faroceann commented 1 week ago

Description

Introduce new methods for Actions:

Example usage:

app.post("/registration-action", async (req, res) => {
  const context = req.body;

  // Verify incoming workos-signature 
  try {
    await workos.actions.verifyHeader({
      payload: context,
      sigHeader: req.headers["workos-signature"] as string,
      secret: env.ACTIONS_SECRET,
    });
  } catch (err) {
    return res.status(400).json({ error: "Invalid signature" });
  }

  let verdict: "allow" | "deny";

  if (context.user_data.email.split("@")[1] === "gmail.com") {
    verdict = "deny";
  } else {
    verdict = "allow";
  }

  // Sign the outgoing response using the actions secret 
  const response = await workos.actions.signResponse({
    type: "user_registration",
    verdict,
    ...(verdict === "deny" && {
      errorMessage: "Please use a work email address",
    }),
    secret: env.ACTIONS_SECRET,
  });

  return res.json(response);
});

Documentation

Does this require changes to the WorkOS Docs? E.g. the API Reference or code snippets need updates.

[x] Yes

If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.