OwnID simplifies session management on the client side by leveraging the ownid.js SDK. This SDK automatically handles session management for you, ensuring that sessions are properly established, maintained, and persisted across user interactions.

Session Management with ownid.js

When a user completes an authentication process using the OwnID solution, the SDK automatically creates and manages the session for that user. The session token is securely stored as a cookie, ensuring that the session remains persistent across different pages and visits. This seamless integration allows developers to focus on building user-facing features without worrying about the underlying session management details.

Initializing and Using the SDK in Your Application

To use OwnID’s session management features, simply integrate the ownid.js SDK into your application and start the authentication flow. Here’s how to initialize OwnID and start the authentication process:

<script>
    ((o,w,n,i,d)=>{o[i]=o[i]||(async(...a)=>((o[i].q=o[i].q||[]).push(a),{error:null,data:null})),
    (d=w.createElement("script")).src='https://cdn.ownid.com/sdk/'+n,d.async=1,w.head.appendChild(d)})  
    (window,document,'<appID>','ownid');
</script>

Once the SDK is initialized, you can start the authentication process like this:

ownid('start').then((user) => {
  console.log('User authenticated:', user);
  // The session is automatically managed by OwnID and stored as a cookie
});

Handling Session Expiry and Logout

The ownid.js SDK also handles session expiration and logout automatically. When a session expires, the user will be logged out automatically. You can also trigger a logout explicitly if needed:

ownid.session.revoke().then(() => {
  // The session cookie is cleared and the user is logged out
});

Checking if a User is Logged In

ownid.session.get()
  .then(() => {
    // User is logged in
  })
  .catch(error => {
    // User is logged out
  });

Listening to session events

ownid.events.addHandlers({
	session: {
      onCreated: () => {
          // User got logged in
      },
      onRevoked: () => {
            // User got logged out
      }
	}
});