Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protected Routes / has(), protect(), and <Protect> #48

Open
evdama opened this issue Feb 5, 2024 · 3 comments
Open

Protected Routes / has(), protect(), and <Protect> #48

evdama opened this issue Feb 5, 2024 · 3 comments

Comments

@evdama
Copy link

evdama commented Feb 5, 2024

How would one use has(), protect(), and <Protect> with clerk-sveltekit?

It's not yet implemented if I'm correct?

Here's a link to Clerks Blog post about it: https://clerk.com/blog/introducing-authorization

@dustypomerleau
Copy link

I struggled with this as well. At the moment, my workaround for a <Protect>-like experience is something like:

<script lang="ts">
    import ClerkLoaded from "clerk-sveltekit/client/ClerkLoaded.svelte";
    import SignedIn from "clerk-sveltekit/client/SignedIn.svelte";
</script>

<ClerkLoaded let:clerk>
    <SignedIn>
        {#if clerk?.session?.checkAuthorization({ permission: "my:custom:permission" })}
            <slot />
        {:else}
            <div>
                You don't have permission to view the content for the selected organization.
            </div>
        {/if}
    </SignedIn>
</ClerkLoaded>

@hbcondo
Copy link

hbcondo commented May 23, 2024

Hi, just wanted to follow-up and check the status of this request. We would also greatly benefit from implementing this in clerk-sveltekit.

@wobsoriano
Copy link

wobsoriano commented Jul 29, 2024

Once this PR gets merged, you'll be able to protect your routes/components like this:

In your server routes:

export const load = (event) => {
  const { has } = event.locals.auth
  
  if (!has({ permission: 'org:widgets:create' })) {
    // ...
  }
  
  // ...
}

and in your components:

<script>
  import Protect from 'clerk-sveltekit/client/Protect.svelte'
</script>

<nav>
  <a href="/">Home</a>
  <a href="/widgets">Widgets</a>
  <Protect role="org:admin">
    <a href="/admin">Admin Panel</a>
  </Protect>
</nav>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants