0

I am trying to implement verify account endpoint in loopback4 thorugh mailgun, since I am new in loopback4 and typescript in general, I am not sure if I am doing the right way. I want to retype the following code in the picture for loopback.I have already done with saving active flag in database and generating secretToken at signing up.

enter image description here

My code in loopback

@post('/users/verify')
  async verify(
    @requestBody(VerifyRequestBody) userData: User
  ): Promise<{ userData: object }> {
    try {
      const foundUser = await this.userRepository.findOne({
        where: {
          secretToken: userData.secretToken,
        }
      })
      if (!foundUser) {
        throw new HttpErrors.Forbidden(`No user found`)
      }

      foundUser.active = true;
      foundUser.secretToken = ''
      const savedUser = await this.userRepository.create(foundUser);

    } catch (err) {
      return err
    }
  }

 User model, I am using MongoDB

import { Entity, model, property } from '@loopback/repository';

@model({ settings: {} })
export class User extends Entity {
  @property({
    type: 'string',
    id: true,
  })
  id: string;

  @property({
    type: 'string',
    required: true,
  })
  email: string;

  @property({
    type: 'string',
    required: true,
  })
  password: string;

  @property({
    type: 'string',
    required: true,
  })
  firstName: string;

  @property({
    type: 'string',
    required: true,
  })
  lastName: string;

  @property({
    type: 'string',
    required: false
  })
  secretToken: string;

  @property({
    type: 'boolean',
    required: false,
    default: false
  })
  active: boolean;

  @property.array(String)
  permissions: String[]


  constructor(data?: Partial<User>) {
    super(data);
  }
}

export interface UserRelations {
  // describe navigational properties here
}

export type UserWithRelations = User & UserRelations;

User repository

import { DefaultCrudRepository } from '@loopback/repository';
import { User, UserRelations } from '../models';
import { MongoDsDataSource } from '../datasources';
import { inject } from '@loopback/core';

export type Credentials = {
  email: string,
  password: string,
  active: boolean
}

export type Verify = {
  secretToken: string
}

export class UserRepository extends DefaultCrudRepository<
  User,
  typeof User.prototype.id,
  UserRelations
  > {
  constructor(@inject('datasources.mongoDS') dataSource: MongoDsDataSource) {
    super(User, dataSource);
  }
}
NZMAI
  • 536
  • 5
  • 27
  • LB4 has a built-in [authentication](https://loopback.io/doc/en/lb4/Loopback-component-authentication.html) and [authorization](https://loopback.io/doc/en/lb4/Loopback-component-authorization.html), please refer to it. – Zhikai Xiong Sep 24 '19 at 11:06
  • Does it have an email verification feature like mailgun? – NZMAI Sep 24 '19 at 23:40
  • The answer is no .But I think it's better to put the auth logic of `mailgun.js` into the built-in authentication and authorization, and it has a lot of extension methods. For example: customize a strategy and then write the relevant logic inside, then you can use it with decorator. – Zhikai Xiong Sep 25 '19 at 03:43

0 Answers0