# Logout via onExecutePostLogin action

**URL:** <https://community.auth0.com/t/logout-via-onexecutepostlogin-action/103002>\
**Category:** Get Help\
**Tags:** sessions, logout\
**Created:** [March 21, 2023, 3:33pm UTC](https://community.auth0.com/t/logout-via-onexecutepostlogin-action/103002 "2023-03-21T15:33:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![torefindsen](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/torefindsen/32/45646_2.png) [@torefindsen](https://community.auth0.com/u/torefindsen)\
**Post date:** [March 21, 2023, 3:33pm UTC](https://community.auth0.com/t/logout-via-onexecutepostlogin-action/103002/1 "2023-03-21T15:33:31Z")

</div>

Hello, I only want to allow logins from verified users - I currently achieve that by denying login in onExecutePostLogin - however I would like to logout the users instead and give them an opportunity to verify their email and then log back in - thus I would like redirect them to the `/oidc/logout` endpoint - but I’m unable to correctly construct the id\_token\_hint that I must pass as a parameter to the logout-endpoint - how do I construct that from the (event, api) parameters pased into onExecutePostLogin ?

```auto
exports.onExecutePostLogin = async (event, api) => {
if (!event.user.email_verified) {

//*logout* unverified user rather than deny them via api.access.deny(..) - this way they 

}
};

```

Thanks a lot for your help!!

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [March 21, 2023, 4:40pm UTC](https://community.auth0.com/t/logout-via-onexecutepostlogin-action/103002/3 "2023-03-21T16:40:17Z")

</div>

Hey there @torefindsen welcome to the community!

> [@torefindsen](#):
>
> how do I construct that from the (event, api) parameters pased into onExecutePostLogin ?

The ID token isn’t available via the `event` or `api` objects from an Action - Tokens will only be returned after the Action code executes.

Hope this helps to clarify!

---

<div class="post-metadata">

**Author:** ![torefindsen](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/torefindsen/32/45646_2.png) [@torefindsen](https://community.auth0.com/u/torefindsen)\
**Post date:** [March 21, 2023, 5:23pm UTC](https://community.auth0.com/t/logout-via-onexecutepostlogin-action/103002/4 "2023-03-21T17:23:37Z")

</div>

Hi @ty.frith - thanks a lot for your reply - but does this mean I cannot log the user out in the PostLogin action? What are my options then for creating a somewhat intuitive worflow for a user that signs-up? Until they have verified their email they will be ‘logged’ in but dnied by my post-login action?

Br  
Tore

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [March 29, 2023, 10:39pm UTC](https://community.auth0.com/t/logout-via-onexecutepostlogin-action/103002/5 "2023-03-29T22:39:16Z")

</div>

Hey @torefindsen sorry for the delayed response here - You won’t be able to log a user out from within a post login action as the user hasn’t successfully logged in until the action is complete.

This is the standard way to approach requiring users to verify their email prior to logging in:

> [@User can sign in before email confirmation](https://community.auth0.com/t/user-can-sign-in-before-email-confirmation/62730/3):
>
> Hi @monika.prakash, Thanks for joining the Community! By default, users can log in to an app without verifying their email, however, the user’s email\_verified attribute will be false until they confirm their email address: { "user\_id": "auth0|507f1f77bcf86cd799439020", "email": "john.doe@gmail.com", "email\_verified": false // \<-- this attribute will be false until user verfies email } In order to require email verification, you can use a [post-login Action](https://auth0.com/docs/actions/triggers/post-login): exports.onExecutePostL…

Users won’t be able to fully log in to your app until they’ve successfully verified their email.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![arne.misbaer](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/arne.misbaer/32/73230_2.png) [@arne.misbaer](https://community.auth0.com/u/arne.misbaer)\
**Post date:** [March 30, 2023, 9:56am UTC](https://community.auth0.com/t/logout-via-onexecutepostlogin-action/103002/6 "2023-03-30T09:56:10Z")

</div>

Hey @ty.frith

We are experiencing the same issue as mentioned is this ticket. And we deny Access to the application. But with denying access we only deny access to the application, but the user stays logged in in Auth0.

And because we did not login the user in the application the user cannot logout to switch the account in Auth0 and is unable to login with a different email address.

Is there a way to logout the user from Auth0 also when denying access to the application?

Kind regards  
Arne

---

<div class="post-metadata">

**Author:** ![Flier7325](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/flier7325/32/72897_2.png) [@Flier7325](https://community.auth0.com/u/Flier7325)\
**Post date:** [April 6, 2023, 6:42am UTC](https://community.auth0.com/t/logout-via-onexecutepostlogin-action/103002/7 "2023-04-06T06:42:39Z")

</div>

I’d also be interested in knowing whether there is a way to log a user out of Auth0 while also denying access.

---

<div class="post-metadata">

**Author:** ![gparascandolo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/gparascandolo/32/38788_2.png) [@gparascandolo](https://community.auth0.com/u/gparascandolo)\
**Post date:** [April 7, 2023, 6:13pm UTC](https://community.auth0.com/t/logout-via-onexecutepostlogin-action/103002/8 "2023-04-07T18:13:20Z")

</div>

Correct me if I am wrong here @ty.frith, but would they be able redirect users to the tenant’s logout endpoint via the Post-Login Action’s `api.redirect.sendUserTo` function?

i.e.

```auto
exports.onExecutePostLogin = async (event, api) => {
  if (!event.user.email_verified) {
    api.redirect.sendUserTo('https://TENANT_DOMAIN/v2/logout', {
      query: { returnTo: 'WHITELISTED_LOGOUT_URL' }
    });
  }
};

```

The `WHITELISTED_LOGOUT_URL` can be a custom error page they’ve set up to notify the user that they must verify their email address before logging in.

Although I understand that this won’t work for post-login flows initiated with `prompt=none` as noted in: [Redirect with Actions](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow/redirect-with-actions#flows-where-prompt-none)

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [April 7, 2023, 6:44pm UTC](https://community.auth0.com/t/logout-via-onexecutepostlogin-action/103002/9 "2023-04-07T18:44:44Z")

</div>

Hey @gparascandolo thanks for jumping in here!

That totally makes sense to me and seems a good solution - Something similar is mentioned here:

> [@Api.access.deny causes redirect loop](https://community.auth0.com/t/api-access-deny-causes-redirect-loop/104298/4):
>
> Thanks @rueben.tiow Our solution: we don’t use api.access.deny() anymore but use api.redirect.sendUserTo('https://{auth0\_tenant}.eu.auth0.com/v2/logout?returnTo=https%3A%2F%2Fexample.com'). With this, the not-allowed user will be logged out immediately after the login and gets redirected to the specified returnTo URL.

Hope this helps everyone!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [April 21, 2023, 6:44pm UTC](https://community.auth0.com/t/logout-via-onexecutepostlogin-action/103002/10 "2023-04-21T18:44:51Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
