Skip to content

Commit

Permalink
CF SDK: OAuth refresh token (#982)
Browse files Browse the repository at this point in the history
* OAuth refresh token

* include changeset
  • Loading branch information
iAmmar7 authored Mar 12, 2024
1 parent 19281d4 commit ded3dc7
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 20 deletions.
6 changes: 6 additions & 0 deletions .changeset/heavy-moose-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@signalwire/core': patch
'@signalwire/js': patch
---

CF SDK: OAuth refresh token
8 changes: 4 additions & 4 deletions packages/core/src/BaseJWTSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from './RPCMessages'
import { SessionOptions } from './utils/interfaces'
import { BaseSession } from './BaseSession'
import { authExpiringAction } from './redux/actions'
import { authExpiringAction, reauthAction } from './redux/actions'
import { type SWCloseEvent, isSATAuth } from './utils'

export class BaseJWTSession extends BaseSession {
Expand Down Expand Up @@ -150,14 +150,14 @@ export class BaseJWTSession extends BaseSession {
if (!this.expiresAt) {
return
}
const refreshTokenFn =
this.options._onRefreshToken || this.options.onRefreshToken
const refreshTokenFn = this.options.onRefreshToken
if (this.expiresIn <= this._refreshTokenNotificationDiff) {
this.dispatch(authExpiringAction())

if (typeof refreshTokenFn === 'function') {
try {
await refreshTokenFn()
const token = await refreshTokenFn()
this.dispatch(reauthAction({ token }))
} catch (error) {
this.logger.error(error)
}
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/redux/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export type ReduxComponent = WebRTCCall | Message
export interface ComponentState {
byId: {
[key: string]: ReduxComponent
},
}
}

export interface SessionState {
Expand Down Expand Up @@ -120,6 +120,7 @@ export type SessionChannelAction =
| PayloadAction<void, 'auth/success'>
| PayloadAction<void, 'auth/expiring'>
| PayloadAction<{ error: SessionAuthError }>
| PayloadAction<{ token: string }>
| PayloadAction<SessionAuthStatus>

export type SwEventChannel = MulticastChannel<MapToPubSubShape<SwEventParams>>
Expand Down
11 changes: 2 additions & 9 deletions packages/core/src/utils/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,8 @@ export interface SessionOptions {
// From `LogLevelDesc` of loglevel to simplify our docs
/** logging level */
logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'silent'
/** To refresh the auth token */
onRefreshToken?(): Promise<void>
/**
* The SDK invokes this method and uses the new token to re-auth.
* TODO: rename it: getNewToken, getRefreshedToken, fetchToken (?)
*
* @internal
* */
_onRefreshToken?(): Promise<void>
/** The SDK invokes this method and uses the new token to re-auth. */
onRefreshToken?(): Promise<string>
sessionChannel?: SessionChannel
/** Unified eventing is required only with Call Fabric SDK */
unifiedEventing?: boolean
Expand Down
7 changes: 1 addition & 6 deletions packages/js/src/fabric/WSClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@ export class WSClient {

constructor(public options: WSClientOptions) {
this.wsClient = createClient<RoomSession>({
host: this.options.host,
token: this.options.token,
debug: {
logWsTraffic: true,
},
logLevel: 'debug',
...this.options,
unifiedEventing: true,
})
}
Expand Down

0 comments on commit ded3dc7

Please sign in to comment.