-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use localStorage to cache logged-in user (#124)
* Use localStorage to cache logged-in user * Rename import to use existing variable name * Update frontend/src/views/ProfileView.vue Co-authored-by: Andreas <mail@devmount.de> --------- Co-authored-by: Andreas <mail@devmount.de>
- Loading branch information
1 parent
221090d
commit 52639a9
Showing
4 changed files
with
88 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { ref, watch } from 'vue'; | ||
|
||
const STORAGE_KEY = 'tba/user'; | ||
|
||
const keys = [ | ||
'email', | ||
'level', | ||
'name', | ||
'timezone', | ||
'username', | ||
]; | ||
|
||
let userObj = null; | ||
|
||
function load() { | ||
// Retrieve values from localStorage | ||
const jsonData = localStorage.getItem(STORAGE_KEY); | ||
if (jsonData) { | ||
try { | ||
const data = JSON.parse(jsonData); | ||
userObj = { | ||
...data, | ||
}; | ||
} catch (e) { | ||
// console.log('Could not parse user from localStorage'); | ||
} | ||
} | ||
} | ||
|
||
function store(data) { | ||
try { | ||
const jsonData = JSON.stringify(data); | ||
localStorage.setItem(STORAGE_KEY, jsonData); | ||
} catch (e) { | ||
// console.log('Could not stringify and store to localStorage'); | ||
} | ||
} | ||
|
||
function copyRefValuesToObj(aRef, obj) { | ||
keys.forEach((k) => { | ||
try { | ||
obj[k] = aRef[k] ?? aRef.value[k]; | ||
} catch (e) { | ||
// console.log(e); | ||
} | ||
}); | ||
} | ||
|
||
// Attempt initial load of data | ||
load(); | ||
export const userStore = ref(userObj); | ||
|
||
// Store in localStorage on update | ||
watch(userStore, (newUser) => { | ||
const data = {}; | ||
copyRefValuesToObj(newUser, data); | ||
store(data); | ||
}); | ||
|
||
export function removeUserFromStorage() { | ||
localStorage.removeItem(STORAGE_KEY); | ||
} | ||
|
||
export function updateUserInStorage(obj) { | ||
// Provides a way to update the userStore | ||
// from a component that received it as a prop | ||
const data = {}; | ||
copyRefValuesToObj(userStore, data); | ||
store({ | ||
...data, | ||
...obj, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters