/**
 * This is a utility file you can use to store and retrieve the user token
 * if your app is using a token-based authentication strategy.
 **/

import storageAvailable from './storageAvailable';

// If the user's browser doesn't support localStorage, we can use this variable
// as a fallback to store the their authentication token. This will allow them
// to use use the application, but if they refresh the browser (or navigate away
// from the application, this variable will be reset, and they'll be logged
// out of the application since we've lost the token that provided their identity).
let tokenFallback = null;

export default {

  saveToken(token) {
    if (storageAvailable('localStorage')) {
      localStorage.setItem('userToken', token);
    } else {
      tokenFallback = token;
      let message = `
      Your web browser does not support storing settings locally. In Safari, the most common cause of this
      is using "Private Browsing Mode". This will not affect the functionality of the application, but you
      will be logged out if you refresh the page.
      `;
      message = message.split('\n').join(' ').trim();
      alert(message);
    }
  },

  hasToken() {
    if (storageAvailable('localStorage')) {
      return !!localStorage.getItem('userToken');
    }

    return !!tokenFallback;
  },

  getToken() {
    if (storageAvailable('localStorage')) {
      return localStorage.getItem('userToken');
    }

    return tokenFallback;
  },

  deleteToken() {
    if (storageAvailable('localStorage')) {
      localStorage.removeItem('userToken');
    } else {
      tokenFallback = null;
    }
  }

};

@@ -299,20 +299,16 @@
 o store 
-the 
 their au
@@ -351,22 +351,18 @@
 them
-%0A//
  to use
- use
+%0A//
  the
@@ -424,19 +424,16 @@
 ate away
-%0A//
  from th
@@ -429,24 +429,27 @@
 way from the
+%0A//
  application
@@ -448,16 +448,17 @@
 lication
+)
 , this v
@@ -501,19 +501,16 @@
 e logged
-%0A//
  out of 
@@ -508,24 +508,27 @@
 d out of the
+%0A//
  application
@@ -583,17 +583,16 @@
 identity
-)
 .%0Alet to
@@ -843,40 +843,37 @@
 not 
-support storing settings locally
+allow access to local storage
 . In
@@ -910,19 +910,20 @@
  of 
-this
 %0A      
+this 
 is u
@@ -996,17 +996,18 @@
 ty of th
-e
+is
  applica
@@ -1016,21 +1016,22 @@
 on, 
+%0A      
 but you
-%0A     
  wil
@@ -1053,16 +1053,34 @@
  if you 
+leave the site or 
 refresh 

improve messaging in auth utility