-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-unisson-auth.coffee
125 lines (103 loc) · 5.66 KB
/
angular-unisson-auth.coffee
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
module = angular.module('angular-unisson-auth', ['http-auth-interceptor', 'ngCookies', 'googleOauth', 'restangular'])
# Rest factories
module.factory('Groups', (Restangular) ->
return Restangular.service('account/group')
)
module.factory('Users', (Restangular) ->
return Restangular.service('account/user')
)
class LoginService
"""
Login a user
"""
constructor: (@$rootScope, @baseUrl, @$http, @$state, @Restangular, @$cookies, @authService, @Token) ->
@$rootScope.authVars =
username : "",
isAuthenticated: false,
loginrequired : false
# Custom restangular for this login URL
@loginRestangular = Restangular.withConfig((RestangularConfigurer) ->
RestangularConfigurer.setBaseUrl(baseUrl)
RestangularConfigurer.setDefaultHttpFields({'ignoreAuthModule':true})
)
# On login required
@$rootScope.$on('event:auth-loginRequired', =>
@$rootScope.authVars.loginrequired = true
console.debug("Login required")
)
# On login successful
@$rootScope.$on('event:auth-loginConfirmed', =>
console.debug("Login OK")
@$rootScope.authVars.loginrequired = false
@$rootScope.authVars.username = @$cookies.username
@$rootScope.authVars.isAuthenticated = true
@loginRestangular.all('account/user').get(@$cookies.username).then((data)=>
console.log("user object", data)
@$rootScope.authVars.user = data
)
)
# set authorization header if already logged in
if @$cookies.username and @$cookies.key
console.debug("Already logged in.")
@$http.defaults.headers.common['Authorization'] = "ApiKey #{@$cookies.username}:#{@$cookies.key}"
@authService.loginConfirmed()
@$rootScope.accessToken = @Token.get()
# Add methods to scope
@$rootScope.submit = this.submit
@$rootScope.authenticateGoogle = this.authenticateGoogle
@$rootScope.forceLogin = this.forceLogin
@$rootScope.logout = this.logout
forceLogin: =>
console.debug("forcing login on request")
@$rootScope.authVars.loginrequired = true
logout: =>
@$rootScope.authVars.isAuthenticated = false
delete @$http.defaults.headers.common['Authorization']
delete @$cookies['username']
delete @$cookies['key']
@$rootScope.authVars.username = ""
if @$rootScope.homeStateName
@$state.go(@$rootScope.homeStateName, {}, {reload:true})
submit: =>
console.debug('submitting login...')
@loginRestangular.all('account/user').customPOST(
{username: @$rootScope.authVars.username, password: @$rootScope.authVars.password},"login", {}
).then((data) =>
console.log(data)
@$cookies.username = data.username
@$cookies.key = data.key
@$http.defaults.headers.common['Authorization'] = "ApiKey #{data.username}:#{data.key}"
@loginRestangular.all('account/user').get(data.username).then((data)=>
console.log("user object", data)
@$rootScope.authVars.user = data
@authService.loginConfirmed()
)
, (data) =>
console.debug("LoginController submit error: #{data.reason}")
@$rootScope.errorMsg = data.reason
)
authenticateGoogle: =>
extraParams = {}
if @$rootScope.askApproval
extraParams = {approval_prompt: 'force'}
@Token.getTokenByPopup(extraParams).then((params) =>
# Verify the token before setting it, to avoid the confused deputy problem.
@loginRestangular.all('account/user/login').customPOST({access_token: params.access_token}, "google", {}).then((data) =>
@$cookies.username = data.username
@$cookies.key = data.key
@$http.defaults.headers.common['Authorization'] = "ApiKey #{data.username}:#{data.key}"
@authService.loginConfirmed()
, (data) =>
console.debug("LoginController submit error: #{data.reason}")
@$rootScope.errorMsg = data.reason
)
, ->
# Failure getting token from popup.
alert("Failed to get token from popup.")
)
module.provider("loginService", ->
setBaseUrl: (baseUrl) ->
@baseUrl = baseUrl
$get: ($rootScope, $http, $state, Restangular, $cookies, authService, Token) ->
return new LoginService($rootScope, @baseUrl, $http, $state, Restangular, $cookies, authService, Token)
)