Hi,
How do i use this provider to store a jwt token…
user.ts
constructor(public http: Http, public api: Api, public settings: Settings) {
}
loginJWT(account: any) {
let endpoint = 'account/' + account.id + '/jwt/';
let seq = this.api.post(endpoint, {}).share();
seq.map(res => res.json()).subscribe(res => {
console.log(res);
if (res.success) {
console.log(res.data);
this.settings.setValue("user", account);
this.settings.setValue("jwt", res.data);
console.log("in");
this._loggedIn(res);
}
}, err => {
});
return seq;
}
error: “this.setting is undefined”
settings.ts
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
/**
* A simple settings/config class for storing key/value pairs with persistence.
*/
@Injectable()
export class Settings {
private SETTINGS_KEY: string = '_settings';
settings: any;
_defaults: any;
_readyPromise: Promise<any>;
constructor(public storage: Storage, defaults: any) {
this._defaults = defaults;
}
load() {
return this.storage.get(this.SETTINGS_KEY).then((value) => {
if (value) {
this.settings = value;
return this._mergeDefaults(this._defaults);
} else {
return this.setAll(this._defaults).then((val) => {
this.settings = val;
})
}
});
}
_mergeDefaults(defaults: any) {
for (let k in defaults) {
if (!(k in this.settings)) {
this.settings[k] = defaults[k];
}
}
return this.setAll(this.settings);
}
merge(settings: any) {
for (let k in settings) {
this.settings[k] = settings[k];
}
return this.save();
}
setValue(key: string, value: any) {
this.settings[key] = value;
return this.storage.set(this.SETTINGS_KEY, this.settings);
}
setAll(value: any) {
return this.storage.set(this.SETTINGS_KEY, value);
}
getValue(key: string) {
return this.storage.get(this.SETTINGS_KEY)
.then(settings => {
return settings[key];
});
}
save() {
return this.setAll(this.settings);
}
get allSettings() {
return this.settings;
}
}