Skip to content

Commit

Permalink
chore: improve listener, docs & types
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Feb 4, 2025
1 parent 1519655 commit 50573d9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Nuxt Auth Utils automatically adds some plugins to fetch the current user sessio

```vue
<script setup>
const { loggedIn, user, session, fetch, clear } = useUserSession()
const { loggedIn, user, session, fetch, clear, openInPopup } = useUserSession()
</script>
<template>
Expand All @@ -74,6 +74,8 @@ const { loggedIn, user, session, fetch, clear } = useUserSession()
<div v-else>
<h1>Not logged in</h1>
<a href="/auth/github">Login with GitHub</a>
<!-- or open the OAuth route in a popup -->
<button @click="openInPopup('/auth/github')">Login with GitHub</button>
</div>
</template>
```
Expand Down Expand Up @@ -106,6 +108,10 @@ interface UserSessionComposable {
* Clear the user session and remove the session cookie.
*/
clear: () => Promise<void>
/**
* Open the OAuth route in a popup that auto-closes when successful.
*/
openInPopup: (route: string, size?: { width?: number, height?: number }) => void
}
```

Expand Down
16 changes: 9 additions & 7 deletions playground/app.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup lang="ts">
const { user, openInPopup } = useUserSession()
const inPopup = ref(false)
const providers = computed(() =>
[
{
Expand Down Expand Up @@ -209,8 +210,6 @@ const providers = computed(() =>
click: inPopup.value ? () => openInPopup(p.to) : void 0,
})),
)
const inPopup = ref(false)
</script>

<template>
Expand Down Expand Up @@ -248,11 +247,6 @@ const inPopup = ref(false)
>
Logout
</UButton>
<UCheckbox
v-model="inPopup"
name="open-in-popup"
label="Open in popup"
/>
</template>
<template #placeholder>
<UButton
Expand All @@ -268,6 +262,14 @@ const inPopup = ref(false)
</UHeader>
<UMain>
<UContainer>
<div class="text-xs mt-4">
Popup mode <UToggle
v-model="inPopup"
size="xs"
name="open-in-popup"
label="Open in popup"
/>
</div>
<NuxtPage />
</UContainer>
</UMain>
Expand Down
32 changes: 17 additions & 15 deletions src/runtime/app/composables/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,32 @@ export function useUserSession(): UserSessionComposable {
}
}

const openInPopup = (route, size = { width: 960, height: 600 }) => {
const popupListener = (e: StorageEvent) => {
if (e.key === 'temp-nuxt-auth-utils-popup') {
fetch()
window.removeEventListener('storage', popupListener)
}
}
const openInPopup = (route: string, size: { width?: number, height?: number } = {}) => {
// Set a local storage item to tell the popup that we pending auth
localStorage.setItem('temp-nuxt-auth-utils-popup', 'true')

const top
= window.top.outerHeight / 2
+ window.top.screenY
- size.height / 2
const left
= window.top.outerWidth / 2
+ window.top.screenX
- size.width / 2
const width = size.width ?? 960
const height = size.height ?? 600
const top = (window.top?.outerHeight ?? 0) / 2
+ (window.top?.screenY ?? 0)
- height / 2
const left = (window.top?.outerWidth ?? 0) / 2
+ (window.top?.screenX ?? 0)
- width / 2

window.open(
route,
'nuxt-auth-utils-popup',
`width=${size.width}, height=${size.height}, top=${top}, left=${left}, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no`,
`width=${width}, height=${height}, top=${top}, left=${left}, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no`,
)

window.addEventListener('storage', (e: StorageEvent) => {
if (e.key === 'temp-nuxt-auth-utils-popup') {
fetch()
}
}, { once: true })
window.addEventListener('storage', popupListener)
}

return {
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/types/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ export interface UserSessionComposable {
* Clear the user session and remove the session cookie.
*/
clear: () => Promise<void>
/**
* Open the OAuth route in a popup that auto-closes when successful.
*/
openInPopup: (route: string, size?: { width?: number, height?: number }) => void
}

0 comments on commit 50573d9

Please sign in to comment.