-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-unisson-auth.js
157 lines (145 loc) · 5.95 KB
/
angular-unisson-auth.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Generated by CoffeeScript 1.7.1
(function() {
var LoginService, module,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
module = angular.module('angular-unisson-auth', ['http-auth-interceptor', 'ngCookies', 'googleOauth', 'restangular']);
module.factory('Groups', function(Restangular) {
return Restangular.service('account/group');
});
module.factory('Users', function(Restangular) {
return Restangular.service('account/user');
});
LoginService = (function() {
"Login a user";
function LoginService($rootScope, baseUrl, $http, $state, Restangular, $cookies, authService, Token) {
this.$rootScope = $rootScope;
this.baseUrl = baseUrl;
this.$http = $http;
this.$state = $state;
this.Restangular = Restangular;
this.$cookies = $cookies;
this.authService = authService;
this.Token = Token;
this.authenticateGoogle = __bind(this.authenticateGoogle, this);
this.submit = __bind(this.submit, this);
this.logout = __bind(this.logout, this);
this.forceLogin = __bind(this.forceLogin, this);
this.$rootScope.authVars = {
username: "",
isAuthenticated: false,
loginrequired: false
};
this.loginRestangular = Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.setBaseUrl(baseUrl);
return RestangularConfigurer.setDefaultHttpFields({
'ignoreAuthModule': true
});
});
this.$rootScope.$on('event:auth-loginRequired', (function(_this) {
return function() {
_this.$rootScope.authVars.loginrequired = true;
return console.debug("Login required");
};
})(this));
this.$rootScope.$on('event:auth-loginConfirmed', (function(_this) {
return function() {
console.debug("Login OK");
_this.$rootScope.authVars.loginrequired = false;
_this.$rootScope.authVars.username = _this.$cookies.username;
_this.$rootScope.authVars.isAuthenticated = true;
return _this.loginRestangular.all('account/user').get(_this.$cookies.username).then(function(data) {
console.log("user object", data);
return _this.$rootScope.authVars.user = data;
});
};
})(this));
if (this.$cookies.username && this.$cookies.key) {
console.debug("Already logged in.");
this.$http.defaults.headers.common['Authorization'] = "ApiKey " + this.$cookies.username + ":" + this.$cookies.key;
this.authService.loginConfirmed();
}
this.$rootScope.accessToken = this.Token.get();
this.$rootScope.submit = this.submit;
this.$rootScope.authenticateGoogle = this.authenticateGoogle;
this.$rootScope.forceLogin = this.forceLogin;
this.$rootScope.logout = this.logout;
}
LoginService.prototype.forceLogin = function() {
console.debug("forcing login on request");
return this.$rootScope.authVars.loginrequired = true;
};
LoginService.prototype.logout = function() {
this.$rootScope.authVars.isAuthenticated = false;
delete this.$http.defaults.headers.common['Authorization'];
delete this.$cookies['username'];
delete this.$cookies['key'];
this.$rootScope.authVars.username = "";
if (this.$rootScope.homeStateName) {
return this.$state.go(this.$rootScope.homeStateName, {}, {
reload: true
});
}
};
LoginService.prototype.submit = function() {
console.debug('submitting login...');
return this.loginRestangular.all('account/user').customPOST({
username: this.$rootScope.authVars.username,
password: this.$rootScope.authVars.password
}, "login", {}).then((function(_this) {
return function(data) {
console.log(data);
_this.$cookies.username = data.username;
_this.$cookies.key = data.key;
_this.$http.defaults.headers.common['Authorization'] = "ApiKey " + data.username + ":" + data.key;
return _this.loginRestangular.all('account/user').get(data.username).then(function(data) {
console.log("user object", data);
_this.$rootScope.authVars.user = data;
return _this.authService.loginConfirmed();
});
};
})(this), (function(_this) {
return function(data) {
console.debug("LoginController submit error: " + data.reason);
return _this.$rootScope.errorMsg = data.reason;
};
})(this));
};
LoginService.prototype.authenticateGoogle = function() {
var extraParams;
extraParams = {};
if (this.$rootScope.askApproval) {
extraParams = {
approval_prompt: 'force'
};
}
return this.Token.getTokenByPopup(extraParams).then((function(_this) {
return function(params) {
return _this.loginRestangular.all('account/user/login').customPOST({
access_token: params.access_token
}, "google", {}).then(function(data) {
_this.$cookies.username = data.username;
_this.$cookies.key = data.key;
_this.$http.defaults.headers.common['Authorization'] = "ApiKey " + data.username + ":" + data.key;
return _this.authService.loginConfirmed();
}, function(data) {
console.debug("LoginController submit error: " + data.reason);
return _this.$rootScope.errorMsg = data.reason;
});
};
})(this), function() {
return alert("Failed to get token from popup.");
});
};
return LoginService;
})();
module.provider("loginService", function() {
return {
setBaseUrl: function(baseUrl) {
return this.baseUrl = baseUrl;
},
$get: function($rootScope, $http, $state, Restangular, $cookies, authService, Token) {
return new LoginService($rootScope, this.baseUrl, $http, $state, Restangular, $cookies, authService, Token);
}
};
});
}).call(this);