Saving user to localstorage
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import {ref} from "vue";
|
import {ref} from "vue";
|
||||||
import {$user} from "./users.js";
|
import {$user} from "./stores/user.js";
|
||||||
|
|
||||||
const formData = ref({username: '', password: ''})
|
const formData = ref({username: '', password: ''})
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
||||||
|
})
|
||||||
@@ -12,6 +12,7 @@ const {title} = Astro.props
|
|||||||
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
||||||
<meta name="generator" content={Astro.generator} />
|
<meta name="generator" content={Astro.generator} />
|
||||||
<title>Subtle Storm</title>
|
<title>Subtle Storm</title>
|
||||||
|
<script src="../components/scripts/user_store.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>{title}</h1>
|
<h1>{title}</h1>
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import LoginForm from '../components/LoginForm.vue';
|
|||||||
{import.meta.env.DEV &&
|
{import.meta.env.DEV &&
|
||||||
<script>
|
<script>
|
||||||
import { logger } from '@nanostores/logger'
|
import { logger } from '@nanostores/logger'
|
||||||
import {$user} from "../components/users";
|
import {$user} from "../components/stores/user";
|
||||||
|
|
||||||
let _destroy = logger({user: $user})
|
let _destroy = logger({user: $user})
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user