This repository was archived by the owner on Mar 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathregister.controller.js
279 lines (250 loc) · 9.08 KB
/
register.controller.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import angular from 'angular'
import _ from 'lodash'
import { BUSY_PROGRESS_MESSAGE, DOMAIN, WIPRO_SSO_PROVIDER, V3_JWT, V2_JWT, V2_SSO, AUTH0_REFRESH, AUTH0_JWT, ZENDESK_JWT } from '../../../core/constants.js'
import { registerUser, socialRegistration, identifySSOProvider } from '../../../core/auth.js'
import { npad } from '../../../core/utils.js'
import { generateReturnUrl, redirectTo } from '../../../core/url.js'
import { getToken, decodeToken, setToken } from '../../../core/token.js'
import { getNewJWT } from '../../../core/auth.js'
(function() {
'use strict'
const SKILL_PICKER_URL = 'https://www.' + DOMAIN + '/settings/profile'
angular.module('accounts').controller('TCRegistrationController', TCRegistrationController)
TCRegistrationController.$inject = ['$log', '$scope', '$state', '$stateParams', 'UserService', 'ISO3166', '$timeout']
function TCRegistrationController($log, $scope, $state, $stateParams, UserService, ISO3166, $timeout) {
var vm = this
vm.registering = false
// auth0 login data, passed from another states as state param
vm.auth0Data = $stateParams.auth0Data
// SSO user data extracted from auth0 login data
vm.ssoUser = vm.auth0Data && vm.auth0Data.ssoUserData ? vm.auth0Data.ssoUserData : null
// regForm is used to pre-populate form items
vm.regForm = $stateParams.regForm
if(vm.regForm) {
vm.username = vm.regForm.handle
vm.firstname = vm.regForm.firstName
vm.lastname = vm.regForm.lastName
vm.countryObj = ISO3166.getCountryObjFromCountryCode(vm.regForm.country)
}
// prepares utm params, if available
var utm = {
source : $stateParams && $stateParams.utm_source ? $stateParams.utm_source : '',
medium : $stateParams && $stateParams.utm_medium ? $stateParams.utm_medium : '',
campaign : $stateParams && $stateParams.utm_campaign ? $stateParams.utm_campaign : ''
}
// Set default for toggle password directive
vm.defaultPlaceholder = 'Create Password'
vm.busyMessage = BUSY_PROGRESS_MESSAGE
vm.retUrl = $stateParams && $stateParams.retUrl ? $stateParams.retUrl : SKILL_PICKER_URL
vm.countries = ISO3166.getAllCountryObjects()
vm.$stateParams = $stateParams
// watch form to detect particular changes in it.
// https://stackoverflow.com/questions/22436501/simple-angularjs-form-is-undefined-in-scope
$scope.$watch('registerForm', function(registerForm) {
if (vm.ssoUser) {
loadSSOUser(vm.ssoUser)
}
})
$scope.$watch('vm.email', function(email) {
vm.ssoForced = !!(identifySSOProvider(email))
})
$scope.usernameFocusLoss = function () {
$timeout(function () {
vm.usernameTips = false
}, 100);
}
$scope.emailFocusLoss = function () {
$timeout(function () {
vm.emailTips = false
}, 100);
}
vm.updateCountry = function (angucompleteCountryObj) {
var countryCode = _.get(angucompleteCountryObj, 'originalObject.code', undefined)
var isValidCountry = _.isUndefined(countryCode) ? false : true
vm.registerForm.country.$setValidity('required', isValidCountry)
vm.isValidCountry = isValidCountry
if (isValidCountry) {
vm.country = angucompleteCountryObj.originalObject
}
}
function setV3Tokens({token, zendeskJwt}) {
$log.debug('Received v3 tokens')
setToken(V3_JWT, token || '')
setToken(ZENDESK_JWT, zendeskJwt || '')
$log.debug('Redirecting to ' + vm.retUrl)
var error = redirectTo(generateReturnUrl(vm.retUrl))
$scope.$apply(function() {
vm.registering = false
if (error) {
vm.error = 'Invalid URL is assigned to the return-URL.'
}
})
}
function startSSO() {
$state.go ('SSO_LOGIN',
{
app: 'member',
email : vm.email,
regForm : {
handle : vm.username,
firstName: vm.firstname,
lastName : vm.lastname,
country : vm.country.code
},
retUrl : vm.retUrl
}
)
}
vm.register = function() {
if (!vm.ssoUser) {
const provider = identifySSOProvider(vm.email)
if (provider) {
startSSO()
return
}
}
vm.registering = true
var userInfo = {
handle: vm.username,
firstName: vm.firstname,
lastName: vm.lastname,
email: vm.email,
country: {
code: npad(vm.country.code, 3),
isoAlpha3Code: vm.country.alpha3,
isoAlpha2Code: vm.country.alpha2
},
utmSource: utm.source,
utmMedium: utm.medium,
utmCampaign: utm.campaign
}
if (!vm.isSocialRegistration && !vm.ssoUser) {// if not social or sso registration
userInfo.credential = { password: vm.password }
} else if (vm.ssoUser) {//SSO user
userInfo.active = true, // activate in registration
userInfo.profile = {
name: vm.ssoUser.name,
email: vm.ssoUser.email,
providerType: 'samlp',
provider: vm.ssoUser.ssoProvider,
userId: vm.ssoUser.ssoUserId
}
} else {
userInfo.profile = {
userId: vm.socialUserId,
name: vm.firstname + ' ' + vm.lastname,
email: vm.socialProfile.email,
emailVerified: vm.socialProfile.email_verified,
providerType: vm.socialProvider,
context: {
handle: vm.username,
accessToken: vm.socialContext.accessToken,
auth0UserId: vm.socialProfile.user_id
}
}
}
var redirectURL = vm.retUrl ? vm.retUrl : SKILL_PICKER_URL;
var body = {
param: userInfo,
options: {
afterActivationURL: redirectURL
}
}
registerUser(body)
.then(function(data) {
vm.registering = false
$log.debug('Registered successfully')
if (vm.ssoUser && vm.auth0Data) {
// LOG IN user
setToken(AUTH0_JWT, vm.auth0Data.idToken)
setToken(AUTH0_REFRESH, vm.auth0Data.refreshToken)
$log.debug('Getting v3jwt')
getNewJWT()
.then(setV3Tokens)
.catch(function(err) {
vm.registering = false
vm.errMsg = err && err.message ? err.message : 'Error in logging in new user'
$scope.$apply()
$log.error('Error in logging in new user', err)
})
} else {
// In the future, go to dashboard
$state.go('MEMBER_REGISTRATION_SUCCESS', {
ssoUser : !!vm.ssoUser,
retUrl : redirectURL
})
}
})
.catch(function(err) {
vm.registering = false
vm.errMsg = err && err.message ? err.message : 'Error in registering new user'
$scope.$apply()
$log.error('Error in registering new user', err)
})
}
vm.socialRegister = function(provider) {
vm.errMsg = null
socialRegistration(provider, null)
.then(function(resp) {
if (resp.status === 'SUCCESS') {
var socialData = resp.data
vm.socialUserId = socialData.socialUserId
vm.username = socialData.username
if (socialData.username) {
vm.registerForm.username.$setDirty()
}
vm.firstname = socialData.firstname
if (socialData.firstname) {
vm.registerForm.firstname.$setDirty()
}
vm.lastname = socialData.lastname
if (socialData.lastname) {
vm.registerForm.lastname.$setDirty()
}
if (socialData.email) {
vm.registerForm.email.$setDirty()
}
vm.email = socialData.email
vm.socialProfile = socialData.socialProfile
vm.socialProvider = socialData.socialProvider
vm.socialContext= {'accessToken': socialData.accessToken}
vm.isSocialRegistration = true
} else {
vm.isSocialRegistration = false
}
$scope.$apply()
})
.catch(function(err) {
switch (err.status) {
case 'SOCIAL_PROFILE_ALREADY_EXISTS':
vm.errMsg = 'An account with that profile already exists. Please login to access your account.'
$log.error('Error registering user with social account', err)
break
default:
vm.errMsg = 'Whoops! Something went wrong. Please try again later.'
$log.error('Error registering user with social account', err)
}
vm.isSocialRegistration = false
$scope.$apply()
})
}
function loadSSOUser(ssoUser) {
vm.ssoUser = ssoUser
if (ssoUser && ssoUser.firstName) {
vm.firstname = ssoUser.firstName
vm.registerForm.firstname.$setDirty()
}
if (ssoUser && ssoUser.lastName) {
vm.lastname = ssoUser.lastName
vm.registerForm.lastname.$setDirty()
}
if (ssoUser && ssoUser.email) {
vm.email = ssoUser.email
vm.registerForm.email.$setDirty()
}
}
vm.showRegistrationPage = function(){
$state.go('MEMBER_REGISTRATION', $stateParams)
}
}
})()