MatrixSynapse/webclient/login/login-controller.js

107 lines
3.7 KiB
JavaScript
Raw Normal View History

2014-08-12 16:10:52 +02:00
angular.module('LoginController', ['matrixService'])
.controller('LoginController', ['$scope', '$location', 'matrixService', 'eventStreamService',
function($scope, $location, matrixService, eventStreamService) {
2014-08-12 16:10:52 +02:00
'use strict';
// Assume that this is hosted on the home server, in which case the URL
// contains the home server.
var hs_url = $location.protocol() + "://" + $location.host();
if ($location.port()) {
hs_url += ":" + $location.port();
}
2014-08-12 16:10:52 +02:00
$scope.account = {
homeserver: hs_url,
2014-08-12 16:10:52 +02:00
desired_user_name: "",
user_id: "",
password: "",
identityServer: "http://localhost:8090",
pwd1: "",
pwd2: ""
};
$scope.register = function() {
// Set the urls
matrixService.setConfig({
homeserver: $scope.account.homeserver,
identityServer: $scope.account.identityServer
});
if ($scope.account.pwd1 !== $scope.account.pwd2) {
$scope.feedback = "Passwords don't match.";
return;
}
else if ($scope.account.pwd1.length < 6) {
$scope.feedback = "Password must be at least 6 characters.";
return;
}
matrixService.register($scope.account.desired_user_name, $scope.account.pwd1).then(
function(response) {
2014-08-12 16:10:52 +02:00
$scope.feedback = "Success";
// Update the current config
var config = matrixService.config();
angular.extend(config, {
access_token: response.data.access_token,
user_id: response.data.user_id
2014-08-12 16:10:52 +02:00
});
matrixService.setConfig(config);
// And permanently save it
matrixService.saveConfig();
eventStreamService.resume();
2014-08-12 16:10:52 +02:00
// Go to the user's rooms list page
$location.path("rooms");
},
function(error) {
if (error.data) {
if (error.data.errcode === "M_USER_IN_USE") {
$scope.feedback = "Username already taken.";
}
}
else if (error.status === 0) {
$scope.feedback = "Unable to talk to the server.";
}
2014-08-12 16:10:52 +02:00
});
};
$scope.login = function() {
matrixService.setConfig({
homeserver: $scope.account.homeserver,
user_id: $scope.account.user_id
});
// try to login
matrixService.login($scope.account.user_id, $scope.account.password).then(
function(response) {
if ("access_token" in response.data) {
2014-08-12 16:10:52 +02:00
$scope.feedback = "Login successful.";
matrixService.setConfig({
homeserver: $scope.account.homeserver,
user_id: response.data.user_id,
access_token: response.data.access_token
2014-08-12 16:10:52 +02:00
});
matrixService.saveConfig();
eventStreamService.resume();
2014-08-12 16:10:52 +02:00
$location.path("rooms");
}
else {
$scope.feedback = "Failed to login: " + JSON.stringify(response.data);
2014-08-12 16:10:52 +02:00
}
},
function(error) {
if (error.data) {
if (error.data.errcode === "M_FORBIDDEN") {
$scope.login_error_msg = "Incorrect username or password.";
}
}
else if (error.status === 0) {
$scope.login_error_msg = "Unable to talk to the server.";
}
2014-08-12 16:10:52 +02:00
}
);
};
}]);