8

I have a login service that performs an http request (to a php backend server) and returns a cookie. After login this cookie is needs to be used in all further requests done by the client.

loginservice:

   login(userCredentials:UserCredentials):Observable<any> {    
        this.request.ServiceType = "Connect"
        this.request.SessionId = "sessionlessrequest"
        this.request.Compression = "no"
        this.request.Parameters = userCredentials;
        let jsonRequest = JSON.stringify(this.request)
        return this.http.post("http://localhost/request.php", "data="+ jsonRequest,
            { headers: new Headers({
                'Content-Type': 'application/x-www-form-urlencoded'
            })
            }).map( (responseData) => {
            this.apiResponse = responseData.json() as ApiResponse
            if (!this.apiResponse.Error) {
                this.sessionData.next(this.apiResponse.Data as UserSessionData)
                this.sessionData.asObservable().share
            } else {
                console.log(this.apiResponse.ErrorMessage)
            }
            return null
        })

When doing this call, the cookie response is like this:
getcookie

I see that I get the session_id from session_start (on my php server)

I when doing the request :

searchExams(searchObject:SearchObject):Observable<any>{
        let headers = new Headers();
        headers.append('Content-Type', 'application/x-www-form-urlencoded',)
        let options = new RequestOptions({ headers: headers, withCredentials: true});
        this.request.ServiceType = ServiceType.SearchExams;
        this.request.SessionId = this.userSessionData.SessionId;
        this.request.Compression = "no"
        this.request.Parameters = searchObject;
        let jsonRequest = JSON.stringify(this.request)
        console.log(jsonRequest)
        return this.http.post("http://localhost/request.php", "data="+ jsonRequest, options
            ).map( (responseData) => {
            this.apiResponse = responseData.json() as ApiResponse
            if (!this.apiResponse.Error) {
....

I see that the request cookie is totally different from the one I got from the server. Moreover it is always the same PHPSESSID postrequest

Do I need to set the request cookie? How? What am I missing?

Thanks....

1

1 Answer 1

20
+50

Try this for the login:

(...)
return this.http.post("http://localhost/request.php", "data="+ jsonRequest,
    { withCredentials: true,
        headers: new Headers({
        'Content-Type': 'application/x-www-form-urlencoded'
    })
    }).map(...)

And the other requests:

(...)
return this.http.post("http://localhost/request.php", "data="+ jsonRequest,
                      {withCredentials: true}).map(...)

Basically, just use withCredentials option set to true.

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

2 Comments

Thank you! Indeed the answer.
Works for me on ionic-angular 3.9.2. Thanks so much! This question is asked and not answered several other places on the web. I will point those questions to this answer.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.