Skip to content

Commit cd6207c

Browse files
committed
scope upgrade works
1 parent 26411a9 commit cd6207c

File tree

3 files changed

+39
-27
lines changed

3 files changed

+39
-27
lines changed

app/components/Header/AuthModal.client.vue

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,21 @@
11
<script setup lang="ts">
2+
import { authRedirect } from '~/composables/useAtproto'
3+
24
const handleInput = shallowRef('')
35
46
const { user, logout } = useAtproto()
57
68
async function handleBlueskySignIn() {
7-
await navigateTo(
8-
{
9-
path: '/api/auth/atproto',
10-
query: { handle: 'https://bsky.social' },
11-
},
12-
{ external: true },
13-
)
9+
await authRedirect('https://bsky.social')
1410
}
1511
1612
async function handleCreateAccount() {
17-
await navigateTo(
18-
{
19-
path: '/api/auth/atproto',
20-
query: { handle: 'https://npmx.social', create: 'true' },
21-
},
22-
{ external: true },
23-
)
13+
await authRedirect('https://npmx.social', true)
2414
}
2515
2616
async function handleLogin() {
2717
if (handleInput.value) {
28-
await navigateTo(
29-
{
30-
path: '/api/auth/atproto',
31-
query: { handle: handleInput.value },
32-
},
33-
{ external: true },
34-
)
18+
await authRedirect(handleInput.value)
3519
}
3620
}
3721
</script>

app/composables/useAtproto.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
1+
import { ERROR_NEED_REAUTH } from '#imports'
2+
import type { FetchError } from 'ofetch'
13
import type { UserSession } from '#shared/schemas/userSession'
24
import type { PackageLikes } from '~~/server/utils/atproto/utils/likes'
35

6+
export async function authRedirect(identifier: string, create: boolean = false) {
7+
let query = { handle: identifier } as {}
8+
if (create) {
9+
query = { ...query, create: 'true' }
10+
}
11+
await navigateTo(
12+
{
13+
path: '/api/auth/atproto',
14+
query,
15+
},
16+
{ external: true },
17+
)
18+
}
19+
20+
export async function handleAuthError(e: unknown, userHandle?: string | null): Promise<never> {
21+
const fetchError = e as FetchError
22+
const errorMessage = fetchError?.data?.message
23+
if (errorMessage === ERROR_NEED_REAUTH && userHandle) {
24+
await authRedirect(userHandle)
25+
}
26+
throw e
27+
}
28+
429
export function useAtproto() {
530
const { data: user, pending, clear } = useFetch<UserSession | null>('/api/auth/session')
631

@@ -16,6 +41,7 @@ export function useAtproto() {
1641
}
1742

1843
export function useLikePackage(packageName: string) {
44+
const { user } = useAtproto()
1945
const data = ref<PackageLikes | null>(null)
2046
const error = ref<Error | null>(null)
2147
const pending = ref(false)
@@ -25,15 +51,16 @@ export function useLikePackage(packageName: string) {
2551
error.value = null
2652

2753
try {
28-
const result = await $fetch('/api/auth/social/like', {
54+
const result = await $fetch<PackageLikes>('/api/auth/social/like', {
2955
method: 'POST',
3056
body: { packageName },
3157
})
58+
3259
data.value = result
3360
return result
3461
} catch (e) {
35-
error.value = e as Error
36-
throw e
62+
error.value = e as FetchError
63+
await handleAuthError(e, user.value?.handle)
3764
} finally {
3865
pending.value = false
3966
}

app/pages/package/[...package].vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { areUrlsEquivalent } from '#shared/utils/url'
1212
import { isEditableElement } from '~/utils/input'
1313
import { formatBytes } from '~/utils/formatters'
1414
import { NuxtLink } from '#components'
15+
import { useModal } from '~/composables/useModal'
1516
1617
definePageMeta({
1718
name: 'package',
@@ -354,7 +355,8 @@ const canonicalUrl = computed(() => {
354355
355356
//atproto
356357
const { user } = useAtproto()
357-
const showAuthModal = ref(false)
358+
359+
const authModal = useModal('auth-modal')
358360
359361
const { data: likesData } = useFetch(() => `/api/social/likes/${packageName.value}`, {
360362
default: () => ({ totalLikes: 0, userHasLiked: false }),
@@ -364,7 +366,7 @@ const { mutate: likePackage } = useLikePackage(packageName.value)
364366
365367
const likeAction = async () => {
366368
if (user.value?.handle == null) {
367-
showAuthModal.value = true
369+
authModal.open()
368370
} else {
369371
const result = await likePackage()
370372
if (result?.totalLikes) {
@@ -1111,7 +1113,6 @@ defineOgImageComponent('Package', {
11111113
</p>
11121114
<NuxtLink to="/" class="btn">{{ $t('common.go_back_home') }}</NuxtLink>
11131115
</div>
1114-
<AuthModal v-model:open="showAuthModal" />
11151116
</main>
11161117
</template>
11171118

0 commit comments

Comments
 (0)