Single Sign-On
- Configuration - Set the network
/login
page - add login button and send to Tonomy ID/callback
page - receive callback/
home page - check for logged in users
Examples below are for a Reactjs website.
1. Configuration - Set the network
Configure to use a specific network (in this case, the Tonomy demo network). Run this at the javascript root of your app (e.g. App.tsx in Reactjs) so they are set before used
import { api } from '@tonomy/tonomy-id-sdk';
api.setSettings({
ssoWebsiteOrigin: "https://accounts.demo.tonomy.foundation",
blockchainUrl: "https://blockchain-api-demo.tonomy.foundation"
});
2. Login page
On your login page add the "Login with Tonomy ID" button and set it to call the ExternalUser.loginWithTonomy
function when pressed. Set your /callback
page path as shown below. This is where your the user will be redirect to in your application, after they complete the login process.
async function onButtonPress() {
await api.ExternalUser.loginWithTonomy({ callbackPath: '/callback' });
}
Request data sharing
During the login process, you can additionally request information from the user account by adding a dataRequest
object when calling the loginWithTonomy
function.
await api.ExternalUser.loginWithTonomy({ callbackPath: '/callback', dataRequest: { username: true } });
Currently, only the username
is able to be requested. We are working on supporting more data sharing options soon including basic personal information (name, date of birth, etc...) and sharing of data between applications.
Styling the Tonomy login button
To use the Tonomy login button styles, import the stylesheet and use the class tonomy-login-button
on your button.
or
3. Callback page
In your /callback
page, call the ExternalUser.verifyLoginRequest()
function when the page renders. This will catch the login parameters from the URL and return a logged in user object.
// call this when the page loads
// e.g. in useEffect() in Reactjs
const user = await api.ExternalUser.verifyLoginRequest();
4. Home page
On your home page or when your app first loads (App.tsx in reactjs), check if the user is already logged in.
import { api, SdkError, SdkErrors } from '@tonomy/tonomy-id-sdk';
// call this when the page loads
// e.g. in useEffect() in Reactjs
try {
const user = await api.ExternalUser.getUser();
} catch (e) {
if (e instanceof SdkError) {
switch (e.code) {
case SdkErrors.AccountNotFound:
// User has not logged in yet
case SdkErrors.UserNotLoggedIn:
// User logged in but key has expired. User needs to login again
default:
// unexpected error!
}
}
}