Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var user = {};
  2. var sinchClient = new SinchClient({
  3.     applicationKey: "<hiddenkey>",
  4.     capabilities: {
  5.         messaging: true
  6.     },
  7.     startActiveConnection: true
  8. });
  9. var messages = [];
  10. var messageClient = sinchClient.getMessageClient();
  11.  
  12. var sessionName = "sinch-" + sinchClient.applicationKey;
  13.  
  14. location.reload = {};
  15.  
  16. messageClient.addEventListener({
  17.     onMessageDelivered: function (messageDeliveryInfo) {
  18.         console.log("Delivered!", messageDeliveryInfo);
  19.     },
  20.     onIncomingMessage: function (message) {
  21.         document.body.innerHTML += (message.textBody + " from " + message.senderId + "<br>");
  22.     }
  23. });
  24.  
  25. function updateUserInformation() {
  26.     document.getElementById("currentUser__username").innerHTML = user.username;
  27. }
  28.  
  29. var sessionObject = JSON.parse(window.localStorage[sessionName] || "{}");
  30. if (sessionObject.userId !== null || typeof sessionObject.userId !== undefined) {
  31.     sinchClient.start(sessionObject).then(function () {
  32.         user.username = sessionObject.userId;
  33.         updateUserInformation();
  34.         window.localStorage[sessionName] = JSON.stringify(sinchClient.getSession());
  35.     }).fail(function (error) {
  36.         alert(error.message);
  37.     })
  38. }
  39.  
  40.  
  41. window.onload = function () {
  42.     document.getElementById("local__registerForm").onsubmit = function () {
  43.         firebase.auth().createUserWithEmailAndPassword((function () {
  44.             //Email vetting
  45.             return document.getElementById("registerForm__emailField").value.trim()
  46.         })(), (function () {
  47.             //Password vetting
  48.             return document.getElementById("registerForm__passwordField").value.trim()
  49.         })()).then(function () {
  50.             firebase.auth().currentUser.updateProfile({
  51.                 displayName: (function () {
  52.                     //Username vetting
  53.                     return document.getElementById("registerForm__usernameField").value.trim()
  54.                 })()
  55.             }).then(function () {
  56.                 sinchClient.newUser({
  57.                     username: document.getElementById("registerForm__usernameField").value.trim(),
  58.                     password: document.getElementById("registerForm__passwordField").value.trim()
  59.                 }, function (ticket) {
  60.                     user.username = document.getElementById("registerForm__usernameField").value.trim();
  61.                     updateUserInformation();
  62.                     sinchClient.start(ticket);
  63.                     window.localStorage[sessionName] = JSON.stringify(sinchClient.getSession());
  64.                 }).fail(function (error) {
  65.                     alert(error.message);
  66.                 })
  67.             })
  68.         })
  69.         return false;
  70.     }
  71.  
  72.  
  73.     document.getElementById("local__loginForm").onsubmit = function () {
  74.         firebase.auth().signInWithEmailAndPassword((function () {
  75.             //Email vetting
  76.             return document.getElementById("loginForm__emailField").value.trim()
  77.         })(), (function () {
  78.             //Password vetting
  79.             return document.getElementById("loginForm__passwordField").value.trim()
  80.         })()).then(function () {
  81.  
  82.             sinchClient.start({
  83.                 username: firebase.auth().currentUser.displayName,
  84.                 password: document.getElementById("loginForm__passwordField").value.trim()
  85.             }).then(function () {
  86.                 user.username = firebase.auth().currentUser.displayName;
  87.                 updateUserInformation();
  88.                 window.localStorage[sessionName] = JSON.stringify(sinchClient.getSession());
  89.             }).fail(function (error) {
  90.                 alert(error.message);
  91.             })
  92.         }).catch(function (error) {
  93.             alert(error.message);
  94.         });
  95.         return false;
  96.     };
  97.  
  98.  
  99.  document.getElementById("imForm__sendMessage").addEventListener("click", function (event) {
  100.         event.preventDefault();
  101.         var message = messageClient.newMessage(document.getElementById("imForm__recepientField").value.trim(), document.getElementById("imForm__messageField").value.trim())
  102.         messageClient.send(message).fail(function (error) {
  103.             console.log(error);
  104.         });
  105.         return false;
  106.     });
  107.  
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement