2014-08-12 10:10:52 -04:00
|
|
|
angular.module('LoginController', ['matrixService'])
|
2014-08-15 08:43:07 -04:00
|
|
|
.controller('LoginController', ['$scope', '$location', 'matrixService', 'eventStreamService',
|
|
|
|
function($scope, $location, matrixService, eventStreamService) {
|
2014-08-12 10:10:52 -04:00
|
|
|
'use strict';
|
|
|
|
|
2014-08-14 08:57:55 -04:00
|
|
|
|
|
|
|
// 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 10:10:52 -04:00
|
|
|
$scope.account = {
|
2014-08-14 08:57:55 -04:00
|
|
|
homeserver: hs_url,
|
2014-08-12 10:10:52 -04:00
|
|
|
desired_user_name: "",
|
|
|
|
user_id: "",
|
|
|
|
password: "",
|
2014-08-15 20:48:44 -04:00
|
|
|
identityServer: "",
|
2014-08-12 10:10:52 -04:00
|
|
|
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(
|
2014-08-14 10:36:40 -04:00
|
|
|
function(response) {
|
2014-08-12 10:10:52 -04:00
|
|
|
$scope.feedback = "Success";
|
|
|
|
// Update the current config
|
|
|
|
var config = matrixService.config();
|
|
|
|
angular.extend(config, {
|
2014-08-14 10:36:40 -04:00
|
|
|
access_token: response.data.access_token,
|
|
|
|
user_id: response.data.user_id
|
2014-08-12 10:10:52 -04:00
|
|
|
});
|
|
|
|
matrixService.setConfig(config);
|
|
|
|
|
|
|
|
// And permanently save it
|
|
|
|
matrixService.saveConfig();
|
2014-08-15 08:43:07 -04:00
|
|
|
eventStreamService.resume();
|
2014-08-12 10:10:52 -04:00
|
|
|
// Go to the user's rooms list page
|
2014-08-22 12:08:03 -04:00
|
|
|
$location.url("home");
|
2014-08-12 10:10:52 -04:00
|
|
|
},
|
2014-08-14 11:03:04 -04:00
|
|
|
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 10:10:52 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.login = function() {
|
|
|
|
matrixService.setConfig({
|
|
|
|
homeserver: $scope.account.homeserver,
|
2014-08-22 05:34:27 -04:00
|
|
|
identityServer: $scope.account.identityServer,
|
2014-08-12 10:10:52 -04:00
|
|
|
user_id: $scope.account.user_id
|
|
|
|
});
|
|
|
|
// try to login
|
|
|
|
matrixService.login($scope.account.user_id, $scope.account.password).then(
|
|
|
|
function(response) {
|
2014-08-14 10:43:16 -04:00
|
|
|
if ("access_token" in response.data) {
|
2014-08-12 10:10:52 -04:00
|
|
|
$scope.feedback = "Login successful.";
|
|
|
|
matrixService.setConfig({
|
|
|
|
homeserver: $scope.account.homeserver,
|
2014-08-22 05:34:27 -04:00
|
|
|
identityServer: $scope.account.identityServer,
|
2014-08-14 11:40:15 -04:00
|
|
|
user_id: response.data.user_id,
|
2014-08-14 10:36:40 -04:00
|
|
|
access_token: response.data.access_token
|
2014-08-12 10:10:52 -04:00
|
|
|
});
|
|
|
|
matrixService.saveConfig();
|
2014-08-15 08:43:07 -04:00
|
|
|
eventStreamService.resume();
|
2014-08-22 12:08:03 -04:00
|
|
|
$location.url("home");
|
2014-08-12 10:10:52 -04:00
|
|
|
}
|
|
|
|
else {
|
2014-08-14 10:43:16 -04:00
|
|
|
$scope.feedback = "Failed to login: " + JSON.stringify(response.data);
|
2014-08-12 10:10:52 -04:00
|
|
|
}
|
2014-08-14 10:36:40 -04:00
|
|
|
},
|
|
|
|
function(error) {
|
2014-08-14 11:03:04 -04:00
|
|
|
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-14 10:36:40 -04:00
|
|
|
}
|
2014-08-12 10:10:52 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}]);
|
|
|
|
|