- Print
Step3. Authentication
- Print
Shoplive uses user information for the following purposes:
Displaying user names and messages in the chat feature.
Storing logs based on user ID when clicking on pop-ups (announcements, banners, coupons, etc.).
Storing logs based on user ID when clicking on products or the like button.
Storing logs for other user actions based on user ID.
The Shoplive Player
uses both simple authentication and secure authentication methods to integrate user information:
Guest: Can be used without login (subject to client restrictions).
Simple authentication: Allows for quick and easy authentication.
Secure authentication: Applies authentication through a secure authentication token (JWT).
For more detailed information, refer to the authentication token generation guide.
Guest mode
You can run
Shoplive Player
without separate authentication.Guest users can also set a username.
import ShopLiveSDK
class MainViewController: UIViewController {
...
override func viewDidLoad() {
super.viewDidLoad()
ShopLive.configure(with: "{AccessKey}")
}
func playWithGuest() {
var user = ShopLiveCommonUser(userId :"{USER_ID}",name : "{USER_NAME}")
ShopLive.user = user
ShopLive.play(data: .init(campaignKey : "{CampaignKey}"))
}
...
}
Chatting
If you do not allow guest chatting, selecting the chat button will not result in any response.
If you do not require a guest username, it will be randomly assigned.
If you set guest usernames as required and click the chat button, a popup for setting the guest username will appear. You must set a guest username to connect to the chat.
Simple authentication
You can link a user's account in the service through the functionality that allows you to set the user's ID and login username.
You have the option to set the user's ID, name, age, gender, user score, and other custom data information.
import ShopLiveSDK
import ShopLiveSDKCommon
class MainViewController: UIViewController {
...
override func viewDidLoad() {
super.viewDidLoad()
ShopLive.configure(with: "{AccessKey}")
}
func playWithUser() {
var user = ShopLiveCommonUser(
userId: "{USER_ID}",
name: "{USER_NAME}",
gender: .male,
userScore : userScore,
age: 25,
custom : ["key" : "AnyType value"])
ShopLive.user = user
ShopLive.play(data: .init(campaignKey : "{CampaignKey}"))
}
...
}
API Reference: ShopLiveCommonUser
Secure authentication
Using the secret key, issued by Shoplive Admin, you can generate JWT token to configure user information such as user ID and name.
import ShopLiveSDK
class MainViewController: UIViewController {
...
override func viewDidLoad() {
super.viewDidLoad()
ShopLive.configure(with: "{AccessKey}")
}
func playWithAuthToken() {
var jwtToken: String = "{USER_JWT}"
ShopLive.authToken = jwtToken
ShopLive.play(data: .init(campaignKey: "{CampaignKey}"))
}
...
}
Request Shoplive representative to issue a secret key to generate JWT token
You should generate the authentication token (JWT) on the client's server and then provide it to the app client through the Shoplive iOS SDK API.
With the secret key issued by Shoplive admin, you can generate JWT.
ShopLive.authToken = "{USER_JWT}"
API Reference: authToken
Change a user name
When a user changes their username in the Shoplive Player, the Shoplive iOS SDK communicates this change to the client by using the onSetUserName.
func onSetUserName(_ payload: [String : Any]) {
payload.forEach { (key, value) in
print("onSetUserName key: \(key) value: \(value)")
}
}
API Reference: onSetUserName(_ payload:)
Switch from a guest account to a logged-in user account
In the Shoplive Admin page, specifically within the Broadcast details page and the Chat settings area, you can turn off Guest chatting access, allowing only logged-in users to use the chat feature. In situations where a user wants to use the chat feature but is currently on a guest account, they will need to switch from the guest account to a logged-in user account.
In this scenario, the Shoplive iOS SDK
triggers the LOGIN_REQUIRED
callback. Upon receiving this callback, you should redirect the user to your app's login page. Once the user completes the login process and obtains authentication credentials, you can relaunch the Shoplive Player with these credentials. This action will switch the user to a logged-in user account, allowing them to use the chat and other features as a logged-in user.
func handleReceivedCommand(_ command: String, with payload: Any?) {
print("handleReceivedCommand command: \(command) payload: \(String(describing: payload))")
switch command {
case "LOGIN_REQUIRED":
/*
1. Move to the login page
2. When login is successful, call the Shoplive Player again using the simple/secure user authentication
*/
break
default:
break
}
}