Javascript SDK
SDK Callback vs Promise
We provide you the flexibility to use either approache when signing in to verify.
Callback
With this approach, the callbacks for onSuccess and onError can be handled in the initialization process of the SDK. This is a valid approach if you are using the default SDK UI component.
callbacks Example
var sdk = Streambird('pk_test_KJuRUZmh1XC342h1n39gH84MuSZDyD13NfhtDkaY6IfwpQA0H', {
headless: false,
networkConfig: {
rpcUrl: 'https://goerli.infura.io/v3/INFURA_API_KEY'
}
});
sdk.init(
...
callbacks: {
onSuccess: async (data) => {
console.log("callback onSuccess", data)
const isLogged = await sdk.sessions.isLoggedIn()
console.log('isLogged:', isLogged)
const accounts = await sdk.rpcProvider.send('eth_accounts', [])
console.log('Iframe accounts:', { accounts })
const user = await sdk.users.getCurrentUser()
console.log('User:', user)
},
onError: (data) => {
console.log("callback onError", data);
}
}
)
Promise
You may have designed your own login UI and need to hook up the login / sign in button event. This will not be possible with the callbacks approach so we have provided a promise
approach.
Promise Example
var sdk = Streambird('pk_test_KJuRUZmh1XC342h1n39gH84MuSZDyD13NfhtDkaY6IfwpQA0H', {
headless: false,
networkConfig: {
rpcUrl: 'https://goerli.infura.io/v3/INFURA_API_KEY'
}
});
sdk.init(...);
const handleLogin = async (e) => {
e.preventDefault();
const email = new FormData(e.target).get("email");
console.log('email', email);
if(email) {
try {
let result = await sdk.magicLinks.loginOrCreate(email);
console.log('result from sdk-constructor', result);
const rpcProvider = sdk.rpcProvider
localStorage.setItem('rpc', rpcProvider)
console.log('RpcProvider: ', rpcProvider)
} catch(error) {
console.log('error', error);
}
}
};
With the above code you initialize the sdk
and then use the sdk
to login when you press the button and use await
to wait for the authentication login response.