Skip to content

Commit

Permalink
Refactored ensureSession to return the session explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
pastuxso committed Dec 17, 2024
1 parent ad08a6d commit aa9ca70
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
20 changes: 13 additions & 7 deletions src/extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,15 +489,21 @@ export class RunmeExtension {
}

if (kernel.isFeatureOn(FeatureName.RequireStatefulAuth)) {
await StatefulAuthProvider.instance.ensureSession()
StatefulAuthProvider.instance.currentSession().then(async (session) => {
if (session) {
await commands.executeCommand('runme.lifecycleIdentitySelection', RunmeIdentity.ALL)
const logger = getLogger(FeatureName.RequireStatefulAuth)
try {
const session = await StatefulAuthProvider.instance.ensureSession()
const nunmeIdentity = session ? RunmeIdentity.ALL : getServerLifecycleIdentity()
await commands.executeCommand('runme.lifecycleIdentitySelection', nunmeIdentity)
} catch (error) {
let message
if (error instanceof Error) {
message = error.message
} else {
const settingsDefault = getServerLifecycleIdentity()
await commands.executeCommand('runme.lifecycleIdentitySelection', settingsDefault)
message = JSON.stringify(error)
}
})

logger.error(message)
}
}

if (kernel.isFeatureOn(FeatureName.Gist)) {
Expand Down
8 changes: 5 additions & 3 deletions src/extension/provider/statefulAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,23 @@ export class StatefulAuthProvider implements AuthenticationProvider, Disposable
return sessions[0]
}

async ensureSession() {
async ensureSession(): Promise<StatefulAuthSession | undefined> {
let session = await this.currentSession()
if (session) {
StatefulAuthProvider.showLoginNotification()
return
return session
}

session = await StatefulAuthProvider.bootstrapFromToken()
const forceLogin = features.isOnInContextState(FeatureName.ForceLogin) || !!session

const silent = forceLogin ? undefined : true

this.newSession(silent)
return this.newSession(silent)
.then(() => {
if (session) {
StatefulAuthProvider.showLoginNotification()
return session
}
})
.catch((error) => {
Expand All @@ -170,6 +171,7 @@ export class StatefulAuthProvider implements AuthenticationProvider, Disposable
if (forceLogin && message === 'User did not consent to login.') {
authentication.getSession(AuthenticationProviders.Stateful, DEFAULT_SCOPES, {})
}
return error
})
}

Expand Down

0 comments on commit aa9ca70

Please sign in to comment.