Back

Appwrite auth state changes via stream

  • 0
  • Tools
  • Accounts
ZiaChoudhary
1 Oct, 2023, 06:57

Firehase hae a beautiful function to provide a stream of auth state changes. I didn't find it's alternative in Appwrite. May soneone help me in achieving this functionality. The only work around in my mind is just to create a timer and after every srcond it will call and send data from account.get() as a stream but its a very horrible solution i think.

Is not there any best alternate?

TL;DR
The user is asking for help in implementing a functionality related to Appwrite auth state changes using the "realtime" feature in JavaScript. They provided some code but are unsure if their approach is correct. They are also asking if there is a better alternative to achieve this functionality, as they couldn't find a similar feature in Appwrite. Solution: - The user can use the "account" channel in Appwrite to get updates related to account events. - They can refer to the documentation for examples on how to use the "realtime" feature in Appwrite: [https://appwrite.io/docs/apis/realtime#examples
D5
1 Oct, 2023, 07:04
D5
1 Oct, 2023, 07:05

You can get updates related to account by using the "account" channel

Francisco "Klogan" Barros
17 Dec, 2023, 13:29

@D5 can we elaborate a bit further on this? According to the documentation, there should be examples on how to use realtime on Authentication flows, but there aren't. Ignoring the vue/nuxt parts of the code below, do you think the approach is correct to listen to anonymous user becomming authenticated, and authenticated user logging out, using realtime auth events in JS?

TypeScript
import type { Models, RealtimeResponseEvent } from 'appwrite'

import { useAppwrite } from './useAppwrite'

type User = Models.User<Models.Preferences>
type UseAppwriteAuthStateChangedOptions = {
  onAuthenticated: (payload: User) => void
  onUnauthenticated: (payload: User) => void
  onSubscribed?: () => void
  onUnsubscribed?: () => void
}

const ACCOUNT_CHANNEL = 'account'

function isCreateSession(events: string[], _payload: unknown): _payload is User {
  return events.includes('users.*.sessions.*.create')
}

function isDeleteSession(events: string[], _payload: unknown): _payload is User {
  return events.includes('users.*.sessions.*.delete')
}

export function useAppwriteAuthStateChanged({
  onAuthenticated,
  onSubscribed,
  onUnauthenticated,
  onUnsubscribed,
}: UseAppwriteAuthStateChangedOptions) {
  const appwrite = useAppwrite()
  const unsubscribeCb = ref<() => void>(() => undefined)

  function handleUpdate({ events, payload }: RealtimeResponseEvent<unknown>) {
    if (isCreateSession(events, payload)) {
      onAuthenticated(payload)
      resubscribe()
    } else if (isDeleteSession(events, payload)) {
      onUnauthenticated(payload)
      resubscribe()
    }
  }

  function subscribe() {
    unsubscribeCb.value = appwrite.client.subscribe(ACCOUNT_CHANNEL, handleUpdate)
    onSubscribed?.()
  }

  function resubscribe() {
    unsubscribe()
    subscribe()
  }

  function unsubscribe() {
    unsubscribeCb.value()
    onUnsubscribed?.()
  }

  onMounted(() => subscribe())
  onUnmounted(() => unsubscribe())

  return {
    unsubscribe,
  }
}
D5
17 Dec, 2023, 15:02

Hello, could you please create a separate post with this?

Francisco "Klogan" Barros
17 Dec, 2023, 15:02

Certainly.

D5
17 Dec, 2023, 15:02

Ping me please once you've done it

Francisco "Klogan" Barros
17 Dec, 2023, 15:16

Pinged on the new thread. I added some context on what I am trying to achieve, probably not very interesting for the question, but it may help if you already know someone who has passed through this problems

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more