import Vue from 'vue'
import axios from 'axios'
const storageLocation = 'loggedIn'

const subscribers = []

const notifySubscribers = newLoginStatus => {
  subscribers.forEach(subscriber => subscriber(newLoginStatus))
}

export const auth = {
  async isLoggedIn () {
    const savedStatus = window.localStorage.getItem(storageLocation)
    if (savedStatus !== null) {
      return savedStatus === '1'
    }
    const response = await axios.get(process.env.VUE_APP_ROOT_API + '/login')
    let loggedIn = '0'
    if (response.data.user !== 'guest') {
      loggedIn = '1'
    }
    window.localStorage.setItem(storageLocation, loggedIn)
    return loggedIn === '1'
  },
  subscribe (onLoginStatusChange) {
    subscribers.push(onLoginStatusChange)
  },
  login (username, password) {
    return axios.post(process.env.VUE_APP_ROOT_API + '/login/login', { username: username, password: password })
      .then(resp => {
        if (resp.data.user !== 'guest') {
          this.loginSuccess()
          return true
        } else {
          return false
        }
      })
  },
  loginGoogle (returnUrl) {
    window.open(process.env.VUE_APP_ROOT_API + '/login/google?callback=' + encodeURI(returnUrl), '', 'width=500px,height=600px')
  },
  loginPbsMiData (returnUrl) {
    window.open(process.env.VUE_APP_ROOT_API + '/login/pbsmidata?callback=' + encodeURI(returnUrl), '', 'width=500px,height=600px')
  },
  loginSuccess () {
    window.localStorage.setItem(storageLocation, '1')
    notifySubscribers(true)
  },
  logout (callback) {
    axios.get(process.env.VUE_APP_ROOT_API + '/login/logout').then(response => {
      window.localStorage.setItem(storageLocation, '0')
      notifySubscribers(false)
      if (callback) {
        callback(response)
      }
    })
  }
}

axios.interceptors.response.use(null, error => {
  if (error.status === 401) {
    auth.logout()
  }
  return Promise.reject(error)
})

Vue.auth = auth
Object.defineProperties(Vue.prototype, {
  $auth: {
    get () {
      return auth
    }
  }
})

@@ -45,41 +45,88 @@
 os'%0A
+%0A
 const 
-storageLocation = 'loggedIn
+STORAGE_LOCATION = 'loggedIn'%0Aconst LOGGED_IN = '1'%0Aconst LOGGED_OUT = '0
 '%0A%0Ac
@@ -359,31 +359,32 @@
 getItem(
-storageLocation
+STORAGE_LOCATION
 )%0A    if
@@ -438,19 +438,25 @@
 tus === 
-'1'
+LOGGED_IN
 %0A    %7D%0A 
@@ -551,19 +551,26 @@
 gedIn = 
-'0'
+LOGGED_OUT
 %0A    if 
@@ -621,19 +621,25 @@
 gedIn = 
-'1'
+LOGGED_IN
 %0A    %7D%0A 
@@ -669,31 +669,32 @@
 setItem(
-storageLocation
+STORAGE_LOCATION
 , logged
@@ -721,19 +721,25 @@
 dIn === 
-'1'
+LOGGED_IN
 %0A  %7D,%0A  
@@ -1529,28 +1529,35 @@
 tem(
-storageLocation, '1'
+STORAGE_LOCATION, LOGGED_IN
 )%0A  
@@ -1728,28 +1728,36 @@
 tem(
-storageLocation, '0'
+STORAGE_LOCATION, LOGGED_OUT
 )%0A  

Use constants for localStorage