不能为空-密码-回送4-JWT身份验证

问题描述

当我想使用JWT身份验证和此json格式在环回API Explorer上进行注册时:

{
"id": "string","nom": "string","prenom": "string","email": "string","sexe": true,"dateNaissance": "string","password": "strifsvng"
}

我收到了该错误消息:

{
  "error": {
"statusCode": 422,"name": "ValidationError","message": "L'instance `User` n'est pas valide. Détails : `password` can't be blank (value: undefined).","details": {
  "context": "User","codes": {
    "password": [
      "presence"
    ]
  },"messages": {
    "password": [
      "can't be blank"
    ]
  }
 }
 }
 }

这是我使用的documentation's loopback链接

您可以在此处看到用户模式:

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

@model()
export class User extends Entity {
   @property({
    type: 'number',id: true,generated: true,})
  id?: number;

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

  @property({
     type: 'string',})
   prenom: string;

   @property({
     type: 'string',})
   dateNaissance: string;

  @property({
     type: 'string',})
   sexe: string;

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

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


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

  export interface UserRelations {
    // describe navigational properties here
  }

  export type UserWithRelations = User & UserRelations;

用户控制器:


// import {inject} from '@loopback/core';
import {inject} from '@loopback/core';
import {
  Credentials,MyUserService,TokenServiceBindings,User,UserRepository,UserServiceBindings,} from '@loopback/authentication-jwt';
import {authenticate,TokenService} from '@loopback/authentication';
import {model,property,repository} from '@loopback/repository';
import {get,getModelSchemaRef,post,requestBody} from '@loopback/rest';
import {SecurityBindings,securityId,UserProfile} from '@loopback/security';
import {genSalt,hash} from 'bcryptjs';
import _ from 'lodash';

@model()
export class NewUserRequest extends User {
  @property({
    type: 'string',})
  password: string;
}

const CredentialsSchema = {
  type: 'object',required: ['email','password'],properties: {
    email: {
      type: 'string',format: 'email',},password: {
      type: 'string',minLength: 8,};

export const CredentialsRequestBody = {
  description: 'The input of login function',content: {
    'application/json': {schema: CredentialsSchema},};

export class UserController {
  constructor(
    @inject(TokenServiceBindings.TOKEN_SERVICE)
    public jwtService: TokenService,@inject(UserServiceBindings.USER_SERVICE)
    public userService: MyUserService,@inject(SecurityBindings.USER,{optional: true})
    public user: UserProfile,@repository(UserRepository) protected userRepository: UserRepository,) {}

  @post('/users/login',{
      responses: {
        '200': {
          description: 'Token',content: {
            'application/json': {
              schema: {
                type: 'object',properties: {
                  token: {
                    type: 'string',})
    async login(
      @requestBody(CredentialsRequestBody) credentials: Credentials,): Promise<{token: string}> {
      // ensure the user exists,and the password is correct
      const user = await this.userService.verifyCredentials(credentials);
      // convert a User object into a UserProfile object (reduced set of properties)
      const userProfile = this.userService.convertToUserProfile(user);

      // create a JSON Web Token based on the user profile
      const token = await this.jwtService.generatetoken(userProfile);
      return {token};
    }

    @authenticate('jwt')
    @get('/whoAmI',{
      responses: {
        '200': {
          description: '',schema: {
            type: 'string',})
    async whoAmI(
      @inject(SecurityBindings.USER)
      currentUserProfile: UserProfile,): Promise<string> {
      return currentUserProfile[securityId];
    }

    @post('/signup',{
      responses: {
        '200': {
          description: 'User',content: {
            'application/json': {
              schema: {
                'x-ts-type': User,})
    async signUp(
      @requestBody({
        content: {
          'application/json': {
            schema: getModelSchemaRef(NewUserRequest,{
              title: 'NewUser',}),})
      newUserRequest: NewUserRequest,): Promise<User> {
      const password = await hash(newUserRequest.password,await genSalt());
      const savedUser = await this.userRepository.create(
        _.omit(newUserRequest,'password'),);

      await this.userRepository.userCredentials(savedUser.id).create({password});

      return savedUser;
    }
  }

我在密码中写一些东西时,我不知道为什么会出错。

预先感谢您:)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...