Login cookie never replaced the 1st time?

Hi, I have this code :slight_smile:

login(mail: string, password: string) {

    this.loadingCtrl.create({
        message: "Authenticating..."
    }).then((overlay)=> {
        this.loading = overlay;
        this.loading.present();
    });    
    
    return this.httpClient.post(this.server + '/login?_format=json',
        JSON.stringify({
            name: name,
            pass: password,
        }),
        {headers: { 'Content-Type': 'application/json' }}
    ).subscribe(
        data => {

            console.log(data);
            this.storage.ready().then(()=>{
                this.storage.set('userdata',data);
                this.storage.get('userdata').then(value=>{
                    console.log(value);
                });
            });
        },
        error => {
            console.log(error);
            this.loading.dismiss();
            if(error.error['message']) {
                this.alertService.presentToast(error.error['message']);
            }
        }
    );
} 

but somehow the cookie was never replaced, it is always using the 1st time cookie and when I refresh the localhost:8100 then it will replaced the cookie, but this shouldnt work this way I believe, am I missing any steps?

Where is the cookie though?

this is one : this.storage.set(‘userdata’,data);

It ain’t a cookie though. It’s a piece of data stored in ionic storage. From your code, data received and being set looks fine to me.

but somehow the cookie was never replaced, it is always using the 1st time cookie and when I refresh the localhost:8100 then it will replaced the cookie

I think this code comes from somewhere else. Are you checking for username somewhere else too, maybe in your app.component.ts and then redirecting user to login page accordingly.

its only that code, but somehow the storage are using the old one, when I want to replace it, after refresh then it loads the new storage which is strange somehow…

In that case just do this.storage.clear(); before your set it from the API response.

// assuming you have an instance variable called response_data(if not, create one)

return this.httpClient.post(this.server + '/login?_format=json',
        JSON.stringify({
            name: name,
            pass: password,
        }),
        {headers: { 'Content-Type': 'application/json' }}
    ).subscribe(
        data => {
           
            this.response_data = data;
           
            this.storage.clear().then(()=>{
                this.storage.set('userdata', this.response_data);
                this.storage.get('userdata').then(value=>{
                    console.log(value);
                });
            });
        },
        error => {
            console.log(error);
            this.loading.dismiss();
            if(error.error['message']) {
                this.alertService.presentToast(error.error['message']);
            }
        }
    );

you are right turns out there are storage being write and that causes the issue. thanks once again!

1 Like