Skip to content

Commit

Permalink
fix(middleware session): fix middleware session
Browse files Browse the repository at this point in the history
  • Loading branch information
BernardoSM committed Oct 9, 2023
1 parent 217181d commit e4ddd63
Show file tree
Hide file tree
Showing 6 changed files with 1,201 additions and 12 deletions.
2 changes: 1 addition & 1 deletion apps/app/components/molecules/TopBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const defaultAsideStore = useDefaultAsideStore();
</VDropdown>
</div>
<div v-else>
<NuxtLink to="/sign-in" @click="sessionStore.signOut()">
<NuxtLink to="/sign-in">
<MButton text="Fazer login" variant="outline" size="sm" />
</NuxtLink>
</div>
Expand Down
9 changes: 4 additions & 5 deletions apps/app/middleware/session.global.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default defineNuxtRouteMiddleware(async (to) => {
if (to.meta.ignoreMiddleware) return;
export default defineNuxtRouteMiddleware(async (to, from) => {
if (from.meta.ignoreMiddleware) return;

const userStore = useUserStore();
const serverUser = useCookie("m-user");
Expand All @@ -10,15 +10,14 @@ export default defineNuxtRouteMiddleware(async (to) => {

if (to.query.oauth) return;

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

if (["profile"].includes(String(to.name)) && !serverHasSession) {
router.push("/sign-in");
navigateTo("/sign-in");
}
if (["sign-in", "sign-up"].includes(String(to.name)) && serverHasSession) {
router.push("/");
navigateTo("/");
}
sessionStore.cleared = false;
});
1 change: 1 addition & 0 deletions apps/app/pages/sign-in.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const runtimeConfig = useRuntimeConfig();
const route = useRoute();
onMounted(async () => {
await sessionStore.signOut();
sessionStore.cleared = false;
if (route.query?.error) {
Expand Down
4 changes: 2 additions & 2 deletions apps/app/pages/sign-out.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ definePageMeta({
const sessionStore = useSessionStore();
const router = useRouter();
onMounted(() => {
sessionStore.signOut();
onMounted(async () => {
await sessionStore.signOut();
router.push("/sign-in");
});
</script>
Expand Down
7 changes: 3 additions & 4 deletions apps/app/stores/clerk/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ export const useSessionStore = defineStore("session", {
}
},
async signOut() {
this.cleared = true;
const clerkToken = useCookies([]);
const userCookie = useCookies([]);
userCookie.remove("m-user");
clerkToken.remove("__session");
this.cleared = true;

await userCookie.remove("m-user");
await clerkToken.remove("__session");
await this.$clerk.signOut();
},
},
Expand Down
Loading

0 comments on commit e4ddd63

Please sign in to comment.