html response instead json

Discussão em 'Angular' iniciado por Roxana, Fevereiro 13, 2025.

  1. Roxana

    Roxana Guest

    I have this error:
    login.component.ts:27 ERROR

    1. HttpErrorResponse {headers: _HttpHeaders, status: 200, statusText: 'OK', url: 'http://localhost:4200/authenticate', ok: false, …}

    Zone - XMLHttpRequest.addEventListener:loadlogin@login.component.ts:27LoginComponent_Template_button_click_12_listener@login.component.html:14Zone - HTMLButtonElement.addEventListener:clickLoginComponent_Template@login.component.html:14Zone - HTMLButtonElement.addEventListener:clickGetAllUsersComponent_Template@get-all-users.component.html:2Promise.then(anonymous)

    I understood that it is because I return an html format instead of json for a login page.

    i have this in angular:

    constructor(private http: HttpClient) { }

    // Metodă pentru autentificare
    login(credentials: { email: string; parola: string }) {
    return this.http.post('/authenticate', credentials, { withCredentials: true });
    }
    }


    in intellij i have 3 classes about login: SecurityConfig,CustomUserDetails and Custom UserDetaillsService.

    in usercontroller i have:

    @GetMapping("/authenticate")
    public ResponseEntity<String> authenticate() {
    return ResponseEntity.ok("Autentificare reușită!");
    }


    in userDetailsService i have:

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    User user = userRepository.findByEmail(username)
    .orElseThrow(() -> new UsernameNotFoundException("User or password not found"));

    return new CustomUserDetails(user.getEmail(),
    user.getParola(),
    authorities(),
    user.getPrenume(),
    user.getNume(),
    user.getSex(),
    user.getData_nasterii(),
    user.getNumar_telefon(),
    user.getTara());
    }


    public Collection<? extends GrantedAuthority> authorities() {
    return Arrays.asList(new SimpleGrantedAuthority("USER"));

    }


    i put the code i think is important.

    I want to make the login work. It's my first project and I have a lot of trouble, but this put me down.

    Continue reading...

Compartilhe esta Página