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

Commit 939a2bc

Browse files
author
vikasrohit
committed
Merge pull request #819 from appirio-tech/feature/sso-accounts-app-integration
integration with accounts app for login/reg flows
2 parents d82b12c + 9fff590 commit 939a2bc

23 files changed

+239
-520
lines changed

app/account/account.routes.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ import angular from 'angular'
1515
data: {
1616
authRequired: false
1717
},
18-
onEnter: ['$state', '$stateParams', 'TcAuthService', 'logger', function($state, $stateParams, TcAuthService, logger) {
19-
if (TcAuthService.isAuthenticated()) {
20-
// redirect to next if exists else dashboard
21-
if ($stateParams.next) {
22-
logger.debug('Redirecting: ' + $stateParams.next)
23-
window.location.href = decodeURIComponent($stateParams.next)
24-
} else {
25-
$state.go('dashboard')
18+
onEnter: ['$state', '$location', '$stateParams', 'TcAuthService', 'logger',
19+
function($state, $location, $stateParams, TcAuthService, logger) {
20+
logger.debug('Checking for authentication...')
21+
if (TcAuthService.isAuthenticated()) {
22+
// redirect to next if exists else dashboard
23+
if ($stateParams.next) {
24+
logger.debug('Redirecting: ' + $stateParams.next)
25+
window.location.href = decodeURIComponent($stateParams.next)
26+
} else {
27+
$state.go('dashboard')
28+
}
2629
}
27-
}
28-
}]
30+
}]
2931
},
3032
'login': {
3133
parent: 'auth',
@@ -47,7 +49,8 @@ import angular from 'angular'
4749
controller: 'FooterController as vm',
4850
template: require('../layout/footer/account-footer')()
4951
}
50-
}
52+
},
53+
controller: 'LoginController'
5154
},
5255
'register': {
5356
parent: 'auth',

app/account/login/login.controller.js

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

34
(function() {
45
'use strict'
56

67
angular.module('tc.account').controller('LoginController', LoginController)
78

8-
LoginController.$inject = ['logger', '$state', '$stateParams', '$location', '$scope', 'TcAuthService', 'UserService', 'Helpers', 'CONSTANTS']
9+
LoginController.$inject = ['logger', '$state', '$stateParams', '$window', '$rootScope', 'Helpers', 'CONSTANTS']
910

10-
function LoginController(logger, $state, $stateParams, $location, $scope, TcAuthService, UserService, Helpers, CONSTANTS) {
11+
function LoginController(logger, $state, $stateParams, $window, $rootScope, 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)
82-
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-
}
17+
function activate() {
18+
var currentUser = getCurrentUser()
19+
logger.debug('checking for logged in user...' + currentUser)
20+
if (!currentUser) {
21+
logger.debug('loading user...')
22+
var next = $stateParams.next ? $stateParams.next : 'dashboard'
23+
loadUser().then(function(token) {
24+
logger.debug('successful login with token ' + token)
25+
$rootScope.$broadcast(CONSTANTS.EVENT_USER_LOGGED_IN)
26+
logger.debug('reidrecting to ' + next)
27+
Helpers.redirectPostLogin(next)
28+
}, function() {
29+
logger.debug('State requires authentication, and user is not logged in, redirecting')
30+
// setup redirect for post login
31+
var retUrl = $state.href(next, {}, {absolute: true})
32+
logger.debug('redirecting to accounts app for login...')
33+
$window.location = CONSTANTS.ACCOUNTS_APP_URL + '?retUrl=' + encodeURIComponent(retUrl)
9934
})
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}
10735
}
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-
})
12936
}
13037
}
13138

app/account/login/login.jade

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +0,0 @@
1-
- var logoMobile = require("../../../assets/images/logo_mobile.svg")
2-
3-
.login-container
4-
header
5-
a.logo-link(href="/", title="Back to the home page")
6-
img(src=logoMobile, alt="Topcoder Logo")
7-
8-
h1 LOG IN TO TOPCODER
9-
10-
form(name="vm.loginForm", role="form", ng-submit="vm.loginForm.$valid && vm.login()", novalidate)
11-
.form-errors(ng-messages="vm.loginErrors")
12-
p.form-error(ng-message="USERNAME_NONEXISTANT") We couldn't find a member with that {{vm.emailOrUsername || "username"}}. Please check that you entered it correctly.
13-
14-
p.form-error(ng-message="WRONG_PASSWORD") That password is incorrect. Please check that you entered the right one.
15-
16-
p.form-error(ng-message="SOCIAL_LOGIN_ERROR") User with that profile is not registered.
17-
18-
div.validation-bar(ng-class="{'error-bar': vm.loginErrors.USERNAME_NONEXISTANT}")
19-
input(ng-model="vm.username", name="username", placeholder="Username or Email", type="text", required)
20-
21-
toggle-password
22-
23-
p.problem-signin
24-
a.forgot-password(ui-sref="resetPassword") Forgot your password?
25-
26-
button.tc-btn.tc-btn-wide(type="submit", ng-disabled="vm.loginForm.$invalid") Log In
27-
28-
section.login-options
29-
p.tc-separator
30-
span Or Log in With
31-
32-
ul.networks
33-
li.network.github
34-
a.ico(ng-click="vm.socialLogin('github')")
35-
span Github
36-
li.network.google-plus
37-
a.ico(ng-click="vm.socialLogin('google-oauth2')")
38-
span Google
39-
li.network.facebook
40-
a.ico(ng-click="vm.socialLogin('facebook')")
41-
span Facebook
42-
li.network.twitter
43-
a.ico(ng-click="vm.socialLogin('twitter')")
44-
span Twitter
45-
46-
p.join-topcoder Not a member yet?  
47-
a(ui-sref="register(vm.$stateParams)") Join now

app/account/logout/logout.controller.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@ import angular from 'angular'
88
LogoutController.$inject = ['logger', 'TcAuthService', '$window', 'CONSTANTS']
99

1010
function LogoutController(logger, TcAuthService, $window, CONSTANTS) {
11-
12-
TcAuthService.logout()
13-
.then(function() {
14-
logger.debug('Successfully logged out.')
15-
16-
// Redirect to home
11+
TcAuthService.logout().then(() => {
12+
logger.debug('MAIN_URL=> ' + CONSTANTS.MAIN_URL)
1713
$window.location.href = CONSTANTS.MAIN_URL
1814
})
1915
}

app/layout/header/header.controller.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import _ from 'lodash'
1313

1414
vm.constants = CONSTANTS
1515
vm.domain = CONSTANTS.domain
16-
vm.login = TcAuthService.login
1716
vm.checkSubmit = checkSubmit
1817
vm.searchTerm = ''
1918
vm.selectedGroup = selectedGroup

app/services/api.service.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import _ from 'lodash'
66

77
angular.module('tc.services').factory('ApiService', ApiService)
88

9-
ApiService.$inject = ['$http', 'logger', 'AuthTokenService', 'Restangular', 'CONSTANTS']
9+
ApiService.$inject = ['$http', 'logger', 'Restangular', 'CONSTANTS']
1010

11-
function ApiService($http, logger, AuthTokenService, Restangular, CONSTANTS) {
11+
function ApiService($http, logger, Restangular, CONSTANTS) {
1212
var service = {
1313
requestHandler: requestHandler,
1414
restangularV2: _getRestangularV2(),

app/services/authToken.service.spec.js

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,6 @@ describe('TcAuthToken Service', function() {
4040

4141
describe('AuthToken Service ', function() {
4242

43-
it('should call store to get v3 token', function() {
44-
expect(service.getV3Token()).to.equal('value')
45-
expect(store.get).to.be.have.been.calledWith('appiriojwt')
46-
})
47-
48-
it('should call store to set v3 token', function() {
49-
service.setV3Token('test')
50-
expect(store.set).to.be.have.been.calledWith('appiriojwt', 'test')
51-
})
52-
5343
it('should retrieve token from cookie', function() {
5444
expect(service.getV2Token()).to.equal('value')
5545
expect($cookies.get).to.be.have.been.calledWith('tcjwt')
@@ -62,11 +52,6 @@ describe('TcAuthToken Service', function() {
6252
expect(store.remove).to.be.have.been.calledWith('appiriojwt')
6353
})
6454

65-
it('should use jwtHelper to decode token', function() {
66-
expect(service.decodeToken('test')).to.equal('decodedToken')
67-
expect(jwtHelper.decodeToken).to.be.have.been.calledWith('test')
68-
})
69-
7055
})
7156

7257
describe('Auth service ', function() {
@@ -107,42 +92,5 @@ describe('TcAuthToken Service', function() {
10792
}
10893
})
10994
})
110-
111-
it('should make a POST request to /authorizations', function() {
112-
service.getTokenFromAuth0Code('test')
113-
$httpBackend.expectPOST(
114-
apiUrl + '/authorizations', {}, {
115-
'Content-Type': 'application/json',
116-
'Authorization': 'Auth0Code test'
117-
}
118-
)
119-
})
120-
121-
it('should make a POST request to exchange V2 token for V3 token', function() {
122-
service.exchangeToken('refreshToken', 'idToken')
123-
$httpBackend.expectPOST(
124-
apiUrl + '/authorizations',
125-
{
126-
param: {
127-
refreshToken: 'refreshToken',
128-
externalToken: 'idToken'
129-
}
130-
},
131-
{
132-
withCredentials: true
133-
}
134-
)
135-
})
136-
137-
it('should make a GET request to refresh V3 token', function() {
138-
service.exchangeToken('refreshToken', 'idToken')
139-
$httpBackend.expectGET(
140-
apiUrl + '/authorizations/1',
141-
{},
142-
{
143-
'Authorization': 'Bearer token'
144-
}
145-
)
146-
})
14795
})
14896
})

0 commit comments

Comments
 (0)