summary refs log tree commit diff
path: root/jsfiddles/register_login/demo.js
blob: 270a96786c5785135e73c07b29ba635593219d8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var accountInfo = {};

var showLoggedIn = function(data) {
    accountInfo = data;
    $(".loggedin").css({visibility: "visible"});
    $("#welcomeText").text("Welcome " + accountInfo.user_id+". Your access token is:  " +
                           accountInfo.access_token);    
};

$('.register').live('click', function() {
    var user = $("#user").val();
    var password = $("#password").val();
    $.ajax({
        url: "http://localhost:8080/matrix/client/api/v1/register",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ user_id: user, password: password }),
        dataType: "json",
        success: function(data) {
            showLoggedIn(data);
        },
        error: function(err) {
            alert(JSON.stringify($.parseJSON(err.responseText)));  
        }
    });
});

var login = function(user, password) {
    $.ajax({
        url: "http://localhost:8080/matrix/client/api/v1/login",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ user: user, password: password, type: "m.login.password" }),
        dataType: "json",
        success: function(data) {
            showLoggedIn(data);
        },
        error: function(err) {
            alert(JSON.stringify($.parseJSON(err.responseText)));  
        }
    });  
};

$('.login').live('click', function() {
    var user = $("#userLogin").val();
    var password = $("#passwordLogin").val();
    $.getJSON("http://localhost:8080/matrix/client/api/v1/login", function(data) {
        if (data.flows[0].type !== "m.login.password") {
            alert("I don't know how to login with this type: " + data.type);
            return;
        }
        login(user, password);
    });
});

$('.logout').live('click', function() {
    accountInfo = {};
    $("#imSyncText").text("");
    $(".loggedin").css({visibility: "hidden"});
});

$('.testToken').live('click', function() {
    var url = "http://localhost:8080/matrix/client/api/v1/initialSync?access_token=" + accountInfo.access_token + "&limit=1";
    $.getJSON(url, function(data) {
         $("#imSyncText").text(JSON.stringify(data, undefined, 2));
    }).fail(function(err) {
        $("#imSyncText").text(JSON.stringify($.parseJSON(err.responseText)));
    });
});