[Organization]: Setting Active organization with slug fails.

When I try to set my active organization via slug, I getting a 403 'User is not a member of the organization', when I do it with the id it works fine.
4 Replies
Antoni
AntoniOP3mo ago
I calling with authClient.useListOrganizations()all my organization but when I want to set one as active I get an 403 with message User is not a member of the organization. I checked in my db the users as well and he is part of organization. Do i miss something? my snippet:
"use client"
import { useState } from "react"
import { authClient } from "@/lib/auth-client"

export default function MinimalExample() {
const { data: organizations, isPending } = authClient.useListOrganizations()
const [isLoading, setIsLoading] = useState(false)

const handleSetFirstOrg = async () => {
if (!organizations || organizations.length === 0) return

setIsLoading(true)
try {
const firstOrg = organizations[0]
await authClient.organization.setActive({
organizationId: firstOrg.id,
organizationSlug: firstOrg.slug,
})
console.log("First organization set as active")
} catch (error) {
console.error("Error setting active organization:", error)
} finally {
setIsLoading(false)
}
}

if (isPending) return <div>Loading organizations...</div>

return (
<button
onClick={handleSetFirstOrg}
disabled={isLoading || !organizations?.length}
>
{isLoading ? "Setting..." : "Set First Organization Active"}
</button>
)
}
"use client"
import { useState } from "react"
import { authClient } from "@/lib/auth-client"

export default function MinimalExample() {
const { data: organizations, isPending } = authClient.useListOrganizations()
const [isLoading, setIsLoading] = useState(false)

const handleSetFirstOrg = async () => {
if (!organizations || organizations.length === 0) return

setIsLoading(true)
try {
const firstOrg = organizations[0]
await authClient.organization.setActive({
organizationId: firstOrg.id,
organizationSlug: firstOrg.slug,
})
console.log("First organization set as active")
} catch (error) {
console.error("Error setting active organization:", error)
} finally {
setIsLoading(false)
}
}

if (isPending) return <div>Loading organizations...</div>

return (
<button
onClick={handleSetFirstOrg}
disabled={isLoading || !organizations?.length}
>
{isLoading ? "Setting..." : "Set First Organization Active"}
</button>
)
}
This snippet works fine only with id
Abdi
Abdi3mo ago
I'm getting the same error, and this happening in better-auth version 1.3.2 and above, setting it to 1.3.1 fixes it
Abdi
Abdi3mo ago
GitHub
fix(organization): resolve slug when setting active organization by...
closes #3528 Summary by cubic Fixed an issue where setting the active organization did not resolve organization slugs correctly. Now, both organization IDs and slugs are properly handled when upd...

Did you find this page helpful?