POST with angle 5

2

So, I'm trying to make an app with angular 5 using auth with token. For that, in login, I need to make a post in my API (which I tested by postman and it's working), but I'm having a hard time doing this with the angular. My code is as follows:

login(username: string, password: string): Observable<boolean> {
    var headers = new Headers({
        "Content-Type": "application/json",
        "Accept": "application/json"
    });

    let postData = {
        grant_type: "password",
        client_id: 2,
        client_secret: "RGNmOzt7WQ8SdNiCcJKKDoYrsFqI2tudopFjOJU3",
        username: "[email protected]",
        password: "password",
        scope: ""
    }

    return this.http.post('http://localhost:8000/oauth/token', JSON.stringify(postData), {
        headers: headers
    })
    .map((response: Response) => {
        // login successful if there's a jwt token in the response
        let token = response.json() && response.json().token;
        if (token) {
            // set token property
            this.token = token;

            // store username and jwt token in local storage to keep user logged in between page refreshes
            localStorage.setItem('currentUser', JSON.stringify({ username: username, token: token }));

            // return true to indicate successful login
            return true;
        } else {
            // return false to indicate failed login
            return false;
        }
    });
}

As I am new to angle 5, I could not debug it right, it does not enter .map and I can not get any exception to understand what is wrong.

Could you help me?

    
asked by anonymous 31.01.2018 / 16:59

1 answer

2

The problem was in the CORS of my app. As soon as I put the authorization in cors it worked: D

    
31.01.2018 / 17:12