Skip to content

Commit

Permalink
fix updated cookies were never serialized
Browse files Browse the repository at this point in the history
  • Loading branch information
KishanBagaria committed Oct 9, 2022
1 parent 1e38d9d commit c39d635
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
31 changes: 11 additions & 20 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ import { eventUrnToMessageID, urnID } from './util'
export type SendMessageResolveFunction = (value: Message[]) => void

export default class LinkedIn implements PlatformAPI {
private currentUser = null

user: CurrentUser

private cookies: any

private searchedUsers: User[]

private realTimeApi: null | LinkedInRealTime = null
Expand All @@ -29,36 +25,31 @@ export default class LinkedIn implements PlatformAPI {

readonly api = new LinkedInAPI()

private afterAuth = async (cookies: any) => {
this.api.setLoginState(CookieJar.fromJSON(cookies))
const currentUser = await this.api.getCurrentUser()
if (!currentUser) throw new ReAuthError()
this.user = mapCurrentUser(currentUser)
}

init = async (serialized: { cookies: any }) => {
const { cookies } = serialized || {}
if (!cookies) return
this.cookies = cookies

await this.api.setLoginState(CookieJar.fromJSON(cookies))

this.currentUser = await this.api.getCurrentUser()
if (!this.currentUser) throw new ReAuthError()

this.user = mapCurrentUser(this.currentUser)
await this.afterAuth(cookies)
}

login = async ({ cookieJarJSON }): Promise<LoginResult> => {
if (!cookieJarJSON?.cookies?.some(({ key }) => key === LinkedInAuthCookieName)) return { type: 'error', errorMessage: 'No authentication cookie was found' }

await this.api.setLoginState(CookieJar.fromJSON(cookieJarJSON))

this.currentUser = await this.api.getCurrentUser()
this.user = mapCurrentUser(this.currentUser)
this.cookies = cookieJarJSON

await this.afterAuth(cookieJarJSON)
return { type: 'success' }
}

serializeSession = () => ({ cookies: this.cookies })
serializeSession = () => ({ cookies: this.api.cookieJar.toJSON() })

logout = () => this.api.logout()

getCurrentUser = () => mapCurrentUser(this.currentUser)
getCurrentUser = () => this.user

updateThreadSeenMap = (threadID: string, participantID: string, messageID: string, seenAt: string) => {
if (!this.threadSeenMap.has(threadID)) this.threadSeenMap.set(threadID, new Map())
Expand Down
2 changes: 1 addition & 1 deletion src/lib/linkedin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class LinkedInAPI {
// key is threadID, values are participantIDs
conversationsParticipants: Record<string, string[]> = {}

setLoginState = async (cookieJar: CookieJar) => {
setLoginState = (cookieJar: CookieJar) => {
if (!cookieJar) throw TypeError()
this.cookieJar = cookieJar
}
Expand Down

0 comments on commit c39d635

Please sign in to comment.