POST with angular 5

Asked

Viewed 1,194 times

2

so I’m trying to make an app with angular 5 using auth with token. For this, in the login, I need to do a post on my API (which I tested by Postman and is working), but I’m having trouble doing this with the angle. My code is this:

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;
        }
    });
}

Since I’m new at corner 5, I couldn’t debug it right, it doesn’t enter the . map and can’t get any Exception to understand what’s wrong.

Could you help me?

1 answer

2


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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.