-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticationService.js
99 lines (81 loc) · 3.01 KB
/
authenticationService.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
(function (angular) {
'use strict';
//// JavaScript Code ////
function AuthServiceFactory($http, $q, localStorageService) {
var apiEndPoint = 'http://ngauthenticationapi.azurewebsites.net/';
var loginUri = '/login';
var logoutUri = '/logout';
var registerUri = '/users';
var _authentication = {
isAuth: false,
userName : ""
};
function _setApiEndpoint(endpoint) {
apiEndPoint = endpoint;
}
function _setLoginUri(newloginUri) {
loginUri = newloginUri;
}
function _setLogoutUri(newlogoutUri) {
logoutUri = newlogoutUri;
}
function _getLoginUri() {
return loginUri;
}
function _setRegisterUri(newregisterUri) {
registerUri = newregisterUri;
}
function _saveRegistration (registration) {
_logOut();
return $http.post(apiEndPoint + 'api/account/register', registration).then(function (response) {
return response;
});
}
function _login(loginData) {
var data = "grant_type=password&username=" + loginData.userName + "&password=" + loginData.password;
var deferred = $q.defer();
$http.post(apiEndPoint + 'token', data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).success(function (response) {
localStorageService.set('authorizationData', { token: response.access_token, userName: loginData.userName });
_authentication.isAuth = true;
_authentication.userName = loginData.userName;
deferred.resolve(response);
}).error(function (err) {
_logOut();
deferred.reject(err);
});
return deferred.promise;
}
function _logOut() {
localStorageService.remove('authorizationData');
_authentication.isAuth = false;
_authentication.userName = "";
}
function _fillAuthData() {
var authData = localStorageService.get('authorizationData');
if (authData)
{
_authentication.isAuth = true;
_authentication.userName = authData.userName;
}
}
var authServiceFactory = {
saveRegistration : _saveRegistration,
login : _login,
logOut : _logOut,
fillAuthData : _fillAuthData,
authentication : _authentication,
getLoginUri : _getLoginUri
};
return {
setApiEndpoint : _setApiEndpoint,
setLoginUri : _setLoginUri,
setLogoutUri : _setLogoutUri,
setRegisterUri : _setRegisterUri,
$get : function() {
return authServiceFactory;
}
};
}
//// Angular Code ////
angular.module('LoopbackAuthService').provider('authService',AuthServiceFactory);
})(angular);