File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11<script setup lang="ts">
2+ import { useAtproto } from ' ~/composables/atproto/useAtproto'
23import { useModal } from ' ~/composables/useModal'
34
45const {
Original file line number Diff line number Diff line change 11<script setup lang="ts">
2- import { authRedirect } from ' ~/composables/useAtproto'
2+ import { authRedirect , useAtproto } from ' ~/composables/atproto /useAtproto'
33
44const handleInput = shallowRef (' ' )
55
Original file line number Diff line number Diff line change 11<script setup lang="ts">
22import { useFocusTrap } from ' @vueuse/integrations/useFocusTrap'
3+ import { useAtproto } from ' ~/composables/atproto/useAtproto'
34
45const isOpen = defineModel <boolean >(' open' , { default: false })
56
Original file line number Diff line number Diff line change 1+ import type { UserSession } from '#shared/schemas/userSession'
2+ import type { LocationQueryRaw } from 'vue-router'
3+
4+ export async function authRedirect ( identifier : string , create : boolean = false ) {
5+ let query : LocationQueryRaw = { handle : identifier }
6+ if ( create ) {
7+ query = { ...query , create : 'true' }
8+ }
9+ await navigateTo (
10+ {
11+ path : '/api/auth/atproto' ,
12+ query,
13+ } ,
14+ { external : true } ,
15+ )
16+ }
17+
18+ export function useAtproto ( ) {
19+ const { data : user , pending, clear } = useFetch < UserSession | null > ( '/api/auth/session' )
20+
21+ async function logout ( ) {
22+ await $fetch ( '/api/auth/session' , {
23+ method : 'delete' ,
24+ } )
25+
26+ clear ( )
27+ }
28+
29+ return { user, pending, logout }
30+ }
Original file line number Diff line number Diff line change 1+ import { FetchError } from 'ofetch'
2+ import { useAtproto } from '~/composables/atproto/useAtproto'
3+ import { handleAuthError } from '~/utils/atproto/helpers'
4+
5+ export function useLikePackage ( packageName : string ) {
6+ const { user } = useAtproto ( )
7+ const data = ref < PackageLikes | null > ( null )
8+ const error = ref < Error | null > ( null )
9+ const pending = ref ( false )
10+
11+ const mutate = async ( ) => {
12+ pending . value = true
13+ error . value = null
14+
15+ try {
16+ const result = await $fetch < PackageLikes > ( '/api/auth/social/like' , {
17+ method : 'POST' ,
18+ body : { packageName } ,
19+ } )
20+
21+ data . value = result
22+ return result
23+ } catch ( e ) {
24+ if ( e instanceof FetchError ) {
25+ await handleAuthError ( e , user . value ?. handle )
26+ }
27+ error . value = e as Error
28+ } finally {
29+ pending . value = false
30+ }
31+ }
32+
33+ return { data, error, pending, mutate }
34+ }
Original file line number Diff line number Diff line change 1+ import { FetchError } from 'ofetch'
2+ import { useAtproto } from '~/composables/atproto/useAtproto'
3+ import { handleAuthError } from '~/utils/atproto/helpers'
4+
5+ export function useUnlikePackage ( packageName : string ) {
6+ const { user } = useAtproto ( )
7+ const data = ref < PackageLikes | null > ( null )
8+ const error = ref < Error | null > ( null )
9+ const pending = ref ( false )
10+
11+ const mutate = async ( ) => {
12+ pending . value = true
13+ error . value = null
14+
15+ try {
16+ const result = await $fetch < PackageLikes > ( '/api/auth/social/like' , {
17+ method : 'DELETE' ,
18+ body : { packageName } ,
19+ } )
20+
21+ data . value = result
22+ return result
23+ } catch ( e ) {
24+ if ( e instanceof FetchError ) {
25+ await handleAuthError ( e , user . value ?. handle )
26+ }
27+ error . value = e as Error
28+ } finally {
29+ pending . value = false
30+ }
31+ }
32+
33+ return { data, error, pending, mutate }
34+ }
Original file line number Diff line number Diff line change @@ -13,6 +13,9 @@ import { isEditableElement } from '~/utils/input'
1313import { formatBytes } from ' ~/utils/formatters'
1414import { NuxtLink } from ' #components'
1515import { useModal } from ' ~/composables/useModal'
16+ import { useAtproto } from ' ~/composables/atproto/useAtproto'
17+ import { useLikePackage } from ' ~/composables/atproto/useLikePackage'
18+ import { useUnlikePackage } from ' ~/composables/atproto/useUnlikePackage'
1619
1720definePageMeta ({
1821 name: ' package' ,
Original file line number Diff line number Diff line change 1+ import type { FetchError } from 'ofetch'
2+ import { authRedirect } from '~/composables/atproto/useAtproto'
3+
4+ export async function handleAuthError (
5+ fetchError : FetchError ,
6+ userHandle ?: string | null ,
7+ ) : Promise < never > {
8+ const errorMessage = fetchError ?. data ?. message
9+ if ( errorMessage === ERROR_NEED_REAUTH && userHandle ) {
10+ await authRedirect ( userHandle )
11+ }
12+ throw fetchError
13+ }
You can’t perform that action at this time.
0 commit comments