Options
All
  • Public
  • Public/Protected
  • All
Menu

StitchAuthListener

StitchAuthListener is an interface for taking action whenever a particular StitchAppClient's authentication state changes.

Implement the methods in this interface to handle these events. You can register your listener with StitchAuth using StitchAuth.addAuthListener.

StitchAuth calls registered listeners when:

  • a user is added to the device for the first time
  • a user logs in
  • a user logs out
  • a user is linked to another identity
  • a listener is registered
  • the active user is changed

Some actions may trigger multiple events. For instance. Logging into a user for the first time will trigger onUserAdded, onUserLoggedIn, and onActiveUserChanged.

Note

The callbacks in this interface are called asynchronously. This means that if many auth events are happening at the same time, events that come in may not necessarily reflect the current state of authentication. In other words, although these methods will be called after events happen, those events may be stale by the time the listener method is called. Always check the state of StitchAuth object for the true authentication state.

Example

// In this example, a custom StitchAuthListener is defined and registered:
const stitchClient = Stitch.defaultAppClient

// Define the listener
const myAuthListener = {
  onUserAdded: (auth, addedUser) => {
    console.log('onUserAdded:', addedUser.profile)
  },
  onUserLoggedIn: (auth, loggedInUser) => {
    console.log('onUserLoggedIn:', loggedInUser.profile)
  },
  onActiveUserChanged: (auth, currentActiveUser, previousActiveUser) => {
    console.log('onActiveUserChanged:', currentActiveUser, previousActiveUser)
  },
  onUserLoggedOut: (auth, loggedOutUser) => {
    console.log('onUserLoggedOut:', loggedOutUser.profile)
  },
  onUserRemoved: (auth, removedUser) => {
    console.log('onUserRemoved:', removedUser.profile)
  },
  onUserLinked: (auth, linkedUser) => {
    console.log('onUserLinked:', linkedUser.profile)
  },
  onListenerRegistered: (auth) => {
    console.log('onListenerRegistered')
  },
}

// Register the listener
const {auth} = stitchClient
auth.addAuthListener(myAuthListener)

// Console:
//   onListenerRegistered

const user = await auth.loginWithCredential(new UserPasswordCredential('user', 'password'))

// Console:
//   onUserAdded
//   onUserLoggedIn
//   onActiveUserChanged

await auth.logout()

// Console:
//   onUserLoggedOut
//   onActiveUserChanged

await auth.removeUserWithId(user.id)

// Console:
//   onUserRemoved

See Also

Hierarchy

  • StitchAuthListener

Index

Methods

Optional onActiveUserChanged

Optional onAuthEvent

Optional onListenerRegistered

  • Called whenever this listener is registered for the first time. This can be useful to infer the state of authentication, because any events that occurred before the listener was registered will not be seen by the listener.

    Parameters

    • auth: StitchAuth

      The instance of StitchAuth where the listener was registered. It can be used to infer the current state of authentication.

    Returns any

Optional onUserAdded

Optional onUserLinked

Optional onUserLoggedIn

Optional onUserLoggedOut

  • Called whenever a user is logged out.

    The user logged out is not necessarily the active user.

    If the user who logged out was the active user, then onActiveUserChanged will be called after this method.

    If the user was an anonymous user, that user will also be removed and onUserRemoved will also be called.

    Parameters

    • auth: StitchAuth

      The instance of StitchAuth where the user was logged out. It can be used to infer the current state of authentication.

    • loggedOutUser: StitchUser

      The user that was logged out.

    Returns any

Optional onUserRemoved

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc