Skip to content

Commit

Permalink
fix: sign out bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
WodenWang820118 committed May 19, 2024
1 parent e1c129b commit 42d72b0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
61 changes: 47 additions & 14 deletions src/shared/components/navigation/navigation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,7 @@
<p>Profile</p>
</router-link>
</div>
<div
@click="
signUserOut;
toggleProfileMenu;
"
class="option"
>
<div @click="signUserOut" class="option">
<img
class="icon"
src="../../../assets/icons/sign-out-alt-regular.svg"
Expand Down Expand Up @@ -95,9 +89,11 @@
</template>

<script lang="ts">
import { ref, computed, defineComponent, onMounted } from "vue";
import { ref, computed, defineComponent, onMounted, onBeforeMount } from "vue";
import { AuthService } from "../../services/auth.service";
import { useUserStore } from "../../../stores/users";
import { useRouter } from "vue-router";
import { auth } from "../../firebase/firebaseInit";
export default defineComponent({
name: "navigation",
Expand All @@ -111,12 +107,18 @@ export default defineComponent({
},
setup() {
const store = useUserStore();
const router = useRouter();
const authService = new AuthService();
const profileMenu = ref(false);
const mobile = ref(false);
const mobileNav = ref(false);
const windowWidth = ref(window.innerWidth);
const profile = ref(null);
const profileInitials = ref("");
const profileFirstName = ref("");
const profileLastName = ref("");
const profileUsername = ref("");
const profileEmail = ref("");
function checkScreen() {
if (windowWidth.value <= 750) {
Expand All @@ -135,29 +137,60 @@ export default defineComponent({
function toggleProfileMenu(e: Event) {
if (e.target === null) return;
e.stopImmediatePropagation();
console.log(e.target);
profileMenu.value = !profileMenu.value;
}
// TODO: duplicate code; same as in profile.vue
function getProfileInfo() {
auth.onAuthStateChanged(async (currentUser) => {
if (currentUser) {
try {
await store.getProfileInfo(currentUser.uid);
store.setUser(currentUser);
store.setProfileInitials();
profileInitials.value = store.profileInitials;
profileFirstName.value = store.profileFirstName;
profileLastName.value = store.profileLastName;
profileUsername.value = store.profileUsername;
profileEmail.value = store.profileEmail;
} catch (error) {
console.error(error);
}
}
});
}
async function signUserOut() {
await authService.signUserOut();
router.push({ name: "home" });
window.location.reload(); // manually update UI state
}
onBeforeMount(() => {
getProfileInfo();
});
onMounted(() => {
checkScreen();
profileMenu.value = false;
});
return {
user: computed(() => store.user),
profileInitials: computed(() => store.profileInitials),
profileFirstName: computed(() => store.profileFirstName),
profileLastName: computed(() => store.profileLastName),
profileUsername: computed(() => store.profileUsername),
profileEmail: computed(() => store.profileEmail),
profileInitials,
profileFirstName,
profileLastName,
profileUsername,
profileEmail,
profileMenu,
profile,
mobile,
mobileNav,
windowWidth,
toggleProfileMenu,
toggleMobileNav,
signUserOut: async () => await authService.signUserOut(),
signUserOut: async () => await signUserOut(),
};
},
});
Expand Down
5 changes: 1 addition & 4 deletions src/shared/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ import {
sendPasswordResetEmail,
signInWithEmailAndPassword,
signOut,
User,
} from "firebase/auth";
import { auth } from "../firebase/firebaseInit";
import { useRouter } from "vue-router";

export class AuthService {
router = useRouter();
constructor() {}

async signUserOut() {
await signOut(auth);
this.router.push({ name: "home" });
sessionStorage.removeItem("users");
}

async sendPasswordResetEmail(email: string) {
Expand Down

0 comments on commit 42d72b0

Please sign in to comment.