Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Erro ao logar na API : status: 401, statusText:

Boa noite! Eu estou problemas no método de autenticação da API. Os dado são enviados porém, no retorno, estou recebendo um erro:

Browser

error: message: "Authentication failed for user flavio" message: "Http failure response for http://localhost:3000/user/login: 401 Unauthorized" name: "HttpErrorResponse" ok: false status: 401 statusText: "Unauthorized" url: "http://localhost:3000/user/login"


API

Authentication failed for user flavio No token generated

####################################


Seguem os códigos fonte:

auth.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

const APIUrl = 'http://localhost:3000'
@Injectable({
    providedIn: 'root'
})
export class AuthService {

    constructor(private http: HttpClient) { }

    authenticate(userName: string, passrord: string) {

        return this.http.post(APIUrl + '/user/login', { userName, passrord });
    }
}

sign-in.component.ts

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { AuthService } from 'src/app/core/auth.service';

@Component({
    templateUrl: './sign-in.component.html',
    styleUrls: ['./sign-in.component.css']
})
export class SignInComponent implements OnInit {

    loginForm: FormGroup;

    constructor(
        private formBuilder: FormBuilder,
        private authService: AuthService) { }

    ngOnInit() {
        this.loginForm = this.formBuilder.group({
            userName: ['', Validators.required],
            password: ['', Validators.required]
        });
    }

    login() {
        const userName = this.loginForm.get('userName').value;
        const password = this.loginForm.get('password').value;        
        this.authService
            .authenticate(userName, password)
            .subscribe(
                () =>
                    console.log('Usuário autenticado.'),
                err => {
                    console.log("Erro", err)                    
                    this.loginForm.reset();
                }
            );
    }
}
1 resposta
solução!

Encontrei o erro! No meu auth.service.ts, no post para a API, estava passando a propriedade password digitada incorretamente (passrord ao invés de password).

authenticate(userName: string, password: string) {

        return this.http.post(APIUrl + '/user/login', { userName, password });
    }

Shame...

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software