@@ -243,8 +243,22 @@ export const useAuthStore = defineStore('auth', {
243243 async init ( ) {
244244 this . reset ( ) ;
245245 this . initializing = true ;
246+ const kuzzleStore = useKuzzleStore ( ) ;
247+ const kuzzle = kuzzleStore . $kuzzle ;
248+
249+ if ( kuzzle === null ) {
250+ throw new Error ( 'Kuzzle is not initialized' ) ;
251+ }
252+
246253 await this . checkFirstAdmin ( ) ;
247- await this . loginByToken ( ) ;
254+
255+ const sessionId = localStorage . getItem ( 'openid-sessionId' ) ;
256+
257+ if ( sessionId ) {
258+ await this . loginByOpenId ( sessionId ) ;
259+ } else {
260+ await this . loginByToken ( ) ;
261+ }
248262 } ,
249263 async createSingleUseToken ( ) : Promise < string > {
250264 const kuzzleStore = useKuzzleStore ( ) ;
@@ -318,6 +332,45 @@ export const useAuthStore = defineStore('auth', {
318332 const jwt = await kuzzle . auth . login ( 'local' , credentials , '2h' ) ;
319333 return await this . setSession ( jwt ) ;
320334 } ,
335+
336+ async loginByOpenId ( sessionId : string ) {
337+ const kuzzleStore = useKuzzleStore ( ) ;
338+ const kuzzle = kuzzleStore . $kuzzle ;
339+
340+ if ( kuzzle === null ) {
341+ throw new Error ( 'Kuzzle is not initialized' ) ;
342+ }
343+
344+ if ( kuzzleStore . currentEnvironment === null ) {
345+ throw new Error ( 'No current environment selected' ) ;
346+ }
347+
348+ const response = await kuzzle . query ( {
349+ controller : 'auth' ,
350+ action : 'login' ,
351+ strategy : 'keycloak' ,
352+ body : {
353+ sessionId : sessionId ,
354+ callbackUrl : window . location . href ,
355+ } ,
356+ } ) ;
357+
358+ kuzzle . jwt = null ;
359+
360+ if ( response . status === 200 ) {
361+ localStorage . removeItem ( 'openid-sessionId' ) ;
362+ const res = await kuzzle . auth . checkToken ( response . result . jwt ) ;
363+
364+ if ( ! res . valid ) {
365+ kuzzle . jwt = null ;
366+ return await this . setSession ( null ) ;
367+ } else {
368+ kuzzle . jwt = response . result . jwt ;
369+ return await this . setSession ( response . result . jwt ) ;
370+ }
371+ }
372+ } ,
373+
321374 async loginByToken ( ) {
322375 const kuzzleStore = useKuzzleStore ( ) ;
323376 const kuzzle = kuzzleStore . $kuzzle ;
0 commit comments