Handle Events
Handle a Successful Login Event
Section titled “Handle a Successful Login Event”Logging in takes place without a page refresh, so you need a way to handle a successful login event. To do so, set a callback as the first and only argument of onLogin. The callback receives the BazaarApp instance:
bzr.onLogin(async () => { // Handle user log in // e.g. Set local state and redirect});For more information about logging in a user, see the authentication section.
Handle a Login Error Event
Section titled “Handle a Login Error Event”Handle a login error event:
bzr.onLoginError(async (bzr, message) => { // Handle log in error // e.g. Show error message console.error("Login error:", message);});Handle a Successful Connection Event
Section titled “Handle a Successful Connection Event”Execute a callback function when a successful connection to the API is established, and each time it is established again after a connection loss.
bzr.onApiConnect(async () => { console.log("Connected to the API successfully.");});Handle a Connection Error Event
Section titled “Handle a Connection Error Event”Execute a callback function when the server refuses the user, e.g. because the token expired or is invalid, or the user hasn’t finished signing up. It is safe to log out here:
bzr.onApiConnectError(async (bzr, message) => { console.error("Connection error:", message); bzr.logOut();});The callback is not called when the network or the server is down. The connection keeps trying by itself and restores your subscriptions once it’s back. Calls made meanwhile wait up to 10 seconds for the connection, then throw a BazaarError.