验证帐户端点是否处于环回

时间:2019-09-24 07:04:02

标签: mailgun email-verification loopback4

我正在尝试在loopback4 thorugh mailgun中实现验证帐户端点,因为我通常是loopback4和Typescript的新手,所以我不确定自己的做法是否正确。我想在图片中重新输入以下代码以进行环回。我已经完成了将活动标志保存在数据库中并在注册时生成secretToken的操作。

enter image description here

我的回送代码

@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;

用户存储库

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);
  }
}

0 个答案:

没有答案