Ionic Starter Template - How to use setting provider to store jwt


How do i use this provider to store a jwt token…


 constructor(public http: Http, public api: Api, public settings: Settings) {

    loginJWT(account: any) {

        let endpoint = 'account/' + + '/jwt/';

        let seq =, {}).share(); => res.json()).subscribe(res => {

            if (res.success) {


                this.settings.setValue("user", account);



        }, err => {


        return seq;

error: “this.setting is undefined”


import { Injectable } from '@angular/core';

import { Storage } from '@ionic/storage';

 * A simple settings/config class for storing key/value pairs with persistence.
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 => {
      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];

  setValue(key: string, value: any) {
    this.settings[key] = value;
    return, this.settings);

  setAll(value: any) {
    return, value);

  getValue(key: string) {
      .then(settings => {
        return settings[key];

  save() {
    return this.setAll(this.settings);

  get allSettings() {
    return this.settings;

You don’t seem to initialize settings in settings.ts to a default value nor ever seem to call load. Which means settings will be undefined when you call this.settings.setValue.

@SigmundFroyd do you mean by initialise setting in settings.ts?

Am i not initialising it in the constructor of my user.ts?

public settings: Settings

And are you saying i need to call the load() function first before i can get and set anything?

     return this.settings.load().then(() => {
            this.settings.setValue("jwt", jwt);
            this.settings.setValue("user_id", user_id);

like this?

The public settings: Settings in your user.ts constructor only initializes your Settings provider, and not the settings object you have settings: any; in your Settings provider.

So based on how you have your code set-up right now, yes you at some point have to call load() and make sure that you don’t set anything until it’s called.

An alternative solution is to simplify your code, which seems a bit mishmashed as is, into something like this:

export class Settings {
  private SETTINGS_KEY_PREFIX: string = '_settings_';

  constructor(public storage: Storage) {

  setValue(key: string, value: any) {
    return + key, value);

  getValue(key: string, defaultValue: any) {
    return + key)
    .then(value => value || defaultValue)
    .err(_ => defaultValue);

This setting.ts is not what i made but it came with the ionic super starter pack.

“pre-built pages, providers, and best practices for Ionic development.”

This is why i want to learn how to make use of it.

Did you find a solution for this? I’m also interested in learning how to deal with settings and how to store a jwt token.