Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(snjs): add error logs when not able to create session #2240

Merged
merged 1 commit into from Mar 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 36 additions & 15 deletions packages/snjs/lib/Services/Session/SessionManager.ts
Expand Up @@ -44,7 +44,7 @@ import {
HttpSuccessResponse,
} from '@standardnotes/responses'
import { CopyPayloadWithContentOverride } from '@standardnotes/models'
import { LegacySession, MapperInterface, Session, SessionToken } from '@standardnotes/domain-core'
import { LegacySession, MapperInterface, Result, Session, SessionToken } from '@standardnotes/domain-core'
import { KeyParamsFromApiResponse, SNRootKeyParams, SNRootKey, CreateNewRootKey } from '@standardnotes/encryption'
import { Subscription } from '@standardnotes/security'
import * as Common from '@standardnotes/common'
Expand Down Expand Up @@ -682,17 +682,20 @@ export class SNSessionManager

const user = sharePayload.user

const session = this.createSession(
const sessionOrError = this.createSession(
sharePayload.accessToken,
sharePayload.accessExpiration,
sharePayload.refreshToken,
sharePayload.refreshExpiration,
sharePayload.readonlyAccess,
)
if (sessionOrError.isFailed()) {
console.error(sessionOrError.getError())

if (session !== null) {
await this.populateSession(rootKey, user, session, sharePayload.host)
return
}

await this.populateSession(rootKey, user, sessionOrError.getValue(), sharePayload.host)
}

private async populateSession(
Expand Down Expand Up @@ -724,17 +727,26 @@ export class SNSessionManager
rootKey: SNRootKey
wrappingKey?: SNRootKey
}): Promise<void> {
const session = this.createSession(
const sessionOrError = this.createSession(
dto.session.access_token,
dto.session.access_expiration,
dto.session.refresh_token,
dto.session.refresh_expiration,
dto.session.readonly_access,
)
if (sessionOrError.isFailed()) {
console.error(sessionOrError.getError())

if (session !== null) {
await this.populateSession(dto.rootKey, dto.user, session, this.apiService.getHost(), dto.wrappingKey)
return
}

await this.populateSession(
dto.rootKey,
dto.user,
sessionOrError.getValue(),
this.apiService.getHost(),
dto.wrappingKey,
)
}

/**
Expand All @@ -755,16 +767,25 @@ export class SNSessionManager
await this.populateSession(rootKey, user, sessionOrError.getValue(), this.apiService.getHost(), wrappingKey)
}
} else if (data.session) {
const session = this.createSession(
const sessionOrError = this.createSession(
data.session.access_token,
data.session.access_expiration,
data.session.refresh_token,
data.session.refresh_expiration,
data.session.readonly_access,
)
if (session !== null && user) {
await this.populateSession(rootKey, user, session, this.apiService.getHost(), wrappingKey)
if (sessionOrError.isFailed()) {
console.error(sessionOrError.getError())

return
}
if (!user) {
console.error('No user in response')

return
}

await this.populateSession(rootKey, user, sessionOrError.getValue(), this.apiService.getHost(), wrappingKey)
}
}

Expand All @@ -774,25 +795,25 @@ export class SNSessionManager
refreshTokenValue: string,
refreshExpiration: number,
readonlyAccess: boolean,
): Session | null {
): Result<Session> {
const accessTokenOrError = SessionToken.create(accessTokenValue, accessExpiration)
if (accessTokenOrError.isFailed()) {
return null
return Result.fail(`Could not create session: ${accessTokenOrError.getError()}`)
}
const accessToken = accessTokenOrError.getValue()

const refreshTokenOrError = SessionToken.create(refreshTokenValue, refreshExpiration)
if (refreshTokenOrError.isFailed()) {
return null
return Result.fail(`Could not create session: ${refreshTokenOrError.getError()}`)
}
const refreshToken = refreshTokenOrError.getValue()

const sessionOrError = Session.create(accessToken, refreshToken, readonlyAccess)
if (sessionOrError.isFailed()) {
return null
return Result.fail(`Could not create session: ${sessionOrError.getError()}`)
}

return sessionOrError.getValue()
return Result.ok(sessionOrError.getValue())
}

override getDiagnostics(): Promise<DiagnosticInfo | undefined> {
Expand Down