Skip to content

Commit 01ec043

Browse files
dr0niusdprothero
authored andcommitted
Adding snippets for Chat token management (TwilioDevEd#748)
* Adding snippets for Chat token management Particularly addressing three scenarios: - initializing the client with initial token - updating the token with provided string - using the built-in token lifecycle events * Listener fix * Add token refresh samples for Obj-C and Swift * Make fetchToken async
1 parent 98591ae commit 01ec043

15 files changed

+127
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import com.twilio.chat.ChatClient;
2+
import com.twilio.chat.ChatClientListener;
3+
4+
ChatClient.Properties.Builder builder = new ChatClient.Properties.Builder();
5+
ChatClient.Properties properties = builder.createProperties();
6+
// See below for client event listener implementation
7+
ChatClientListener listener = new Listener();
8+
9+
// Make a secure request to your backend to retrieve an access token.
10+
// Use an authentication mechanism to prevent token exposure to 3rd parties.
11+
12+
String accessToken = "<your token here>";
13+
14+
ChatClient.create(getApplicationContext(), accessToken, properties, listener);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const Chat = require('twilio-chat');
2+
3+
// Make a secure request to your backend to retrieve an access token.
4+
// Use an authentication mechanism to prevent token exposure to 3rd parties.
5+
6+
const accessToken = '<your accessToken>';
7+
8+
Chat.Client.create(accessToken)
9+
.then(client => {
10+
// Use Programmable Chat client
11+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#import <TwilioChatClient/TwilioChatClient.h>
2+
3+
// Make a secure request to your backend to retrieve an access token.
4+
// Use an authentication mechanism to prevent token exposure to 3rd parties.
5+
6+
NSString *accessToken = @"<your token here>";
7+
8+
[TwilioChatClient chatClientWithToken:accessToken
9+
properties:properties delegate:delegate completion:(TCHResult *result, TwilioChatClient *client) {
10+
if (![result isSuccessful]) {
11+
// warn the user the initialization didn't succeed
12+
}
13+
}];
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import TwilioChatClient
2+
3+
// Make a secure request to your backend to retrieve an access token.
4+
// Use an authentication mechanism to prevent token exposure to 3rd parties.
5+
6+
let accessToken = "<your token here>"
7+
8+
TwilioChatClient.chatClient(withToken: accessToken,
9+
properties: nil, delegate: self) {
10+
(result, chatClient) in
11+
if (!result.isSuccessful()) {
12+
// warn the user the initialization didn't succeed
13+
}
14+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"title": "Initializing Chat SDK",
3+
"type": "mobile"
4+
}

ip-messaging/tokens/renewal/meta.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"title": "Responding to Renewal Events",
3+
"type": "mobile"
4+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Listener implements ChatClientListener {
2+
@Override public void onTokenAboutToExpire()
3+
{
4+
// Implement fetchToken() to make a secure request to your backend to retrieve a refreshed access token.
5+
// Use an authentication mechanism to prevent token exposure to 3rd parties.
6+
fetchToken((String updatedToken) -> {
7+
chatClient.updateToken(updatedToken, new StatusListener {
8+
@Override public void onSuccess() {
9+
System.out.println("Token renewed successfully");
10+
}
11+
});
12+
});
13+
}
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
chatClient.on('tokenAboutToExpire', function() {
2+
// Implement fetchToken() to make a secure request to your backend to retrieve a refreshed access token.
3+
// Use an authentication mechanism to prevent token exposure to 3rd parties.
4+
fetchToken(function(updatedToken) {
5+
chatClient.updateToken(updatedToken);
6+
});
7+
});

ip-messaging/tokens/renewal/renewal.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
- (void)chatClientTokenWillExpire:(TwilioChatClient *)chatClient {
2+
// Implement fetchToken() to make a secure request to your backend to retrieve a refreshed access token.
3+
// Use an authentication mechanism to prevent token exposure to 3rd parties.
4+
[self fetchToken:completion:^(NSString *updatedToken) {
5+
[chatClient updateToken:updatedToken
6+
completion:^(TCHResult * _Nonnull result) {
7+
if (!result.isSuccessful) {
8+
NSLog(@"Error updating token: %@", result.error);
9+
}
10+
}];
11+
}]
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func chatClientTokenWillExpire(_ chatClient: TwilioChatClient) {
2+
// Implement fetchToken() to make a secure request to your backend to retrieve a refreshed access token.
3+
// Use an authentication mechanism to prevent token exposure to 3rd parties.
4+
fetchToken(completion: { (updatedToken) -> (Void) in
5+
if let updatedToken = updatedToken {
6+
chatClient.updateToken(updatedToken, completion: { (result) in
7+
if !result.isSuccessful() {
8+
print("Error updating token: \(String(describing: result.error?.description))")
9+
return
10+
}
11+
})
12+
}
13+
})
14+
}

ip-messaging/tokens/update/meta.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"title": "Updating a Token in Chat SDK",
3+
"type": "mobile"
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
chatClient.updateToken(accessToken, new StatusListener {
2+
@Override public void onSuccess() {
3+
System.out.println("Token updated successfully");
4+
}
5+
});

ip-messaging/tokens/update/update.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
chatClient.updateToken(accessToken);

ip-messaging/tokens/update/update.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[chatClient updateToken:accessToken completion:^(TCHResult *result) {
2+
if (![result isSuccessful]) {
3+
// warn the user the update didn't succeed
4+
}
5+
}];
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
chatClient.updateToken(accessToken) { (result) in
2+
if (!result.isSuccessful()) {
3+
// warn the user the update didn't succeed
4+
}
5+
}

0 commit comments

Comments
 (0)