Skip to content

Commit d007b8d

Browse files
Merge pull request #517 from Upatric/patch-1
Update navigate-between-organizations.mdx
2 parents 4cd6439 + ff2a522 commit d007b8d

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/content/docs/authenticate/manage-authentication/navigate-between-organizations.mdx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@ You can now extract the `organizations` claim from ID tokens in the way you norm
6565
For example, in React you could use:
6666

6767
```jsx
68-
const {getClaim} = useKindeAuth();
69-
console.log(getClaim("organizations", "id_token").value);
68+
const { getClaim } = useKindeAuth()
69+
70+
getClaim('organizations', 'idToken').then((organizations) => {
71+
console.log('value:', organizations?.value)
72+
})
7073
```
7174
7275
## Step 2: Build the switcher
@@ -76,18 +79,30 @@ To build a simple list of orgs, use something like the following React example.
7679
In this example, we’ve also included a check to see if this is the current organization.
7780
7881
```jsx
79-
const {getClaim, getOrganization} = useKindeAuth();
82+
const { login, getClaim, getOrganization } = useKindeAuth()
83+
84+
const [orgs, setOrgs] = useState<{ id: string; name: string }[]>([])
85+
const [currentOrgCode, setCurrentOrgCode] = useState<string | null>(null)
86+
87+
useEffect(() => {
88+
getClaim('organizations', 'idToken').then((organizations) => {
89+
setOrgs(organizations?.value ?? [])
90+
})
91+
getOrganization().then((org) => {
92+
setCurrentOrgCode(org)
93+
})
94+
}, [getClaim, getOrganization])
8095

8196
<ul>
82-
{getClaim("organizations", "idToken").value.map((item) => (
97+
{orgs.map((item) => (
8398
<li key={item.id}>
84-
<button onClick={() => login({orgCode: item.id})} type="button">
99+
<button onClick={() => login({ orgCode: item.id })} type='button'>
85100
{item.name}
86-
{getOrganization().orgCode === item.id ? " (Current)" : null}
101+
{currentOrgCode === item.id ? ' (Current)' : null}
87102
</button>
88103
</li>
89104
))}
90-
</ul>;
105+
</ul>
91106
```
92107
93108
With some extra styling, a switcher might look something like this:

0 commit comments

Comments
 (0)