> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ownid.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Frontend SDK

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:

```javascript JavaScript theme={null}
<script>
    ((o,w,n,i,d)=>{o[i]=o[i]||((...a) =>new Promise((r, j)=>((o[i].q = o[i].q || []).push([{r,j,_isResolvers:true},...a])))),
    (d=w.createElement("script")).src='https://cdn.ownid.com/sdk/'+n,d.async=1,w.head.appendChild(d);o[i].whenReady = o[i]('init')})
    (window,document,'<appID>','ownid');
</script>
```

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

```javascript theme={null}
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:

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

### Checking if a User is Logged In

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

### Listening to session events

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

```
