Swoop offers some basic account management via webhooks. By implementing a simple API method to respond to a Swoop webhook, Swoop can handle "account not found" and "create account" flows for your web app.

If you register your webhook with the Swoop Dashboard, Swoop will notify your webhook when a new user authenticates. You can use this callback to tell Swoop whether or not the user exists inside of your application.

If the user does exist, then Swoop will continue the application flow and redirect back to your application. If they don't, the user will be prompted to either try a different email or create a new account.

Upon selecting the "Create New Account" option, Swoop will hit your webhook endpoint again with the basic registration information (email, uid) and you will be responsible for inserting them into your database.

The entire webhook API is optional and will only be presented to your user if you register an endpoint in the Swoop Dashboard.

Below are some examples of responding to a Swoop webhook.

const webhook = async(req, res) => {
  let { type}  = req.body;

  if (type === 'account.exists') {
    let { email}  = req.body.data;
    const user = await userService.getUserByEmail(email);
    if (user) {
      return res.status(httpStatus.OK).send({ 'account.exists': true });
    }
    return res.status(httpStatus.OK).send({ 'account.exists': false });
  }

  if (type === 'account.create') {
    let { email}  = req.body.data;
    let user = await userService.getUserByEmail(email);
    if (!user) {
      let userBody = {
        email,
        swoopId: req.body.id,
        password: "abc12345678",
        name: email
      }
      user = await userService.createUser(userBody);
      return res.status(httpStatus.OK).send({ 'account.create': true });
    }
    return res.status(httpStatus.OK).send({ 'account.create': false });
  }

  return res.status(httpStatus.NOT_FOUND);
};