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.

1.7 KB
Raw Blame
/* eslint-disable no-else-return */
import { AuthStrategy } from '.';
import { LOGGER } from '../../../../../../config/logging';
import { userService } from '../../../../user/services';

interface UserTokenData {
  logtoUser?: any;
  user?: {
    id: any;
    email: any;
    teamId: any;
  };
  userId?: any;
}

export default class DefaultUIAuth implements AuthStrategy {
  name = 'defaultUIAuth';

  async verifyToken(token: string) {
    const data: UserTokenData = {};

    if (!token) {
      return { error: 'Access token is required', data: null, success: false };
    }

    const userAuth = {
      sub: 'sub_1',
      name: 'Admin',
      picture: undefined,
      updated_at: Date.now(),
      username: null,
      created_at: Date.now(),
      email: '[email protected]',
      email_verified: true,
      primaryEmail: '[email protected]',
      avatar: undefined,
    };
    data.logtoUser = userAuth;
    const logtoUser = data.logtoUser;

    if (!logtoUser.primaryEmail) logtoUser.primaryEmail = logtoUser.email;
    if (!logtoUser.avatar) logtoUser.avatar = logtoUser.picture;
    // LOGGER.info(`User ${logtoUser.primaryEmail} is logging in (name: ${logtoUser.name})`);
    const user = await userService.findOrCreateUser({
      email: logtoUser.primaryEmail,
      name: logtoUser.name,
      avatar: logtoUser.avatar,
    }); //* check if user exists in our DB, if not, create one

    if (!user.teamId) {
      LOGGER.info(`User ${logtoUser.primaryEmail} is logging without a team`);
      return { error: 'User does not belong to a team', data: null, success: false };
    }

    data.user = {
      id: user.id,
      email: user.email,
      teamId: user.teamId,
    };
    data.userId = user.id;

    return { data, success: true };
  }
}