Saving user to localstorage

This commit is contained in:
2026-01-03 00:44:49 +02:00
parent 927a6ccd80
commit 2c021db505
5 changed files with 21 additions and 3 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
<script setup>
import {ref} from "vue";
import {$user} from "./users.js";
import {$user} from "./stores/user.js";
const formData = ref({username: '', password: ''})
+17
View File
@@ -0,0 +1,17 @@
import {$user} from "../stores/user.js";
const LOCAL_STORAGE_KEY="user"
$user.subscribe(value => {
if(value == null) return;
localStorage[LOCAL_STORAGE_KEY] = JSON.stringify(value)
})
window.addEventListener('load', () => {
let data = localStorage[LOCAL_STORAGE_KEY]
if (data == null) { return }
data = JSON.parse(data)
if (data == null || data.token == null) { return }
if(Date.parse(data.expiresAt) < Date()) { return } // TODO: this should probably have more logic
$user.set(data)
})
@@ -1,3 +1,3 @@
import { atom } from 'nanostores'
export const $user = atom(null)
export const $user = atom(null)