For some reason I am having trouble accessing the storage service and setting storage properties with my custom auth service.
I have a login method that should hit my API, get a token, decode the token and store the user information via storage. This all works if I do this in the login promise but that seems like an anti-pattern to me.
I've imported the storage module...
import { Storage } from '@ionic/storage';
And injected into the constructor...
constructor(private http: Http, private storage: Storage) {
return this.http.post(this.LOGIN_URL, JSON.stringify(credentials), { headers: this.contentHeader })
.map(this.extractData)
.catch(this.handleError);
}
And then when I try to use it in the response from the http
service...
private extractData(res: Response) {
let body = res.json();
this.error = null;
this.user = body.user[0];
this.storage.ready().then(()=>{
this.storage.set('profile',this.user);
this.storage.set('token',body.token);
});
return body || {};
}
I get the error
Cannot read property 'set' of undefined
I can't seem to find online how it should work within a service differently from within a module.