# Does anyone have an example for how to use the authClient.logout() method?

**URL:** <https://forum.dfinity.org/t/does-anyone-have-an-example-for-how-to-use-the-authclient-logout-method/8652>\
**Category:** Developers\
**Created:** [November 17, 2021, 9:28pm UTC](https://forum.dfinity.org/t/does-anyone-have-an-example-for-how-to-use-the-authclient-logout-method/8652 "2021-11-17T21:28:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [November 17, 2021, 9:28pm UTC](https://forum.dfinity.org/t/does-anyone-have-an-example-for-how-to-use-the-authclient-logout-method/8652/1 "2021-11-17T21:28:25Z")

</div>

I’m attempting to make a logout button and am trying to use the authClient.logout() method except I don’t know what arguments it takes (if any) and how i should structure the argument I’m coding in javascript. A simple example would be great!

---

<div class="post-metadata">

**Author:** ![Ori](https://avatars.discourse-cdn.com/v4/letter/o/ecae2f/32.png) [@Ori](https://forum.dfinity.org/u/Ori)\
**Post date:** [November 17, 2021, 9:53pm UTC](https://forum.dfinity.org/t/does-anyone-have-an-example-for-how-to-use-the-authclient-logout-method/8652/2 "2021-11-17T21:53:44Z")

</div>

It doesn’t take any arguments, an example from Kyle is here:

> <https://github.com/krpeacock/auth-client-demo/blob/e49918c450c79a4de90341ba2c4ce6a4d37c9d18/src/auth_client_demo_assets/src/views/loggedIn.ts#L42>

Edit: Actually you can optionally pass it a url to forward on to after logging out, see docs:  
[https://erxue-5aaaa-aaaab-qaagq-cai.raw.ic0.app/auth-client/classes/authclient.html#logout](https://erxue-5aaaa-aaaab-qaagq-cai.raw.ic0.app/auth-client/classes/authclient.html#logout)

Eg  
`await authclient.logout({returnTo:”https://xkcd.com/710/”})`

---

<div class="post-metadata">

**Author:** ![alexeychirkov](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/alexeychirkov/32/1991_2.png) [@alexeychirkov](https://forum.dfinity.org/u/alexeychirkov)\
**Post date:** [November 23, 2022, 3:32pm UTC](https://forum.dfinity.org/t/does-anyone-have-an-example-for-how-to-use-the-authclient-logout-method/8652/3 "2022-11-23T15:32:37Z")

</div>

Dear @anon74414410, should I recreate AuthClient after every `await authclient.logout()` call?

Thing is that `"key"` in set in `init()` method of `AuthClient`. This leads to this situation:

1. Fresh page load
2. Initialize AuthClient - inside its `init()` method `"key"` variable is set (does not matter from `KEY_STORAGE_KEY` or or `Ed25519KeyIdentity.generate()`)
3. logout by calling `await authClient.logout()` - at this moment storage is destroyed
4. without refreshing a page - login again. At `authorize-client-success` phase only `KEY_STORAGE_DELEGATION` key is set to storage.
5. We are logged in but we have no `KEY_STORAGE_KEY` in storage.
6. Refresh the page - we are NOT logged in, because we have no `KEY_STORAGE_KEY` in storage

Which is better?

1. set `KEY_STORAGE_KEY` in the same `authorize-client-success` phase next to `KEY_STORAGE_DELEGATION`?
2. recreate AuthClient?

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [November 23, 2022, 5:04pm UTC](https://forum.dfinity.org/t/does-anyone-have-an-example-for-how-to-use-the-authclient-logout-method/8652/4 "2022-11-23T17:04:39Z")

</div>

For what it’s worth, in Papyrs, I explicitely set `authClient` to `undefined` after logout to recreat it. I added following [comment](https://github.com/papyrs/ic/blob/1720315aa8a9917d950f4ea1090e35c09e3cff62/providers/ic/src/providers/auth/auth.providers.ts#L107) in my code about it for my future self:

> Reset local object otherwise next sign in (sign in - sign out - sign in) might not work out - i.e. agent-js might not recreate the delegation or identity if not resetted

In NNS dapp we do not reset `authClient` explicitely because after each logout we reload the `window` anyway, therefore a new client will implicitely be loaded afterwards.

Two distinct solution but in both, a new instance of `authClient` is created.

---

<div class="post-metadata">

**Author:** ![alexeychirkov](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/alexeychirkov/32/1991_2.png) [@alexeychirkov](https://forum.dfinity.org/u/alexeychirkov)\
**Post date:** [November 24, 2022, 9:26am UTC](https://forum.dfinity.org/t/does-anyone-have-an-example-for-how-to-use-the-authclient-logout-method/8652/5 "2022-11-24T09:26:36Z")

</div>

Thanks for sharing your experience.  
In all our products Usergeek/Canistergeek/Configeek I will also create a new AuthClient on logout 👍

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [November 29, 2022, 4:28pm UTC](https://forum.dfinity.org/t/does-anyone-have-an-example-for-how-to-use-the-authclient-logout-method/8652/6 "2022-11-29T16:28:36Z")

</div>

The goal of `logout` is to close all timeouts and reset all the values, but there is no harm in simply discarding the instance and starting over when you start a new session
