Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

1023 B
Raw Blame
import { Logger } from '@smythos/sre';

import { mwUserAPI } from '@core/services/smythAPIReq';

const console = Logger('(Debugger) Middleware: Team Access Check');

export async function teamAccessCheck(req, res, next) {
  const teamId = req.params.teamId;
  const accessToken = req.header('Authorization') ? req.header('Authorization').split(' ')[1] : null;

  if (!accessToken) {
    return res.status(401).send({ error: 'Unauthorized' });
  }

  if (!teamId) {
    console.error('Team ID is required');
    return res.status(400).send({ error: 'Team ID is required' });
  }

  try {
    const result = await mwUserAPI.get(`/teams/me`, {
      headers: {
        Authorization: `Bearer ${accessToken}`,
        'x-smyth-team-id': teamId,
      },
    });

    const team = result?.data?.team || {};

    if (teamId !== team?.id) {
      return res.status(401).send({ error: 'Unauthorized' });
    }

    next();
  } catch (err) {
    console.error(err);
    return res.status(401).send({ error: 'Unauthorized' });
  }
}