@@ -2,8 +2,8 @@ import type { Dispatch, PropsWithChildren, SetStateAction } from 'react'
22import { createContext , useContext , useEffect , useRef , useState } from 'react'
33import { z } from 'zod'
44import keycloakInstance , { KeycloakService } from '../util/keycloak'
5- import { createPersonalOrganization } from '../mutations/users/organization_mutations'
65import { getAPIServiceConfig } from '../config/config'
6+ import { OrganizationService } from '../service/users/OrganizationService'
77
88const IdTokenClaimsSchema = z . object ( {
99 sub : z . string ( ) . uuid ( ) ,
@@ -19,20 +19,6 @@ const IdTokenClaimsSchema = z.object({
1919 } ) . optional ( )
2020} )
2121
22- export const fakeToken = IdTokenClaimsSchema . default ( {
23- sub : '18159713-5d4e-4ad5-94ad-fbb6bb147984' ,
24- 25- email_verified : true ,
26- name : 'Max Mustermann' ,
27- preferred_username : 'max.mustermann' ,
28- given_name : 'Max' ,
29- family_name : 'Mustermann' ,
30- organization : {
31- id : '3b25c6f5-4705-4074-9fc6-a50c28eba406' ,
32- name : 'helpwave fake'
33- }
34- } )
35-
3622const config = getAPIServiceConfig ( )
3723
3824const UserFromIdTokenClaims = IdTokenClaimsSchema . transform ( ( obj ) => ( {
@@ -63,15 +49,15 @@ const tokenToUser = (token: string): User | null => {
6349
6450 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
6551 // @ts -ignore
66- if ( Object . keys ( decoded ? .organization ) . length === 0 ) delete decoded . organization
52+ if ( decoded . organization && Object . keys ( decoded . organization ) . length === 0 ) delete decoded . organization
6753
6854 const parsed = UserFromIdTokenClaims . safeParse ( decoded )
6955 return parsed . success ? parsed . data : null
7056}
7157
7258type AuthContextValue = {
7359 user ?: User ,
74- setUser ?: Dispatch < SetStateAction < User | undefined > > ,
60+ setUser ?: Dispatch < SetStateAction < User | undefined > > ,
7561 token ?: string ,
7662 organization ?: {
7763 id : string ,
@@ -87,8 +73,10 @@ const defaultAuthContextValue: AuthContextValue = {
8773 token : undefined ,
8874 organization : undefined ,
8975 organizations : [ ] ,
90- signOut : ( ) => { } ,
91- redirectUserToOrganizationSelection : ( ) => { }
76+ signOut : ( ) => {
77+ } ,
78+ redirectUserToOrganizationSelection : ( ) => {
79+ }
9280}
9381
9482const AuthContext = createContext < AuthContextValue > ( defaultAuthContextValue )
@@ -120,11 +108,49 @@ export const ProvideAuth = ({ children }: PropsWithChildren) => {
120108
121109 useEffect ( ( ) => {
122110 if ( config . fakeTokenEnable ) {
123- const user = tokenToUser ( config . fakeToken )
111+ const user : User | null = tokenToUser ( config . fakeToken )
124112 if ( ! user ) throw new Error ( 'Invalid fake token' )
125- didInit . current = true
126- setUser ( user )
127- setToken ( config . fakeToken )
113+ try {
114+ OrganizationService . getForUser ( ) . then ( organizations => {
115+ console . log ( `Found ${ organizations . length } organizations for fake token user` )
116+ if ( organizations . length > 0 ) {
117+ const organization = organizations [ 0 ] !
118+ console . log ( `Using ${ organization . longName } for user.` , organization )
119+ setUser ( ( ) => ( {
120+ ...user ,
121+ organization : {
122+ id : organization . id ,
123+ name : organization . longName ,
124+ }
125+ } ) )
126+ setToken ( config . fakeToken )
127+ didInit . current = true
128+ } else {
129+ console . log ( 'Creating a new organization' )
130+ OrganizationService . create ( {
131+ id : '' ,
132+ 133+ longName : 'Test Organization' ,
134+ shortName : 'Test-Org' ,
135+ avatarURL : 'https://helpwave.de/favicon.ico' ,
136+ isPersonal : false ,
137+ isVerified : false ,
138+ } ) . then ( organization => {
139+ setUser ( ( ) => ( {
140+ ...user ,
141+ organization : {
142+ id : organization . id ,
143+ name : organization . longName ,
144+ }
145+ } ) )
146+ setToken ( config . fakeToken )
147+ didInit . current = true
148+ } )
149+ }
150+ } )
151+ } catch ( e ) {
152+ console . error ( 'Use auth with fake token failed' , e )
153+ }
128154 return
129155 }
130156
@@ -147,7 +173,7 @@ export const ProvideAuth = ({ children }: PropsWithChildren) => {
147173
148174 if ( ! user ?. organization ?. id ) {
149175 console . info ( 'after init keycloak, no organization found, creating personal' )
150- createPersonalOrganization ( )
176+ OrganizationService . createPersonal ( )
151177 . then ( ( ) => {
152178 keycloakInstance ?. updateToken ( - 1 )
153179 . then ( ( ) => {
0 commit comments