panteliselef / astro-with-clerk-auth

Community Astro SDK for Clerk with starter `astro-clerk-auth`
https://astro-clerk.elef.codes
MIT License
44 stars 10 forks source link

Signing out a user #126

Open JeroenReumkens opened 4 months ago

JeroenReumkens commented 4 months ago

Hi!

Thanks for making this package! I'm right now converting my own very poor implementation of this to your package, and really liking it so far! Thanks for all the work you put in 🙏

I'm wondering if it's possible to also add functionality to sign the user out? Preferably by exposing a method we can use on the server to clear the session. A React hook as a second option would definitely be welcome too, although I like to sign out on the server most of the times (eg create a route /auth/logout).

Thank you so much! Happy to make a PR by the way, but might need some pointers on how / where to add it in that case.

panteliselef commented 4 months ago

@JeroenReumkens Thanks for your kind words. Keep the feedback coming :)

Clerk already offers a signOut method client side, so I might build on top of that. For now you should be able to get access to that method directly via the $clerk store.

import { $clerk } from 'astro-clerk-auth/stores';

Probably a useClerk hook will come in handy for the react fans as well.

Regarding the server solution that you mention, are you looking for a way to simply fire a request from the client to your endpoint in order for the user to sign out ?

JeroenReumkens commented 4 months ago

Thanks for getting back to me @panteliselef

In the meantime I indeed found the $clerk store. That definitely works great! I think exposing a small useClerk would indeed be very nice, which simply could return a useStore for the $clerk store. That's what I did on the client now, to be able to trigger the signout object from Clerk.

Until that hook is added I think it will be very useful to mention the $clerk store in the docs, since it gives a lot of useful methods you can use.

Server side

As for the server, I'd like something like:

---
const auth = Astro.locals.auth();
await auth.signOut();
Astro.redirect('/');
---

This way I can also create a page (/logout) which you can visit to also sign out.

panteliselef commented 3 months ago

@JeroenReumkens I've added signOut to useAuth. I'm reluctant on introduce a useClerk hook, how often do you need to grab the $clerk store ?