Skip to content

Commit

Permalink
refactor: improve sign out and add set user cookie flag
Browse files Browse the repository at this point in the history
  • Loading branch information
BernardoSM committed Oct 12, 2023
1 parent 2362b38 commit 95e7433
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 22 deletions.
16 changes: 10 additions & 6 deletions apps/app/middleware/session.global.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
export default defineNuxtRouteMiddleware(async (to, from) => {
if (from.meta.ignoreMiddleware) return;
export default defineNuxtRouteMiddleware(async (to) => {
const sessionStore = useSessionStore();
if (to.name === "sign-out") {
await sessionStore.signOut();
return navigateTo({ path: "/sign-in" });
}

const userStore = useUserStore();
const serverUser = useCookie("m-user");

if (serverUser) {
if (serverUser.value as unknown) {
userStore.user = serverUser.value as typeof userStore.user;
}

if (to.query.oauth) return;

const sessionStore = useSessionStore();
const serverHasSession = !!serverUser.value || sessionStore.hasSession();

if (["profile"].includes(String(to.name)) && !serverHasSession) {
navigateTo("/sign-in");
return navigateTo("/sign-in");
}

if (["sign-in", "sign-up"].includes(String(to.name)) && serverHasSession) {
navigateTo("/");
return navigateTo("/");
}
sessionStore.cleared = false;
});
14 changes: 1 addition & 13 deletions apps/app/pages/sign-out.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
<script setup lang="ts">
definePageMeta({
ignoreMiddleware: true,
});
const sessionStore = useSessionStore();
const router = useRouter();
onMounted(async () => {
await sessionStore.signOut();
router.push("/sign-in");
});
</script>
<script setup lang="ts"></script>

<template>
<div class="flex justify-center pt-10 text-sm text-gray-500">
Expand Down
5 changes: 2 additions & 3 deletions apps/app/stores/clerk/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ export const useSessionStore = defineStore("session", {
checkSessionStatus() {
const clientSession = this.$clerk?.client?.sessions[0];
if (clientSession?.status === "expired") {
this.signOut();
this.$router.push("/sign-in");
this.$router.push("/sign-out");
}
},
hasSession() {
const clientSession = this.$clerk?.client?.sessions[0];
return !this.cleared && clientSession;
return Boolean(!this.cleared && clientSession);
},
async refreshSession() {
const clientSession = this.$clerk?.client?.sessions[0];
Expand Down
2 changes: 2 additions & 0 deletions apps/app/stores/clerk/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export const useUserStore = defineStore("user", {
this.user = userData;
const userCookie = useCookies([]);
userCookie.set("m-user", userData);

console.log("set user");
},
async updateUser() {
await this.$clerk.user.update<UpdateUserParams>({
Expand Down

0 comments on commit 95e7433

Please sign in to comment.