@@ -65,8 +65,11 @@ You can now extract the `organizations` claim from ID tokens in the way you norm
65
65
For example, in React you could use:
66
66
67
67
``` 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
+ })
70
73
` ` `
71
74
72
75
## Step 2: Build the switcher
@@ -76,18 +79,30 @@ To build a simple list of orgs, use something like the following React example.
76
79
In this example, we’ve also included a check to see if this is the current organization.
77
80
78
81
` ` ` 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])
80
95
81
96
< ul>
82
- {getClaim ( " organizations " , " idToken " ). value .map ((item ) => (
97
+ {orgs .map ((item ) => (
83
98
< li key= {item .id }>
84
- < button onClick= {() => login ({orgCode: item .id })} type= " button" >
99
+ < button onClick= {() => login ({ orgCode: item .id })} type= ' button' >
85
100
{item .name }
86
- {getOrganization (). orgCode === item .id ? " (Current)" : null }
101
+ {currentOrgCode === item .id ? ' (Current)' : null }
87
102
< / button>
88
103
< / li>
89
104
))}
90
- < / ul> ;
105
+ < / ul>
91
106
` ` `
92
107
93
108
With some extra styling, a switcher might look something like this:
0 commit comments