Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use localStorage to cache logged-in user #124

Merged
merged 4 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ import NavBar from '@/components/NavBar';
import TitleBar from '@/components/TitleBar';
import SiteNotification from '@/elements/SiteNotification';
import { siteNotificationStore } from '@/stores/alert-store';

// current user object
// structure: { username, email, name, level, timezone, id }
import { userStore as currentUser } from '@/stores/user-store';
devmount marked this conversation as resolved.
Show resolved Hide resolved

// component constants
const apiUrl = inject('apiUrl');
const dj = inject('dayjs');
Expand Down Expand Up @@ -91,10 +96,6 @@ provide('call', call);
// menu items for main navigation
const navItems = ['calendar', 'appointments', 'settings'];

// current user object
// structure: { username, email, name, level, timezone, id }
const currentUser = ref(null);

// db tables
const calendars = ref([]);
const appointments = ref([]);
Expand All @@ -107,6 +108,10 @@ const routeIsPublic = computed(
// check login state of current user first
const checkLogin = async () => {
if (auth.isAuthenticated.value) {
if (currentUser.value) {
// avoid calling the backend unnecessarily
return;
}
// call backend to create user if they do not exist in database
const { data, error } = await call('login').get().json();
// assign authed user data
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/components/SettingsAccount.vue
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ import PrimaryButton from '@/elements/PrimaryButton.vue';
import SecondaryButton from '@/elements/SecondaryButton.vue';
import TextButton from '@/elements/TextButton.vue';
import CautionButton from '@/elements/CautionButton.vue';
import { updateUserInStorage } from '@/stores/user-store';

// view properties
const props = defineProps({
Expand Down Expand Up @@ -197,6 +198,8 @@ const updateUser = async () => {
};
const { error } = await call('me').put(inputData).json();
if (!error.value) {
// Trigger update to user in localStorage
updateUserInStorage(inputData);
errorUsername.value = false;
// TODO show some confirmation
await refreshData();
Expand Down
73 changes: 73 additions & 0 deletions frontend/src/stores/user-store.js
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 = [
devmount marked this conversation as resolved.
Show resolved Hide resolved
'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];
Copy link
Contributor Author

@radishmouse radishmouse Jul 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if there's a better way to do this... I had to wrap it in a try/catch when accessing the timzeone field (and possibly other fields that don't already exist).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, feels like there is an easier way, but I don't currently don't see it either 😅

} catch (e) {
// console.log(e);
}
});
}

// Attempt initial load of data
load();
export const userStore = ref(userObj);

// Store in localStorage on update
watch(userStore, (newUser) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is triggered by App.vue when we first log in.
We may decide later on to make the user a provide() value, in which case it could also be triggered from the Account settings view

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, makes sense, I would like this 👍🏻

const data = {};
copyRefValuesToObj(newUser, data);
store(data);
});

export function removeUserFromStorage() {
localStorage.removeItem(STORAGE_KEY);
}

export function updateUserInStorage(obj) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vue complains if you try to assign to a prop.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, normally you would emit an event to the prop holding component which then updates the prop. But for now this should be fine.

// Provides a way to update the userStore
// from a component that received it as a prop
const data = {};
copyRefValuesToObj(userStore, data);
store({
...data,
...obj,
});
}
4 changes: 3 additions & 1 deletion frontend/src/views/ProfileView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
</template>

<script setup>
import { subscriberLevels, appointmentState } from '@/definitions';
import { inject, computed, onMounted } from 'vue';
import { keyByValue } from '@/utils';
import { useI18n } from 'vue-i18n';
import { subscriberLevels, appointmentState } from '@/definitions';
import { removeUserFromStorage } from '@/stores/user-store';
import PrimaryButton from '@/elements/PrimaryButton';

// icons
Expand All @@ -58,6 +59,7 @@ const pendingAppointments = computed(() => props.appointments.filter((a) => a.st

// do log out
const logout = () => {
removeUserFromStorage();
auth.logout({
logoutParams: {
returnTo: window.location.origin,
Expand Down