# Automatic Session signout using okta-auth-js

**URL:** <https://devforum.okta.com/t/automatic-session-signout-using-okta-auth-js/24882>\
**Category:** Questions\
**Created:** [July 21, 2023, 9:25am UTC](https://devforum.okta.com/t/automatic-session-signout-using-okta-auth-js/24882 "2023-07-21T09:25:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel-optimizely](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/daniel-optimizely/32/4858_2.png) [@daniel-optimizely](https://devforum.okta.com/u/daniel-optimizely)\
**Post date:** [July 21, 2023, 9:25am UTC](https://devforum.okta.com/t/automatic-session-signout-using-okta-auth-js/24882/1 "2023-07-21T09:25:39Z")

</div>

We’re developing an SPA and we’re using okta-auth-js to login to our own auth domain, maintained by our company.

We have a requirement on the application to force a signout whenever the current session surpasses 2h.

So we constructed the okta auth client code this way:

```auto
new OktaAuth({
      issuer: env.oktaIssuer,
      clientId: env.oktaClientId,
      redirectUri: redirectUri,
      postLogoutRedirectUri: postLogoutRedirectUri,
      scopes: ['openid', 'profile', 'email', 'offline_access'],
      // https://github.com/okta/okta-auth-js#services
      services: {
        autoRenew: true,
        autoRemove: true,
        syncStorage: true
      },
      // https://github.com/okta/okta-auth-js#authorize-options
      maxAge: 7200
    })

```

We were expecting that the maxAge parameter would be enough to force a signout, but it’s not working. Session and Tokens seem to be two different moving pieces in the okta-auth-js code, so disabling Token renewal didn’t force a signout iirc.

We ended up implementing the auto signout code ourselves, using a setInterval check that decodes the id or access token and extracts the auth\_time claim, (which in our understanding is similar to the session creation time) to determine if enough time has passed to signout the user.

But we’re not convinced this is the best approach; has someone had this requirement before and how did/would you solve it?

---

<div class="post-metadata">

**Author:** ![AlexandreUnca](https://avatars.discourse-cdn.com/v4/letter/a/a9adbd/32.png) [@AlexandreUnca](https://devforum.okta.com/u/AlexandreUnca)\
**Post date:** [July 21, 2023, 12:38pm UTC](https://devforum.okta.com/t/automatic-session-signout-using-okta-auth-js/24882/2 "2023-07-21T12:38:43Z")

</div>

hello,  
in Security ==\> global session policy then you can add policy with rules and timeout of the session.  
I never used it but may be it can be for you?

---

<div class="post-metadata">

**Author:** ![daniel-optimizely](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/daniel-optimizely/32/4858_2.png) [@daniel-optimizely](https://devforum.okta.com/u/daniel-optimizely)\
**Post date:** [July 21, 2023, 12:59pm UTC](https://devforum.okta.com/t/automatic-session-signout-using-okta-auth-js/24882/3 "2023-07-21T12:59:46Z")

</div>

Hi! Another team has access to the okta session settings, but as far as I’m aware, the global session policy is currently set up at ‘infinite’ iirc, but I believe the session timeout for our app is at 2h… it doesn’t seem to work with okta-auth-js though.

---

<div class="post-metadata">

**Author:** ![ramgan](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@ramgan](https://devforum.okta.com/u/ramgan)\
**Post date:** [July 21, 2023, 2:11pm UTC](https://devforum.okta.com/t/automatic-session-signout-using-okta-auth-js/24882/4 "2023-07-21T14:11:21Z")

</div>

`maxAge` parameter in `OktaClient` setting is intended to be sent during authorize call. You can find the `max_age` parameter in [this](https://developer.okta.com/docs/reference/api/oidc/#request-parameters) document.

Currently there is no inbuilt functionality for timed sign outs. However you can setup your access token/refresh token combo to expire in 2 hours and the app session will expire and will need re-authentication at the end of it. When you make an authorize call at this point with `maxAge` parameter set to 2 hours, Okta will challenge the user again for authentication even though Global session might be active.

---

<div class="post-metadata">

**Author:** ![daniel-optimizely](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/daniel-optimizely/32/4858_2.png) [@daniel-optimizely](https://devforum.okta.com/u/daniel-optimizely)\
**Post date:** [July 24, 2023, 11:57am UTC](https://devforum.okta.com/t/automatic-session-signout-using-okta-auth-js/24882/5 "2023-07-24T11:57:18Z")

</div>

I’ve set the maxAge and disabled token refresh like so:

```auto
const authClient = new OktaAuth({
    issuer: env.oktaIssuer,
    clientId: env.oktaClientId,
    redirectUri: redirectUri,
    postLogoutRedirectUri: postLogoutRedirectUri,
    scopes: ['openid', 'profile', 'email', 'offline_access'],
    // https://github.com/okta/okta-auth-js#services
    services: {
      autoRenew: false,
      autoRemove: true,
      syncStorage: true
    },
    tokenManager: {
      autoRenew: false
    },
    // https://github.com/okta/okta-auth-js#authorize-options
    maxAge: 7200
  });

```

What I observed was:

1. Tokens were not set with a 2h expiration time; they seem to default to 1h
2. After tokens expired, I was not redirected to sign-in page automatically, but when I refreshed the page, I was redirected to the sign-in page

With #1, I don’t think maxAge is changing anything, am I doing something worng?

But regarding #1, is there anything OktaAuth should be doing to redirect to sign-in page, or is there something I can manually call from the SDK, e.g. listen on tokenManager expire event?

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [February 15, 2024, 11:36pm UTC](https://devforum.okta.com/t/automatic-session-signout-using-okta-auth-js/24882/6 "2024-02-15T23:36:40Z")

</div>

Closed on February 15, 2024, 11:36pm UTC


