Создать сервис сброса пароля с помощью токена - PullRequest
0 голосов
/ 05 декабря 2018

Я хочу сбросить пароль с помощью токена.Я пытался реализовать это:

Компонент:

@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 User, если учетная запись найдена.

...