# How to use the state parameter in passport-okta-oauth?

**URL:** <https://devforum.okta.com/t/how-to-use-the-state-parameter-in-passport-okta-oauth/10159>\
**Category:** OAuth/OIDC\
**Created:** [July 16, 2020, 6:53am UTC](https://devforum.okta.com/t/how-to-use-the-state-parameter-in-passport-okta-oauth/10159 "2020-07-16T06:53:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mahesh\_kumaran](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@mahesh\_kumaran](https://devforum.okta.com/u/mahesh_kumaran)\
**Post date:** [July 16, 2020, 6:53am UTC](https://devforum.okta.com/t/how-to-use-the-state-parameter-in-passport-okta-oauth/10159/1 "2020-07-16T06:53:55Z")

</div>

I am using `passport-okta-oauth` to implement Okta SSO in my application . I referred the [documentation](https://developer.okta.com/docs/guides/implement-oauth-for-okta/request-access-token/) and came to know about the usage of the `state` parameter. However , when I pass `state` parameter in my request. I get the following error :

```json
{ 'message': 'Unable to verify authorization request state'}

```

Here is the reference to my code:

```nohighlight
app.get('/oauth/redirect/okta', function (req, res, next) {
        // Retriving the state value here 
        let stateString = JSON.parse(Buffer.from(req.query.state, 'base64').toString('ascii'));
        passport.authenticate('okta', { failureRedirect: `${config.oauthDomainUrl}/oauth/failure?provider=okta` }, function (err, user, info) {
            if (req.query.error != undefined) {
                res.redirect('/oauth/failure?provider=okta');
            } else {
                var profileId = user.id;

                /* Profile Id is sent along the URL to know that the user authentication is successful */
                let redirectUrl = `${config.redirectUrl}?pid=${profileId}&orgId=${stateString.orgId}&clientId=${stateString.clientId}`;
                res.redirect(redirectUrl)
            }

        })(req, res, next);
});

app.get('/oauth/okta', function (req, res, next) {
    let stateString = Buffer.from(JSON.stringify(req.query)).toString('base64')
        passport.authenticate('okta',{
            state: stateString // passing the state string here
        },function () {
            /* The request will be redirected to Okta for authentication, so this
                function will not be called. */
        })(req, res, next);
    }
})

```

In the three parameters in the successful redirect `(err, user, info)` . I get the following response

err : `null`   
  
user : `false`  
  
info : `{ 'message': 'Unable to verify authorization request state'}`

Everything worked fine until I passed the `state` parameter.

---

<div class="post-metadata">

**Author:** ![td98401](https://avatars.discourse-cdn.com/v4/letter/t/51bf81/32.png) [@td98401](https://devforum.okta.com/u/td98401)\
**Post date:** [November 23, 2021, 5:18pm UTC](https://devforum.okta.com/t/how-to-use-the-state-parameter-in-passport-okta-oauth/10159/2 "2021-11-23T17:18:13Z")

</div>

I’m running into the exact same problem described here, can anyone offer an explanation or solution? I need to pass some state through to the callback so this is quite an issue.

To add some additional context, I can see that if I do not pass the state parameter, a randomly generated state value is attached to the request on the session property:

```auto
req.session: Session {
  cookie: {
    path: '/',
    _expires: null,
    originalMaxAge: null,
    httpOnly: true,
    secure: false
  },
  '<myhost>.okta.com': { state: 'dzyMg6AO1xFxnwdn33WbnRKT' }
}

```

When I pass my custom state (which is just a string and is allowed as per the documentation) then that key/value pair is missing which causes the error from the oauth-library I am using (I am using passport-oauth2 but I also used passport-okta-oauth and experienced the same problem).

```auto
req.session: Session {
  cookie: {
    path: '/',
    _expires: null,
    originalMaxAge: null,
    httpOnly: true,
    secure: false
  }
}

```

---

<div class="post-metadata">

**Author:** ![j4016](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@j4016](https://devforum.okta.com/u/j4016)\
**Post date:** [January 24, 2022, 11:27am UTC](https://devforum.okta.com/t/how-to-use-the-state-parameter-in-passport-okta-oauth/10159/3 "2022-01-24T11:27:10Z")

</div>

Hi,

I am having the similar issue and would like to know if anyone has had success with this?

---

<div class="post-metadata">

**Author:** ![j4016](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@j4016](https://devforum.okta.com/u/j4016)\
**Post date:** [January 25, 2022, 4:32pm UTC](https://devforum.okta.com/t/how-to-use-the-state-parameter-in-passport-okta-oauth/10159/4 "2022-01-25T16:32:26Z")

</div>

For anyone looking - the solution for me was from here - [Infinite redirect loop, "Invalid authorization request state." · Issue #89 · auth0/passport-auth0 · GitHub](https://github.com/auth0/passport-auth0/issues/89#issuecomment-856971807)

---

<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 8, 2024, 9:24pm UTC](https://devforum.okta.com/t/how-to-use-the-state-parameter-in-passport-okta-oauth/10159/5 "2024-02-08T21:24:02Z")

</div>

Closed on February 8, 2024, 9:24pm UTC


