feat: create atproto oauth login
This commit is contained in:
59
src/hooks/useATProtoLogin.hook.ts
Normal file
59
src/hooks/useATProtoLogin.hook.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { computed, ref } from 'vue'
|
||||
|
||||
import { getAuthor } from '@/modules/atproto/getAuthor'
|
||||
import { restoreSession, sdkSignOut, signInWithHandle } from '@/modules/atproto/service/atprotoOAuth'
|
||||
import { clearSession, loadSession, saveSession } from '@/modules/atproto/service/atprotoSession'
|
||||
|
||||
const did = ref<string | null>(null)
|
||||
const handle = ref<string | null>(null)
|
||||
|
||||
let init = true
|
||||
|
||||
const initializeAuth = async () => {
|
||||
const session = await restoreSession()
|
||||
|
||||
if (session) {
|
||||
const author = await getAuthor(session.did)
|
||||
const resolvedHandle = author?.handle ?? ''
|
||||
|
||||
did.value = session.did
|
||||
handle.value = resolvedHandle
|
||||
await saveSession(session.did, resolvedHandle)
|
||||
} else {
|
||||
const stored = await loadSession()
|
||||
did.value = stored?.did ?? ''
|
||||
handle.value = stored?.handle ?? ''
|
||||
}
|
||||
}
|
||||
|
||||
export const useATProtoLogin = () => {
|
||||
if (init) {
|
||||
init = false
|
||||
initializeAuth()
|
||||
}
|
||||
|
||||
const isLoggedIn = computed(() => !!did.value)
|
||||
const isATProtoReady = computed(() => did.value !== null)
|
||||
|
||||
const signIn = async (inputHandle: string): Promise<void> => {
|
||||
await signInWithHandle(inputHandle)
|
||||
}
|
||||
|
||||
const signOut = async (): Promise<void> => {
|
||||
if (did.value) {
|
||||
await sdkSignOut(did.value)
|
||||
}
|
||||
await clearSession()
|
||||
did.value = ''
|
||||
handle.value = ''
|
||||
}
|
||||
|
||||
return {
|
||||
did,
|
||||
handle,
|
||||
isLoggedIn,
|
||||
isATProtoReady,
|
||||
signIn,
|
||||
signOut,
|
||||
}
|
||||
}
|
||||
21
src/hooks/useFollows.hook.ts
Normal file
21
src/hooks/useFollows.hook.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { Ref, ref, watch } from 'vue'
|
||||
|
||||
import { getFollows } from '@/modules/atproto/service/getFollows'
|
||||
|
||||
export const useFollows = (did: Ref<string | null>) => {
|
||||
const follows = ref<Set<string>>(new Set())
|
||||
|
||||
watch(
|
||||
did,
|
||||
async (value) => {
|
||||
if (value) {
|
||||
follows.value = await getFollows(value)
|
||||
} else {
|
||||
follows.value = new Set()
|
||||
}
|
||||
},
|
||||
{ immediate: true },
|
||||
)
|
||||
|
||||
return { follows }
|
||||
}
|
||||
@@ -3,7 +3,12 @@ import { PublicNoteListItem } from "@/modules/note/models/Note"
|
||||
import { computedAsync } from "@vueuse/core"
|
||||
import { computed, ref, Ref } from "vue"
|
||||
|
||||
export function usePublicNoteList(did?: Ref<string | undefined>) {
|
||||
interface UsePublicNoteListOptions {
|
||||
did?: Ref<string | undefined>
|
||||
followsFilter?: Ref<Set<string>>
|
||||
}
|
||||
|
||||
export function usePublicNoteList(options?: UsePublicNoteListOptions) {
|
||||
const isLoading = ref(false)
|
||||
const notes = ref<PublicNoteListItem[]>([])
|
||||
const cursor = ref<string | null | undefined>(null)
|
||||
@@ -12,7 +17,7 @@ export function usePublicNoteList(did?: Ref<string | undefined>) {
|
||||
const onLoadMore = async () => {
|
||||
isLoading.value = true
|
||||
|
||||
const path = did?.value ? `/${did.value}/notes` : "/notes"
|
||||
const path = options?.did?.value ? `/${options.did.value}/notes` : "/notes"
|
||||
const noteAPI = new URL(path, "https://api.litenote.li212.fr")
|
||||
|
||||
if (cursor.value) {
|
||||
@@ -39,8 +44,14 @@ export function usePublicNoteList(did?: Ref<string | undefined>) {
|
||||
const getAuthor = (did: string) =>
|
||||
authors.value.has(did) ? authors.value.get(did)?.handle : ""
|
||||
|
||||
const filteredNotes = computed(() => {
|
||||
const filter = options?.followsFilter?.value
|
||||
if (!filter || filter.size === 0) return notes.value
|
||||
return notes.value.filter((n) => filter.has(n.did))
|
||||
})
|
||||
|
||||
return {
|
||||
notes,
|
||||
notes: filteredNotes,
|
||||
isLoading,
|
||||
canLoadMore,
|
||||
onLoadMore,
|
||||
|
||||
Reference in New Issue
Block a user