Skip to content

Commit e5817b1

Browse files
committed
chore: leverage useHub().auth
1 parent c163243 commit e5817b1

File tree

4 files changed

+19
-31
lines changed

4 files changed

+19
-31
lines changed

_demo/middleware/auth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default defineNuxtRouteMiddleware(() => {
2-
const { loggedIn } = useUserSession()
2+
const { user } = useHub().auth
33

4-
if (!loggedIn.value) {
4+
if (!user.value) {
55
return navigateTo('/')
66
}
77
})

_demo/pages/index.vue

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
<script setup lang="ts">
2-
const host = useRequestURL().host
3-
let hosting = { url: 'https://pages.cloudflare.com', title: 'CloudFlare Pages' }
4-
if (host.includes('.netlify.app')) {
5-
hosting = { url: 'https://www.netlify.com/products/#netlify-edge-functions', title: 'Netlify Edge Functions' }
6-
} else if (host.includes('.vercel.app')) {
7-
hosting = { url: 'https://vercel.com/features/edge-functions', title: 'Vercel Edge Functions' }
8-
} else if (host.includes('.lagon.dev')) {
9-
hosting = { url: 'https://lagon.app', title: 'Lagon' }
10-
} else if (host.includes('.deno.dev')) {
11-
hosting = { url: 'https://deno.com/deploy', title: 'Deno Deploy' }
12-
}
13-
const isD1 = host.includes('nuxt-todos-edge.pages.dev')
14-
const { loggedIn } = useUserSession()
2+
const { auth } = useHub()
153
</script>
164

175
<template>
@@ -21,8 +9,8 @@ const { loggedIn } = useUserSession()
219
Todo List
2210
</h3>
2311
<UButton
24-
v-if="!loggedIn"
25-
to="/api/auth/providers/github"
12+
v-if="!auth.user"
13+
@click="auth.loginWith('github')"
2614
icon="i-simple-icons-github"
2715
label="Login with GitHub"
2816
color="black"
@@ -50,17 +38,7 @@ const { loggedIn } = useUserSession()
5038
</div>
5139
</template>
5240
<p class="font-medium">
53-
Welcome to Nuxt Todos Edge.
54-
</p>
55-
<p>
56-
A <a href="https://nuxt.com" target="_blank" class="text-primary-500" rel="noopener">Nuxt</a> demo hosted on <a :href="hosting.url" target="_blank" rel="noopener" class="text-primary-500">{{ hosting.title }}</a> with server-side rendering on the edge and using <NuxtLink
57-
:href="isD1 ? 'https://developers.cloudflare.com/d1/' : 'https://turso.tech'"
58-
target="_blank"
59-
rel="noopener"
60-
class="text-primary-500"
61-
>
62-
{{ isD1 ? 'D1' : 'Turso' }} database
63-
</NuxtLink>.
41+
Welcome to NuxtHub Demo
6442
</p>
6543
<hr class="dark:border-gray-700">
6644
<p class="text-sm text-gray-700 dark:text-gray-300 italic">

_demo/pages/todos.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const newTodo = ref('')
77
const newTodoInput = ref(null)
88
99
const toast = useToast()
10-
const { user, clear } = useUserSession()
10+
const { user, logout } = useHub().auth
1111
const { data: todos } = await useFetch('/api/todos')
1212
1313
async function addTodo () {
@@ -55,7 +55,7 @@ async function deleteTodo (todo) {
5555
const items = [[{
5656
label: 'Logout',
5757
icon: 'i-heroicons-arrow-left-on-rectangle',
58-
click: clear
58+
click: logout
5959
}]]
6060
</script>
6161

composables/hub.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
export function useHub() {
22
const config = useState<HubConfig['public']>('hub_config')
3+
const session = useUserSession()
34

45
return {
5-
config
6+
config,
7+
auth: {
8+
...session,
9+
loginWith(provider: string) {
10+
window.location.href = `/api/auth/providers/${provider}`
11+
},
12+
logout() {
13+
return session.clear()
14+
}
15+
}
616
}
717
}

0 commit comments

Comments
 (0)