Skip to content

Commit bdec735

Browse files
committed
scope upgrade works
1 parent 017ae9b commit bdec735

3 files changed

Lines changed: 39 additions & 27 deletions

File tree

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 {
631
data: user,
@@ -23,6 +48,7 @@ export function useAtproto() {
2348
}
2449

2550
export function useLikePackage(packageName: string) {
51+
const { user } = useAtproto()
2652
const data = ref<PackageLikes | null>(null)
2753
const error = ref<Error | null>(null)
2854
const pending = ref(false)
@@ -32,15 +58,16 @@ export function useLikePackage(packageName: string) {
3258
error.value = null
3359

3460
try {
35-
const result = await $fetch('/api/auth/social/like', {
61+
const result = await $fetch<PackageLikes>('/api/auth/social/like', {
3662
method: 'POST',
3763
body: { packageName },
3864
})
65+
3966
data.value = result
4067
return result
4168
} catch (e) {
42-
error.value = e as Error
43-
throw e
69+
error.value = e as FetchError
70+
await handleAuthError(e, user.value?.handle)
4471
} finally {
4572
pending.value = false
4673
}

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',
@@ -358,7 +359,8 @@ const canonicalUrl = computed(() => {
358359
359360
//atproto
360361
const { user } = useAtproto()
361-
const showAuthModal = ref(false)
362+
363+
const authModal = useModal('auth-modal')
362364
363365
const { data: likesData } = useFetch(() => `/api/social/likes/${packageName.value}`, {
364366
default: () => ({ totalLikes: 0, userHasLiked: false }),
@@ -368,7 +370,7 @@ const { mutate: likePackage } = useLikePackage(packageName.value)
368370
369371
const likeAction = async () => {
370372
if (user.value?.handle == null) {
371-
showAuthModal.value = true
373+
authModal.open()
372374
} else {
373375
const result = await likePackage()
374376
if (result?.totalLikes) {
@@ -1126,7 +1128,6 @@ defineOgImageComponent('Package', {
11261128
</p>
11271129
<NuxtLink to="/" class="btn">{{ $t('common.go_back_home') }}</NuxtLink>
11281130
</div>
1129-
<AuthModal v-model:open="showAuthModal" />
11301131
</main>
11311132
</template>
11321133

0 commit comments

Comments
 (0)