11import { namedNode , NamedNode , sym } from 'rdflib'
22import { appContext , offlineTestID } from './authUtil'
33import * as debug from '../util/debug'
4- import { EVENTS , Session } from '@inrupt/solid-client-authn-browser '
4+ import { SessionWithLegacyEvents } from '../authSession/authSession '
55import { AuthenticationContext , AuthnLogic } from '../types'
66
77export class SolidAuthnLogic implements AuthnLogic {
8- private session : Session
8+ private session : SessionWithLegacyEvents
99
10- constructor ( solidAuthSession : Session ) {
10+ constructor ( solidAuthSession : SessionWithLegacyEvents ) {
1111 this . session = solidAuthSession
1212 }
1313
1414 // we created authSession getter because we want to access it as authn.authSession externally
15- get authSession ( ) :Session { return this . session }
15+ get authSession ( ) : SessionWithLegacyEvents { return this . session }
1616
1717 currentUser ( ) : NamedNode | null {
1818 const app = appContext ( )
1919 if ( app . viewingNoAuthPage ) {
2020 return sym ( app . webId )
2121 }
22- if ( this && this . session && this . session . info && this . session . info . webId && this . session . info . isLoggedIn ) {
23- return sym ( this . session . info . webId )
22+ const sessionAny = this . session as any
23+ const webId = sessionAny ?. info ?. webId || sessionAny ?. webId
24+ const isLoggedIn = sessionAny ?. info ?. isLoggedIn ?? sessionAny ?. isActive ?? Boolean ( webId )
25+ if ( this && this . session && webId && isLoggedIn ) {
26+ return sym ( webId )
2427 }
2528 return offlineTestID ( ) // null unless testing
2629 }
@@ -40,21 +43,51 @@ export class SolidAuthnLogic implements AuthnLogic {
4043 if ( preLoginRedirectHash ) {
4144 window . localStorage . setItem ( 'preLoginRedirectHash' , preLoginRedirectHash )
4245 }
43- this . session . events . on ( EVENTS . SESSION_RESTORED , ( url ) => {
44- debug . log ( `Session restored to ${ url } ` )
45- if ( document . location . toString ( ) !== url ) history . replaceState ( null , '' , url )
46- } )
46+ const sessionAny = this . session as any
47+ if ( typeof sessionAny ?. events ?. on === 'function' ) {
48+ // Backward-compatible hook for auth clients exposing an EventEmitter-style API.
49+ sessionAny . events . on ( 'sessionRestore' , ( url : string ) => {
50+ debug . log ( `Session restored to ${ url } ` )
51+ if ( document . location . toString ( ) !== url ) history . replaceState ( null , '' , url )
52+ } )
53+ }
4754
4855 /**
4956 * Handle a successful authentication redirect
5057 */
5158 const redirectUrl = new URL ( window . location . href )
5259 redirectUrl . hash = ''
53- await this . session
54- . handleIncomingRedirect ( {
60+ if ( typeof sessionAny ?. handleIncomingRedirect === 'function' ) {
61+ await sessionAny . handleIncomingRedirect ( {
5562 restorePreviousSession : true ,
5663 url : redirectUrl . href
5764 } )
65+ } else {
66+ if ( typeof sessionAny ?. restore === 'function' ) {
67+ const wasActive = sessionAny ?. isActive ?? Boolean ( sessionAny ?. webId )
68+ try {
69+ await sessionAny . restore ( )
70+ } catch ( error ) {
71+ const message = error instanceof Error ? error . message : String ( error )
72+ if ( ! / n o s e s s i o n t o r e s t o r e / i. test ( message ) ) {
73+ throw error
74+ }
75+ debug . log ( 'No previous session to restore' )
76+ }
77+ const isNowActive = sessionAny ?. isActive ?? Boolean ( sessionAny ?. webId )
78+ if ( ! wasActive && isNowActive ) {
79+ sessionAny . events ?. emit ( 'sessionRestore' , window . location . href )
80+ }
81+ }
82+ if ( typeof sessionAny ?. handleRedirectFromLogin === 'function' ) {
83+ const wasActive = sessionAny ?. isActive ?? Boolean ( sessionAny ?. webId )
84+ await sessionAny . handleRedirectFromLogin ( )
85+ const isNowActive = sessionAny ?. isActive ?? Boolean ( sessionAny ?. webId )
86+ if ( ! wasActive && isNowActive ) {
87+ sessionAny . events ?. emit ( 'login' )
88+ }
89+ }
90+ }
5891
5992 // Check to see if a hash was stored in local storage
6093 const postLoginRedirectHash = window . localStorage . getItem ( 'preLoginRedirectHash' )
@@ -81,7 +114,7 @@ export class SolidAuthnLogic implements AuthnLogic {
81114 return Promise . resolve ( setUserCallback ? setUserCallback ( me ) : me )
82115 }
83116
84- const webId = this . webIdFromSession ( this . session . info )
117+ const webId = this . webIdFromSession ( sessionAny ?. info || sessionAny )
85118 if ( webId ) {
86119 me = this . saveUser ( webId )
87120 }
@@ -119,8 +152,17 @@ export class SolidAuthnLogic implements AuthnLogic {
119152 /**
120153 * @returns {Promise<string|null> } Resolves with WebID URI or null
121154 */
122- webIdFromSession ( session ?: { webId ?: string , isLoggedIn : boolean } ) : string | null {
123- const webId = session ?. webId && session . isLoggedIn ? session . webId : null
155+ webIdFromSession ( session ?: { webId ?: string , isLoggedIn ?: boolean , isActive ?: boolean } ) : string | null {
156+ const webId = session ?. webId
157+ if ( ! webId ) {
158+ return null
159+ }
160+ if ( typeof session ?. isLoggedIn === 'boolean' ) {
161+ return session . isLoggedIn ? webId : null
162+ }
163+ if ( typeof session ?. isActive === 'boolean' ) {
164+ return session . isActive ? webId : null
165+ }
124166 return webId
125167 }
126168
0 commit comments