Skip to content

Commit

Permalink
fix(storefront): Properly set ecomPassport session auth for vbeta-app
Browse files Browse the repository at this point in the history
Ensure sign out is called with Firebase auth initialized
  • Loading branch information
leomp12 committed Apr 16, 2024
1 parent a4d1be9 commit a6d8e4e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
6 changes: 4 additions & 2 deletions packages/storefront/src/lib/scripts/vbeta-app.ts
Expand Up @@ -218,7 +218,7 @@ if (!import.meta.env.SSR) {
if (isAuthenticated.value) {
ecomPassport.setSession({
auth: {
...session.auth,
token: session.auth,
id: session.auth?.customer_id,
level: 3,
},
Expand All @@ -242,7 +242,9 @@ if (!import.meta.env.SSR) {
ecomPassport.on('logout', () => {
if (isAuthenticated.value) {
logout();
window.location.href = '/';
watch(isAuthenticated, () => {
window.location.href = '/';
}, { once: true });
}
});
}, 400);
Expand Down
27 changes: 16 additions & 11 deletions packages/storefront/src/lib/state/customer-session.ts
Expand Up @@ -2,7 +2,7 @@ import type { Customers } from '@cloudcommerce/api/types';
import type { Auth } from 'firebase/auth';
import api from '@cloudcommerce/api';
import { nickname as getNickname } from '@ecomplus/utils';
import { ref, computed } from 'vue';
import { ref, computed, watch } from 'vue';
import { requestIdleCallback } from '@@sf/sf-lib';
import useStorage from '@@sf/state/use-storage';

Expand Down Expand Up @@ -40,26 +40,18 @@ const customerEmail = computed({
},
});

let firebaseAuth: Auth;
let firebaseAuth: Auth | undefined;
const isLogged = computed(() => {
return isAuthenticated.value || !!firebaseAuth?.currentUser?.emailVerified;
});
const logout = () => {
firebaseAuth.signOut().then(() => {
session.auth = emptySession.auth;
session.customer = emptySession.customer;
localStorage.removeItem(storageKey);
});
};

const throwNoAuth = (msg = 'Not authenticated') => {
const err: any = new Error(msg);
err.isNoAuth = true;
throw err;
};

const authenticate = async () => {
const authToken = await firebaseAuth.currentUser?.getIdToken();
const authToken = await firebaseAuth?.currentUser?.getIdToken();
if (!authToken) {
throwNoAuth('Can\'t get Firebase user ID token');
return;
Expand Down Expand Up @@ -155,6 +147,19 @@ const initializeFirebaseAuth = (canWaitIdle?: boolean) => {
}
};

const logout = () => {
if (!firebaseAuth) {
initializeFirebaseAuth();
watch(isAuthReady, () => logout(), { once: true });
return;
}
firebaseAuth.signOut().then(() => {
session.auth = emptySession.auth;
session.customer = emptySession.customer;
localStorage.removeItem(storageKey);
});
};

export default session;

export {
Expand Down

0 comments on commit a6d8e4e

Please sign in to comment.