@@ -30,6 +30,14 @@ import {
3030 createEventBuffer ,
3131 EventBuffer ,
3232} from "../components/form/event-buffer" ;
33+ import {
34+ clearOrgSwitchingFlag ,
35+ getStorageKeyForOrgSwitchingUser ,
36+ LEGACY_STORAGE_TOKEN_KEY ,
37+ raiseOrgSwitchingFlag ,
38+ shouldResumeOrgSwitchingFlow ,
39+ STORAGE_TOKEN_KEY ,
40+ } from "../domain/org" ;
3341
3442export type StorageOption = "memory" | "localStorage" | "cookie" ;
3543
@@ -136,11 +144,6 @@ export const SlashIDContext =
136144 createContext < ISlashIDContext > ( initialContextValue ) ;
137145SlashIDContext . displayName = "SlashIDContext" ;
138146
139- export const LEGACY_STORAGE_TOKEN_KEY = "@slashid/USER_TOKEN" ;
140-
141- export const STORAGE_TOKEN_KEY = ( oid : string ) =>
142- `${ LEGACY_STORAGE_TOKEN_KEY } /${ oid } ` ;
143-
144147const createStorage = ( storageType : StorageOption ) => {
145148 switch ( storageType ) {
146149 case "memory" :
@@ -225,7 +228,11 @@ export const SlashIDProvider = ({
225228 }
226229
227230 setUser ( newUser ) ;
228- storageRef . current ?. setItem ( currentOrgStorageTokenKey , newUser . token ) ;
231+
232+ storageRef . current ?. setItem (
233+ getStorageKeyForOrgSwitchingUser ( newUser ) || currentOrgStorageTokenKey ,
234+ newUser . token
235+ ) ;
229236 } ,
230237 [ state , currentOrgStorageTokenKey ]
231238 ) ;
@@ -254,6 +261,7 @@ export const SlashIDProvider = ({
254261 if ( isNewOidTokenValid ) {
255262 newToken = newOidToken ;
256263 } else {
264+ raiseOrgSwitchingFlag ( ) ;
257265 newToken = await user . getTokenForOrganization ( newOid ) ;
258266 }
259267
@@ -264,6 +272,7 @@ export const SlashIDProvider = ({
264272 setToken ( newToken ) ;
265273 setOid ( newOid ) ;
266274 setOrgSwitchingState ( { state : "idle" } ) ;
275+ clearOrgSwitchingFlag ( ) ;
267276
268277 return new User ( newToken , sidRef . current ) ;
269278 } ,
@@ -621,7 +630,11 @@ export const SlashIDProvider = ({
621630 ] ,
622631 {
623632 until : ( value ) => value !== null ,
624- then : ( ) => {
633+ then : ( foundUser ) => {
634+ if ( foundUser && shouldResumeOrgSwitchingFlow ( oid , foundUser ) ) {
635+ __switchOrganizationInContext ( { oid : foundUser . oid } ) ;
636+ }
637+
625638 setState ( "ready" ) ;
626639 } ,
627640 }
@@ -636,6 +649,8 @@ export const SlashIDProvider = ({
636649 storeUser ,
637650 token ,
638651 validateToken ,
652+ oid ,
653+ __switchOrganizationInContext ,
639654 ] ) ;
640655
641656 const contextValue = useMemo < ISlashIDContext > ( ( ) => {
0 commit comments