Create User
Before you log in to the user, you must add the user to CometChat.- For proof of concept/MVPs: Create the user using the CometChat Dashboard.
- For production apps: Use the CometChat Create User API to create the user when your user signs up in your app.
Sample UsersWe have setup 5 users for testing having UIDs:
cometchat-uid-1
, cometchat-uid-2
, cometchat-uid-3
, cometchat-uid-4
and cometchat-uid-5
.login()
method.
We recommend you call the CometChat login method once your user logs into your app. The login method needs to be called only once but the getLoggedInUser() needs to be checked every-time when the app starts and if it returns null then you need to call the login method.
Login using Auth Key
This simple authentication procedure is useful when you are in development or if you do not require additional security. The login method needs to be called in the following scenarios:- When the user is logging to the App for the first time.
- If the CometChat.getLoggedInUser() function returns nil.
If you are using v2.0.6 of SDK or greater please make sure you add the check of CometChat.getLoggedInUser() function in your app where you check App’s user login status. In case it returns nil then you need to call the Login method inside it.
Parameter | Description |
---|---|
UID | The UID of the user that you would like to login |
authKey | CometChat Auth Key |
login()
method returns the User
object containing all the information of the logged-in user.
Login using Auth Token
This advanced authentication procedure does not use the auth Key directly in your client code and thus ensuring that your auth Key is not leaked even if the client code is reverse engineered.- Create a user via the CometChat API when the user signs up in your app.
- Create an Auth Token via the CometChat API for the new user and save the token in your database.
- Load the Auth Token in your client and pass it to the
login()
method.
- When the user is logging to the App for the first time.
- If the CometChat.getLoggedInUser() function returns nil.
If you are using v2.0.6 of SDK or greater please make sure you add the check of CometChat.getLoggedInUser() function in your app where you check App’s user login status. In case it returns nil then you need to call the Login method inside it.
Parameter | Description |
---|---|
authToken | Auth Token of the user you would like to login |
login()
method returns the User
object containing all the information of the logged-in user.
Logout
You can use thelogout()
method to log out the user from CometChat.