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

CF SDK: OAuth refresh token #982

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading