Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 8be43e2

Browse files
author
vikasrohit
committed
AS#100297043256582, Move all login to link of Members to accounts.topcoder.com
-- Handling login state of topcoder-app to redirect user to correct path. For logged in users, load state specified by `next` param or go to dashboard and for non logged in user, it redirects user to the login page
1 parent a84ddb0 commit 8be43e2

File tree

3 files changed

+16
-171
lines changed

3 files changed

+16
-171
lines changed

app/account/account.routes.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,8 @@ import angular from 'angular'
3636
data: {
3737
title: 'Login'
3838
},
39-
views: {
40-
'header@': {
41-
template: require('../layout/header/account-header')()
42-
},
43-
'container@': {
44-
template: require('./login/login')(),
45-
controller: 'LoginController',
46-
controllerAs: 'vm'
47-
},
48-
'footer@': {
49-
controller: 'FooterController as vm',
50-
template: require('../layout/footer/account-footer')()
51-
}
52-
}
39+
template: '',
40+
controller: 'LoginController',
5341
},
5442
'register': {
5543
parent: 'auth',

app/account/login/login.controller.js

Lines changed: 14 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import angular from 'angular'
2+
import { getCurrentUser, loadUser } from '../../services/userv3.service.js'
23

34
(function() {
45
'use strict'
@@ -10,122 +11,25 @@ import angular from 'angular'
1011
function LoginController(logger, $state, $stateParams, $location, $scope, TcAuthService, UserService, Helpers, CONSTANTS) {
1112
var vm = this
1213
vm.$stateParams = $stateParams
13-
vm.passwordReset = false
14-
vm.loginErrors = {
15-
USERNAME_NONEXISTANT: false,
16-
WRONG_PASSWORD: false,
17-
SOCIAL_LOGIN_ERROR: false
18-
}
19-
20-
vm.login = login
21-
vm.socialLogin = socialLogin
22-
23-
// reference for main vm
24-
var mainVm = $scope.$parent.main
2514

2615
activate()
2716

28-
function activate() {}
29-
30-
function login() {
31-
vm.loginErrors.USERNAME_NONEXISTANT = false
32-
vm.loginErrors.WRONG_PASSWORD = false
33-
34-
// TODO ideally it should be done by dedicated directive to handle all outside clicks
35-
mainVm.menuVisible = false
36-
37-
if (Helpers.isEmail(vm.username)) {
38-
// the user is loggin in using email
39-
vm.emailOrUsername = 'email'
40-
41-
// ensure email exists
42-
// uses same validity check as registration
43-
// valid => email isn't already used by someone
44-
UserService.validateUserEmail(vm.username).then(function(data) {
45-
if (data.valid) {
46-
// email doesn't exist
47-
vm.loginErrors.USERNAME_NONEXISTANT = true
48-
} else {
49-
_doLogin(vm.username, vm.currentPassword)
50-
}
51-
}).catch(function(resp) {
52-
// TODO handle error
53-
// assume email exists, login would in any case if it didn't
54-
vm.loginErrors.USERNAME_NONEXISTANT = false
55-
_doLogin(vm.username, vm.currentPassword)
56-
})
57-
} else {
58-
// the user is logging in using a username
59-
vm.emailOrUsername = 'username'
60-
61-
// username - make sure it exists
62-
UserService.validateUserHandle(vm.username).then(function(data) {
63-
if (data.valid) {
64-
// username doesn't exist
65-
vm.loginErrors.USERNAME_NONEXISTANT = true
66-
} else {
67-
_doLogin(vm.username, vm.currentPassword)
68-
}
69-
}).catch(function(resp) {
70-
// TODO handle error
71-
// assume email exists, login would in any case if it didn't
72-
_doLogin(vm.username, vm.currentPassword)
73-
})
74-
}
75-
}
76-
77-
function _doLogin(usernameOrEmail, password) {
78-
return TcAuthService.login(usernameOrEmail, password)
79-
.then(function(data) {
80-
// setup login event for analytics tracking
81-
Helpers.setupLoginEventMetrics(usernameOrEmail)
17+
function activate() {
18+
var currentUser = getCurrentUser()
19+
if (!currentUser) {
20+
loadUser().then(function(token) {
21+
logger.debug('successful login with token ' + JSON.stringify(token))
22+
$rootScope.$broadcast(CONSTANTS.EVENT_USER_LOGGED_IN)
8223
return Helpers.redirectPostLogin($stateParams.next)
83-
84-
})
85-
.catch(function(err) {
86-
logger.warning(err)
87-
88-
switch (err.status) {
89-
case 'ACCOUNT_INACTIVE':
90-
$state.go('registeredSuccessfully')
91-
// user should already be redirected
92-
break
93-
case 'UNKNOWN_ERROR':
94-
default:
95-
vm.loginErrors.WRONG_PASSWORD = true
96-
vm.password = ''
97-
logger.error('Error logging in: ', err)
98-
}
24+
}, function() {
25+
logger.debug('State requires authentication, and user is not logged in, redirecting')
26+
// setup redirect for post login
27+
event.preventDefault()
28+
var next = $state.href(toState.name, toParams, {absolute: true})
29+
var retUrl = next
30+
$window.location = CONSTANTS.ACCOUNTS_APP_URL + '?retUrl=' + encodeURIComponent(retUrl)
9931
})
100-
}
101-
102-
function socialLogin(platform) {
103-
// we need to pass on the 'next' param if we have one
104-
var params = {}
105-
if ($stateParams.next) {
106-
params = {next: $stateParams.next}
10732
}
108-
109-
// redirect back to login
110-
var callbackUrl = $state.href('login', params, {absolute: true})
111-
112-
TcAuthService.socialLogin(platform, callbackUrl)
113-
.then(function() {
114-
logger.debug('logged in')
115-
return Helpers.redirectPostLogin($stateParams.next)
116-
})
117-
.catch(function(err) {
118-
/*eslint no-fallthrough:0*/
119-
switch (err.status) {
120-
case 'ACCOUNT_INACTIVE':
121-
window.location.href = 'https://www.' + CONSTANTS.domain + '/account-inactive/'
122-
case 'USER_NOT_REGISTERED':
123-
default:
124-
vm.socialLoginError = 401
125-
vm.loginErrors.SOCIAL_LOGIN_ERROR = true
126-
logger.error('Error logging in with social account', err)
127-
}
128-
})
12933
}
13034
}
13135

app/account/login/login.jade

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)