import { writable } from "svelte/store";

// an alternative way of doing things where i have a svelte store connected to localStorage

// do in need to adapt this to work with 2fa?

let _user = localStorage.getItem('42User');

// turns out a simple store is actually the easiest :)
// export const userStore = writable(_user ? JSON.parse(_user) : null);  // we start with no user, but go get one if one exists
// export const userStore = writable(null);

// ok so this will happen no matter what, basically we are telling it what to do if the store containing the user changes
userStore.subscribe((value) => {
  if (value)
    localStorage.setItem('42User', JSON.stringify(value));
  else
    localStorage.removeItem('42User'); // for logout
});

export const userLogout = () => userStore.set(null);


// export const tmpStore = userStore