forked-synapse/webclient/login/login-controller.js

109 lines
3.8 KiB
JavaScript
Raw Normal View History

2014-08-12 10:10:52 -04:00
angular.module('LoginController', ['matrixService'])
.controller('LoginController', ['$scope', '$location', 'matrixService', 'eventStreamService',
function($scope, $location, matrixService, eventStreamService) {
2014-08-12 10:10:52 -04: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 10:10:52 -04:00
$scope.account = {
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(
function(response) {
2014-08-12 10:10:52 -04: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 10:10:52 -04:00
});
matrixService.setConfig(config);
// And permanently save it
matrixService.saveConfig();
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
},
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) {
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,
user_id: response.data.user_id,
access_token: response.data.access_token
2014-08-12 10:10:52 -04:00
});
matrixService.saveConfig();
eventStreamService.resume();
2014-08-22 12:08:03 -04:00
$location.url("home");
2014-08-12 10:10:52 -04:00
}
else {
$scope.feedback = "Failed to login: " + JSON.stringify(response.data);
2014-08-12 10:10:52 -04: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 10:10:52 -04:00
}
);
};
}]);