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

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!

1 Like

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

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

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

2 Likes

Dear @kpeacock, 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?

For what it’s worth, in Papyrs, I explicitely set authClient to undefined after logout to recreat it. I added following comment 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.

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

1 Like

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