Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var user = {};
- var sinchClient = new SinchClient({
- applicationKey: "<hiddenkey>",
- capabilities: {
- messaging: true
- },
- startActiveConnection: true
- });
- var messages = [];
- var messageClient = sinchClient.getMessageClient();
- var sessionName = "sinch-" + sinchClient.applicationKey;
- location.reload = {};
- messageClient.addEventListener({
- onMessageDelivered: function (messageDeliveryInfo) {
- console.log("Delivered!", messageDeliveryInfo);
- },
- onIncomingMessage: function (message) {
- document.body.innerHTML += (message.textBody + " from " + message.senderId + "<br>");
- }
- });
- function updateUserInformation() {
- document.getElementById("currentUser__username").innerHTML = user.username;
- }
- var sessionObject = JSON.parse(window.localStorage[sessionName] || "{}");
- if (sessionObject.userId !== null || typeof sessionObject.userId !== undefined) {
- sinchClient.start(sessionObject).then(function () {
- user.username = sessionObject.userId;
- updateUserInformation();
- window.localStorage[sessionName] = JSON.stringify(sinchClient.getSession());
- }).fail(function (error) {
- alert(error.message);
- })
- }
- window.onload = function () {
- document.getElementById("local__registerForm").onsubmit = function () {
- firebase.auth().createUserWithEmailAndPassword((function () {
- //Email vetting
- return document.getElementById("registerForm__emailField").value.trim()
- })(), (function () {
- //Password vetting
- return document.getElementById("registerForm__passwordField").value.trim()
- })()).then(function () {
- firebase.auth().currentUser.updateProfile({
- displayName: (function () {
- //Username vetting
- return document.getElementById("registerForm__usernameField").value.trim()
- })()
- }).then(function () {
- sinchClient.newUser({
- username: document.getElementById("registerForm__usernameField").value.trim(),
- password: document.getElementById("registerForm__passwordField").value.trim()
- }, function (ticket) {
- user.username = document.getElementById("registerForm__usernameField").value.trim();
- updateUserInformation();
- sinchClient.start(ticket);
- window.localStorage[sessionName] = JSON.stringify(sinchClient.getSession());
- }).fail(function (error) {
- alert(error.message);
- })
- })
- })
- return false;
- }
- document.getElementById("local__loginForm").onsubmit = function () {
- firebase.auth().signInWithEmailAndPassword((function () {
- //Email vetting
- return document.getElementById("loginForm__emailField").value.trim()
- })(), (function () {
- //Password vetting
- return document.getElementById("loginForm__passwordField").value.trim()
- })()).then(function () {
- sinchClient.start({
- username: firebase.auth().currentUser.displayName,
- password: document.getElementById("loginForm__passwordField").value.trim()
- }).then(function () {
- user.username = firebase.auth().currentUser.displayName;
- updateUserInformation();
- window.localStorage[sessionName] = JSON.stringify(sinchClient.getSession());
- }).fail(function (error) {
- alert(error.message);
- })
- }).catch(function (error) {
- alert(error.message);
- });
- return false;
- };
- document.getElementById("imForm__sendMessage").addEventListener("click", function (event) {
- event.preventDefault();
- var message = messageClient.newMessage(document.getElementById("imForm__recepientField").value.trim(), document.getElementById("imForm__messageField").value.trim())
- messageClient.send(message).fail(function (error) {
- console.log(error);
- });
- return false;
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement