mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-05-02 20:54:50 -04:00
Move the unknown token broadcast to the interceptor. Return the $http promise and not a wrapped one via $q. Everything now needs a level deeper nesting. Fixed registration and login.
This commit is contained in:
parent
76005c44f7
commit
db3e1d73c6
4 changed files with 16 additions and 30 deletions
|
@ -42,20 +42,20 @@ matrixWebClient.config(['$routeProvider', '$provide', '$httpProvider',
|
||||||
redirectTo: '/rooms'
|
redirectTo: '/rooms'
|
||||||
});
|
});
|
||||||
|
|
||||||
$provide.factory('AccessTokenInterceptor', function ($q) {
|
$provide.factory('AccessTokenInterceptor', ['$q', '$rootScope',
|
||||||
|
function ($q, $rootScope) {
|
||||||
return {
|
return {
|
||||||
responseError: function(rejection) {
|
responseError: function(rejection) {
|
||||||
console.log("Rejection: " + JSON.stringify(rejection));
|
|
||||||
if (rejection.status === 403 && "data" in rejection &&
|
if (rejection.status === 403 && "data" in rejection &&
|
||||||
"errcode" in rejection.data &&
|
"errcode" in rejection.data &&
|
||||||
rejection.data.errcode === "M_UNKNOWN_TOKEN") {
|
rejection.data.errcode === "M_UNKNOWN_TOKEN") {
|
||||||
console.log("TODO: Got a 403 with an unknown token. Logging out.")
|
console.log("Got a 403 with an unknown token. Logging out.")
|
||||||
// TODO logout
|
$rootScope.$broadcast("M_UNKNOWN_TOKEN");
|
||||||
}
|
}
|
||||||
return $q.reject(rejection);
|
return $q.reject(rejection);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
}]);
|
||||||
$httpProvider.interceptors.push('AccessTokenInterceptor');
|
$httpProvider.interceptors.push('AccessTokenInterceptor');
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
|
|
|
@ -49,32 +49,13 @@ angular.module('matrixService', [])
|
||||||
if (path.indexOf(prefixPath) !== 0) {
|
if (path.indexOf(prefixPath) !== 0) {
|
||||||
path = prefixPath + path;
|
path = prefixPath + path;
|
||||||
}
|
}
|
||||||
// Do not directly return the $http instance but return a promise
|
return $http({
|
||||||
// with enriched or cleaned information
|
|
||||||
var deferred = $q.defer();
|
|
||||||
$http({
|
|
||||||
method: method,
|
method: method,
|
||||||
url: baseUrl + path,
|
url: baseUrl + path,
|
||||||
params: params,
|
params: params,
|
||||||
data: data,
|
data: data,
|
||||||
headers: headers
|
headers: headers
|
||||||
})
|
})
|
||||||
.success(function(data, status, headers, config) {
|
|
||||||
deferred.resolve(data, status, headers, config);
|
|
||||||
})
|
|
||||||
.error(function(data, status, headers, config) {
|
|
||||||
// Enrich the error callback with an human readable error reason
|
|
||||||
var reason = data.error;
|
|
||||||
if (!data.error) {
|
|
||||||
reason = JSON.stringify(data);
|
|
||||||
}
|
|
||||||
deferred.reject(reason, data, status, headers, config);
|
|
||||||
|
|
||||||
if (403 === status && "M_UNKNOWN_TOKEN" === data.errcode) {
|
|
||||||
// The access token is no more valid, broadcast the issue
|
|
||||||
$rootScope.$broadcast("M_UNKNOWN_TOKEN");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return deferred.promise;
|
return deferred.promise;
|
||||||
};
|
};
|
||||||
|
|
|
@ -39,14 +39,13 @@ angular.module('LoginController', ['matrixService'])
|
||||||
}
|
}
|
||||||
|
|
||||||
matrixService.register($scope.account.desired_user_name, $scope.account.pwd1).then(
|
matrixService.register($scope.account.desired_user_name, $scope.account.pwd1).then(
|
||||||
function(data) {
|
function(response) {
|
||||||
$scope.feedback = "Success";
|
$scope.feedback = "Success";
|
||||||
|
|
||||||
// Update the current config
|
// Update the current config
|
||||||
var config = matrixService.config();
|
var config = matrixService.config();
|
||||||
angular.extend(config, {
|
angular.extend(config, {
|
||||||
access_token: data.access_token,
|
access_token: response.data.access_token,
|
||||||
user_id: data.user_id
|
user_id: response.data.user_id
|
||||||
});
|
});
|
||||||
matrixService.setConfig(config);
|
matrixService.setConfig(config);
|
||||||
|
|
||||||
|
@ -74,7 +73,7 @@ angular.module('LoginController', ['matrixService'])
|
||||||
matrixService.setConfig({
|
matrixService.setConfig({
|
||||||
homeserver: $scope.account.homeserver,
|
homeserver: $scope.account.homeserver,
|
||||||
user_id: $scope.account.user_id,
|
user_id: $scope.account.user_id,
|
||||||
access_token: response.access_token
|
access_token: response.data.access_token
|
||||||
});
|
});
|
||||||
matrixService.saveConfig();
|
matrixService.saveConfig();
|
||||||
$location.path("rooms");
|
$location.path("rooms");
|
||||||
|
@ -82,6 +81,11 @@ angular.module('LoginController', ['matrixService'])
|
||||||
else {
|
else {
|
||||||
$scope.feedback = "Failed to login: " + JSON.stringify(response);
|
$scope.feedback = "Failed to login: " + JSON.stringify(response);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
if (error.data.errcode === "M_FORBIDDEN") {
|
||||||
|
$scope.login_error_msg = "Incorrect username or password.";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
<h3>Got an account?</h3>
|
<h3>Got an account?</h3>
|
||||||
<form novalidate>
|
<form novalidate>
|
||||||
<!-- Login with an registered user -->
|
<!-- Login with an registered user -->
|
||||||
|
<div>{{ login_error_msg }} </div>
|
||||||
<div>
|
<div>
|
||||||
<input id="user_id" size="70" type="text" auto-focus ng-model="account.user_id" placeholder="User ID (ex:@bob:localhost)"/>
|
<input id="user_id" size="70" type="text" auto-focus ng-model="account.user_id" placeholder="User ID (ex:@bob:localhost)"/>
|
||||||
<br />
|
<br />
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue