Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
en:comet:authentication [2017/07/16 16:30]
levhav
en:comet:authentication [2019/12/18 02:05] (current)
Line 1: Line 1:
 <​rst>​EN::​002-API::​005-Users’ authorization on the comet server</​rst>​ <​rst>​EN::​002-API::​005-Users’ authorization on the comet server</​rst>​
 +<​rst>​Header:​ Authorization on the CppComet </​rst>​
  
 ====== Users’ authorization on the comet-server ====== ====== Users’ authorization on the comet-server ======
  
-Besides of channels where each user, who know channel’s ​name, can subscribe it, here is an opportunity to authorize users on the comet-server and send private messages to users by their IDs.  User authorization involved several steps. The first step – it is sending user’s ID and a random hash in your system to the comet-server.+Besides of channels where each user, who know name of channel, can subscribe it, here is an opportunity to authorize users on the comet-server and to send private messages to users by their IDs.  User authorization involved several steps. The first step – it is sending user’s ID and a random hash in your system to the comet-server.
  
 <code sql> <code sql>
Line 24: Line 25:
 While messages from authorized users are sending by its ID ([[en:​comet:​cometql|insert query to table users_messages]]),​ messages is sending to all user’s devices (up to 16), where he is authorizing at this moment. It’s very convenient when some user has visited your site and has authorized here by more than one device. While messages from authorized users are sending by its ID ([[en:​comet:​cometql|insert query to table users_messages]]),​ messages is sending to all user’s devices (up to 16), where he is authorizing at this moment. It’s very convenient when some user has visited your site and has authorized here by more than one device.
   ​   ​
-If some user at that moment is offline, ​than sent message is placed into a queue of messages and would be delivered when it user will be online.+If some user at that moment is offline, ​then sent message is placed into a queue of messages and would be delivered when it user will be online.
  
-The main purpose of mentioned queue is delivering messages after a short-term user’s offline. For example, in those cases when user is updating some site’s ​page where was opened a connection, and this user goes offline approximately for one second.+The main purpose of mentioned queue is delivering messages after a short-term user’s offline. For example, in those cases when user is updating some page in site where was opened a connection, and this user goes offline approximately for one second.
  
 ====== The subscription to receiving private messages ====== ====== The subscription to receiving private messages ======
Line 34: Line 35:
 The msg channel refers to [[en:​comet:​javascript_api:​pipe-types|channels list with special properties]]. The msg channel refers to [[en:​comet:​javascript_api:​pipe-types|channels list with special properties]].
  
 +======= Determining the authorization status =======
 +
 +You can track down changing authorization status on comet server from JavaScript API.
 +<code JavaScript>​
 +// Adding the callback function for notification about successful authorization
 +cometApi.onAuthSuccess(function(){
 +    console.log("​Connection and authorization were successful"​)
 +})
 +
 +// Adding the callback function for notification about unsuccessful authorization
 +cometApi.onAuthFalill(function(){
 +    console.log("​Connection was successful, but no authorization found"​)
 +})
 +</​code>​
 +These functions will be called when authorization status will be changing. I.e. at least once an authorization will be successful or unsuccessful when connecting to comet server or while status will be changed. ​
 +
 +Also, you can call the function “isAuthorized” to determine authorization status.
 +<code JavaScript>​cometApi.isAuthorized()</​code>​
 +The function “isAuthorized” may return 3 different values: ​
 +  * false – authorization is not complete;
 +  * true - authorization is complete;
 +  * undefined – the status in undefined – this answer returns even before an attempt to connect to comet server.
 +
 +====== Online demo ======
 +
 +[[https://​github.com/​CppComet/​auth-example|code of online demo]] [[https://​cppcomet.github.io/​auth-example/​index.html|online demo on github]] [[https://​codepen.io/​Levhav/​pen/​XaWLra|Look in codepen.io]]
 +
 +<​html>​
 +<iframe height='​800'​ scrolling='​no'​ title='​CppComet auth chat example'​ src='//​codepen.io/​Levhav/​embed/​XaWLra/?​height=265&​theme-id=dark&​default-tab=js,​result&​embed-version=2'​ frameborder='​no'​ allowtransparency='​true'​ allowfullscreen='​true'​ style='​width:​ 100%;'>​See the Pen <a href='​https://​codepen.io/​Levhav/​pen/​XaWLra/'>​CppComet auth chat example</​a>​ by Trapenok Victor (<a href='​https://​codepen.io/​Levhav'>​@Levhav</​a>​) on <a href='​https://​codepen.io'>​CodePen</​a>​.
 +</​iframe>​
 +</​html>​