Skip to content

Commit

Permalink
moved authentication fetchTokenThenJoin up the file, agoramanager asy…
Browse files Browse the repository at this point in the history
…nc joinchannel will prioritise tokenUrl. added more param pages to documentation. added navigation title
  • Loading branch information
maxxfrazer committed Jul 19, 2023
1 parent 518a38d commit 1a716f5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set -eo pipefail
JSON_FILE_PATH="./AgoraManager/config.json"

# Read the JSON file contents
JSON_CONTENTS=$(cat "${JSON_FILE_PATH}")
JSON_CONTENTS=$(git show :${JSON_FILE_PATH})

# Extract the value of the "appId" key from the JSON
APP_ID_VALUE=$(echo "${JSON_CONTENTS}" | jq -r '.appId')
Expand Down
16 changes: 6 additions & 10 deletions AgoraManager/AgoraManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,20 @@ open class AgoraManager: NSObject, ObservableObject, AgoraRtcEngineDelegate {
}

@discardableResult
open func joinChannel(_ channel: String) async -> Int32 {
if let rtcToken = DocsAppConfig.shared.rtcToken, !rtcToken.isEmpty {
return self.joinChannel(
channel, token: DocsAppConfig.shared.rtcToken,
uid: DocsAppConfig.shared.uid, info: nil
)
}
var token: String?
open func joinChannel(_ channel: String, uid: UInt? = nil) async -> Int32 {
let userId = uid ?? DocsAppConfig.shared.uid
var token = DocsAppConfig.shared.rtcToken
if !DocsAppConfig.shared.tokenUrl.isEmpty {
do {
token = try await self.fetchToken(
from: DocsAppConfig.shared.tokenUrl, channel: channel, role: self.role
from: DocsAppConfig.shared.tokenUrl, channel: channel,
role: self.role, userId: userId
)
} catch {
print("token server fetch failed: \(error.localizedDescription)")
}
}
return self.joinChannel(channel, token: token, uid: DocsAppConfig.shared.uid, info: nil)
return self.joinChannel(channel, token: token, uid: userId, info: nil)
}

/// Leaves the channel and stops the preview for the session.
Expand Down
4 changes: 2 additions & 2 deletions Example-App/Docs-Examples/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ struct ContentView: View {
CustomCameraInputView(continueTo: CustomAudioVideoView.self)
}
NavigationLink("Audio and voice effects") {
EmptyView()
Text("Not yet implemented.")
}
}
}
}.navigationTitle("Video SDK Samples")
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions Example-App/Docs-Examples/Documentation.docc/Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ Take a look through each class to see how to implement all the features with Ago

- ``TokenAuthenticationView``
- ``CallQualityView``
- ``CloudProxyView``
- ``MediaEncryptionView``
- ``ScreenShareAndVolumeView``
- ``CustomAudioVideoView``

### Passing Parameters

- ``ChannelInputView``
- ``TokenAuthInputView``
- ``EncryptionKeysInputView``
- ``CustomCameraInputView``
- ``ProxyInputView``
34 changes: 18 additions & 16 deletions authentication-workflow/TokenAuthenticationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ public extension AgoraManager {

return tokenResponse.rtcToken
}

/// Fetch a token from the token server, and then join the channel using Agora SDK.
/// - Returns: A boolean, for whether or not the token fetching was successful.
func fetchTokenThenJoin(tokenUrl: String, channel: String) async -> Bool {
if let token = try? await self.fetchToken(
from: tokenUrl, channel: channel,
role: role, userId: 0
) {
agoraEngine.joinChannel(byToken: token, channelId: channel, info: nil, uid: 0)
return true
} else { return false }
}
}

/// A Codable struct representing the token server response.
Expand All @@ -45,7 +57,6 @@ public struct TokenResponse: Codable {

/// A view that authenticates the user with a token and joins them to a channel using Agora SDK.
struct TokenAuthenticationView: View {

/// The Agora SDK manager.
@ObservedObject var agoraManager = AgoraManager(appId: DocsAppConfig.shared.appId, role: .broadcaster)
/// The channel ID to join.
Expand Down Expand Up @@ -82,23 +93,14 @@ struct TokenAuthenticationView: View {
Text("Error fetching token.")
}
}.onAppear {
/// On joining, call ``TokenAuthenticationView/fetchTokenThenJoin()``.
Task { tokenPassed = await fetchTokenThenJoin() }
Task {
/// On joining, call ``AgoraManager/fetchTokenThenJoin(tokenUrl:channel:)``.
tokenPassed = await agoraManager.fetchTokenThenJoin(
tokenUrl: tokenUrl, channel: channelId
)
}
}.onDisappear { agoraManager.leaveChannel() }
}

/// Fetch a token from the token server, and then join the channel using Agora SDK
/// - Returns: A boolean, for whether or not the token fetching was successful.
func fetchTokenThenJoin() async -> Bool {
if !channelId.isEmpty,
let token = try? await self.agoraManager.fetchToken(
from: self.tokenUrl, channel: self.channelId,
role: self.agoraManager.role, userId: 0
) {
self.agoraManager.joinChannel(channelId, token: token)
return true
} else { return false }
}
}

struct TokenAuthenticationView_Previews: PreviewProvider {
Expand Down

0 comments on commit 1a716f5

Please sign in to comment.