# AuthService.signup custom logic

```ts
/* ------------------------- Start custom logic here ------------------------ */
/**
 * Validate the request body
 */

  authValidators.validateSignupBody(body);


const { email } = body.data;
const oldUserDoc = await this.userModel.findOne({ email }).exec();

if (oldUserDoc) {
  throw new exceptions.ValueAlreadyExistsException('Email already exists', {
    email: {
      value: email,
      message: 'Email already exists'
    }
  });
}

const authManager = new AuthManager();
const { salt, password } = await authManager.hashPassword(body.data.password);
const tracking_id = await createTrackingId('user', this.userModel as any);
const userObject: Partial<EntityAlias> = {
  profile: {
    first_name: body.data.meta?.first_name || '',
    family_name: body.data.meta?.family_name || '',
    full_name: buildUserFullName(body.data.meta),
    sex: body.data.meta?.sex,
    photo: {
      id: '',
      url: ''
    },
    phones: [],
    website: body.data.meta?.website,
    address: body.data.meta?.address,
  },
  attributes: {
  },
  permissions: [],
  email: body.data.email,
  app: authData?.current?.app?._id || null,
  role: body.data.meta?.role || 'user',
  tracking_id,
  salt,
  password
}
const doc = await this.userModel.create({
  ...userObject
} as any);
this.logger.silly('Generating JWT');
const payload: Jsm.Core.Security.JWTUserAuthPayload = {
  _id: doc._id.toString(),
  tracking_id: doc.tracking_id,
  role: doc.role,
  space: 'default'
};
const token = authManager.generateToken(payload, 'default', false, config.security.jwt);
const refresh_token = authManager.generateToken(payload, 'default', true, config.security.jwt);

if (!doc) {
  throw new exceptions.InternalServerError('User cannot be created');
}

// dispatch event
this.eventDispatcher.dispatch<Jsm.Core.Auth.Events.Payloads.UserSignedUp>(authEvents.userSignedUp, {
  data: mapDocumentToExposed(doc, {
    omitAuthMeta: false
  })
});

// return result
result = {
  data: {
    user: mapDocumentToExposed(doc),
    token,
    refresh_token,
  },
};
/* ------------------------ end of custom logic here ------------------------ */
```

