1

I want to make a REST call in Angular which should return a JWT (which is fake) in response. At least the console logging should be output, at best "localStorage" should be filled. The call is actually sent and the REST interface answers. Here is a log from Wireshark. Only the "console.log (" XXXXXXXX ");" is written to the console. Why is the logging not written?

SOLUTION: I passed an incorrect JSON object.

{"id" : "user.id", "username" : "user.username", "firstName" : "user.firstName", "lastName" : "user.lastName", "token" : "fake-jwt-token" }

Failure beforehand:

Angular:

login(username: string, password: string) {console.log("XXXXXXXX");
    return this.http.post<any>(`http://localhost/api/v1/users/authenticate`, "{username=\"" + username + "\",password=\"" + password +"\"}")//return this.http.post<any>(`${config.apiUrl}/users/authenticate`, { username, password })
        .pipe(map(user => {console.log(user);
            // login successful if there's a jwt token in the response
            if (user && user.token) {
                console.log("true :)");
                // store user details and jwt token in local storage to keep user logged in between page refreshes
                localStorage.setItem('currentUser', JSON.stringify(user));
                this.currentUserSubject.next(user);
            } else {
                console.log("false :(")
            }

            return user;
        }));
}

Wireshark:

POST /api/v1/users/authenticate HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 44
Accept: application/json, text/plain, */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36
Content-Type: text/plain
Sec-Fetch-Site: same-site
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: http://localhost:4200/login
Accept-Encoding: gzip, deflate, br
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7

{username="user1",password="secretpassword"}HTTP/1.1 200 OK
Date: Fri, 26 Jun 2020 11:29:20 GMT
Server: Apache/2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.4.2
X-Powered-By: PHP/7.4.2
Access-Control-Allow-Methods: POST
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With
Content-Length: 124
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/json; charset=UTF-8

{id: "user.id", username: "user.username", firstName: "user.firstName", lastName: "user.lastName", token: 'fake-jwt-token' }
0

2 Answers 2

0

Are you calling the login with the subscribe method? e.g. login().subscribe()?

Sign up to request clarification or add additional context in comments.

Comments

0

post api for login is missing its port(4200):

return this.http.post<any>(http://localhost:4200/api/v1/users/authenticate, { username: username, password: password })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.