使用令牌创建重置密码服务

时间:2018-12-05 10:55:31

标签: spring angular typescript angular6 spring-rest

我想使用令牌重设密码。我试图实现这一点:

组件:

@Component({
  selector: 'app-reset-password-edit',
  templateUrl: './reset-password-edit.component.html',
  styleUrls: ['./reset-password-edit.component.scss']
})
export class ResetPasswordEditComponent implements OnInit {

  user: UserReset = new UserReset(null, null, null, null, null, null);

  passwordMismatch = false;
  notFound = false;

  constructor(private resetService: PasswordResetService,
              private route: ActivatedRoute) {
  }

  ngOnInit() {
    if (this.route.snapshot.queryParamMap.has('reset_password_token')) {
      this.user.reset_password_token = this.route.snapshot.queryParams['reset_password_token'];
      this.resetService.sendToken(this.route.snapshot.queryParams['reset_password_token'])
        .subscribe((user) => {
            this.user = user;
          },
          error => this.handleError(error));
    }
  }

  reset() {

    if (this.user.password != this.user.confirmPassword) {
      this.passwordMismatch = true;
      return;
    }

    this.resetService.resetPassword(this.user).subscribe(() => {

      },
      error => this.handleError(error));
  }

  handleError(error) {
    console.log(error);
    switch (error.error) {
      case 'INVALID_TOKEN':

        break;
    }

    switch (error.status) {
      case 404:
        // redirect here to login page
        break;
    }
  }
}

服务:

@Injectable({
  providedIn: 'root'
})
export class PasswordResetService {

  constructor(private http: HttpClient) { }

  sendToken(token: String): Observable<UserReset> {
    return this.http.post<UserReset>(environment.api.urls.users.token, token);
  }
}

对象:

export class UserReset {
  constructor(
    public id: string,
    public name: string,
    public email: string,
    public password: string,
    public reset_password_token: string
  ) {}
}

端点:

@PostMapping("{token}")
    public ResponseEntity<?> token(@PathVariable String token) {
        return userRepository.findByResetPasswordToken(token).map(user -> {         
            PasswordResetDTO obj = new PasswordResetDTO();
            obj.setId(user.getId());
            obj.setName(user.getLogin());

            return ok(obj);

        }).orElseGet(() -> notFound().build());
    }

Java对象:

public class PasswordResetDTO {
    private Integer id;
    private String name;
    private String email;
    private String password;
    private String confirmPassword;
    private String reset_password_token;
    .....
}

我收到错误消息:

Ambiguous handler methods mapped for '/api/users/token': {public org.springframework.http.ResponseEntity backend.restapi.UserController.save(java.lang.String,org.datalis.admin.backend.restapi.dto.UserDTO), public org.springframework.http.ResponseEntity org.datalis.admin.backend.restapi.UserController.token(java.lang.String)}

将长字符串发送到Spring端点并返回到Angular填充对象的正确方法是什么?

在我的情况下,如果要找到帐户,我想发送令牌并返回到Angular User对象。

0 个答案:

没有答案