Skip to content

Commit ad88478

Browse files
committed
Add AuthProvider model, add providers to API and make login screen dynamic
1 parent 8b6cc13 commit ad88478

6 files changed

Lines changed: 146 additions & 95 deletions

File tree

client/app/modules/users/controllers/login.ctrl.js

Lines changed: 93 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -8,99 +8,103 @@
88
* @requires $location
99
* Contrller for Login Page
1010
**/
11-
angular.module ('com.module.users')
12-
.controller ('LoginCtrl', function ($scope, $routeParams, $location, toasty, User, AppAuth, gettextCatalog) {
13-
14-
var TWO_WEEKS = 1000 * 60 * 60 * 24 * 7 * 2;
15-
16-
$scope.credentials = {
17-
email: 'admin@admin.com',
18-
password: 'admin',
19-
ttl: TWO_WEEKS,
20-
rememberMe: true
21-
};
22-
23-
$scope.schema = [
24-
{
25-
label: '',
26-
property: 'email',
27-
placeholder: 'email',
28-
type: 'email',
29-
attr: {required: true, ngMinlength: 4},
30-
msgs: {
31-
required: 'You need an email address',
32-
email: 'Email address needs to be valid',
33-
valid: 'Nice email address!'
34-
}
35-
},
36-
{
37-
label: '',
38-
property: 'password',
39-
placehodler: gettextCatalog.getString ('Password'),
40-
type: 'password',
41-
attr: {required: true}
42-
},
43-
{
44-
property: 'rememberMe',
45-
label: gettextCatalog.getString ('Stay signed in'),
46-
type: 'checkbox'
47-
}
48-
];
49-
50-
$scope.options = {
51-
validation: {
52-
enabled: true,
53-
showMessages: false
54-
},
55-
layout: {
56-
type: 'basic',
57-
labelSize: 3,
58-
inputSize: 9
59-
}
60-
};
61-
62-
$scope.loginGoogle = function () {
63-
window.location = '/auth/google';
64-
};
65-
66-
$scope.loginFacebook = function () {
67-
window.location = '/auth/facebook';
68-
};
69-
70-
$scope.login = function () {
71-
72-
73-
$scope.loginResult = User.login ({
74-
include: 'user',
75-
rememberMe: $scope.credentials.rememberMe
76-
}, $scope.credentials,
77-
function (user) {
78-
79-
console.log (user.id); // => acess token
80-
console.log (user.ttl); // => 1209600 time to live
81-
console.log (user.created); // => 2013-12-20T21:10:20.377Z
82-
console.log (user.userId); // => 1
83-
84-
var next = $location.nextAfterLogin || '/';
85-
$location.nextAfterLogin = null;
86-
AppAuth.currentUser = $scope.loginResult.user;
87-
toasty.pop.success ({
88-
title: gettextCatalog.getString ('Logged in'),
89-
msg: gettextCatalog.getString ('You are logged in!'),
90-
sound: false
91-
});
92-
if (next === '/login') {
93-
next = '/';
11+
angular.module('com.module.users')
12+
.controller('LoginCtrl', function ($scope, $routeParams, $location, toasty, User, AppAuth, AuthProvider, gettextCatalog) {
13+
14+
var TWO_WEEKS = 1000 * 60 * 60 * 24 * 7 * 2;
15+
16+
$scope.credentials = {
17+
email: 'admin@admin.com',
18+
password: 'admin',
19+
ttl: TWO_WEEKS,
20+
rememberMe: true
21+
};
22+
23+
$scope.schema = [
24+
{
25+
label: '',
26+
property: 'email',
27+
placeholder: 'email',
28+
type: 'email',
29+
attr: {required: true, ngMinlength: 4},
30+
msgs: {
31+
required: 'You need an email address',
32+
email: 'Email address needs to be valid',
33+
valid: 'Nice email address!'
9434
}
95-
$location.path (next);
35+
},
36+
{
37+
label: '',
38+
property: 'password',
39+
placehodler: gettextCatalog.getString('Password'),
40+
type: 'password',
41+
attr: {required: true}
42+
},
43+
{
44+
property: 'rememberMe',
45+
label: gettextCatalog.getString('Stay signed in'),
46+
type: 'checkbox'
47+
}
48+
];
9649

50+
$scope.options = {
51+
validation: {
52+
enabled: true,
53+
showMessages: false
9754
},
98-
function (res) {
99-
$scope.loginError = res.data.error;
100-
});
55+
layout: {
56+
type: 'basic',
57+
labelSize: 3,
58+
inputSize: 9
59+
}
60+
};
61+
62+
$scope.socialLogin = function (provider) {
63+
window.location = provider.authPath;
64+
};
65+
66+
AuthProvider.count(function (result) {
67+
if (result.count > 0) {
68+
AuthProvider.find(function (result) {
69+
$scope.authProviders = result;
70+
});
71+
}
72+
});
73+
74+
$scope.login = function () {
75+
76+
77+
$scope.loginResult = User.login({
78+
include: 'user',
79+
rememberMe: $scope.credentials.rememberMe
80+
}, $scope.credentials,
81+
function (user) {
82+
83+
console.log(user.id); // => acess token
84+
console.log(user.ttl); // => 1209600 time to live
85+
console.log(user.created); // => 2013-12-20T21:10:20.377Z
86+
console.log(user.userId); // => 1
87+
88+
var next = $location.nextAfterLogin || '/';
89+
$location.nextAfterLogin = null;
90+
AppAuth.currentUser = $scope.loginResult.user;
91+
toasty.pop.success({
92+
title: gettextCatalog.getString('Logged in'),
93+
msg: gettextCatalog.getString('You are logged in!'),
94+
sound: false
95+
});
96+
if (next === '/login') {
97+
next = '/';
98+
}
99+
$location.path(next);
100+
101+
},
102+
function (res) {
103+
$scope.loginError = res.data.error;
104+
});
101105

102106

103-
};
107+
};
104108

105109

106-
});
110+
});

client/app/modules/users/views/login.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
<div class="footer">
1616
<button type="submit" class="btn bg-olive btn-block" translate>Sign in</button>
1717
<a href='#/register' class="btn bg-yellow btn-block" translate>Or register</a>
18-
<button type="button" ng-click="loginGoogle()" class="btn bg-red btn-block">Google</button>
19-
<button type="button" ng-click="loginFacebook()" class="btn bg-blue btn-block">Facebook</button>
18+
<a ng-repeat="provider in authProviders" ng-hide="provider.link" ng-click="socialLogin(provider)"
19+
class="btn btn-block btn-{{provider.class}}">
20+
{{provider.provider | ucfirst}}
21+
</a>
2022
</div>
2123
</form>
2224
</div>

common/models/auth-provider.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function(AuthProvider) {
2+
3+
};

common/models/auth-provider.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "AuthProvider",
3+
"plural": "AuthProviders",
4+
"base": "PersistedModel",
5+
"strict": false,
6+
"properties": {},
7+
"validations": [],
8+
"relations": {},
9+
"acls": [],
10+
"methods": []
11+
}

server/boot/passport.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ module.exports = function (app) {
3535
if (config) {
3636
console.log('Configuring passport');
3737

38+
var AuthProvider = app.models.AuthProvider;
39+
3840
var loopbackPassport = require('loopback-component-passport');
3941

4042
var PassportConfigurator = loopbackPassport.PassportConfigurator;
@@ -51,11 +53,35 @@ module.exports = function (app) {
5153
userCredentialModel: app.models.userCredential
5254
});
5355

54-
// Configure passport strategies for third party auth providers
56+
// Configure passport strategies for third party auth providers and add them to the API
5557
for (var s in config) {
5658
var c = config[s];
57-
c.session = c.session !== false;
58-
passportConfigurator.configureProvider(s, c);
59+
60+
if (c.provider != 'local') {
61+
62+
var providerClass = c.provider;
63+
if (c.provider === 'google') {
64+
var providerClass = 'google-plus';
65+
}
66+
67+
var entry = {
68+
name: s,
69+
link: c.link,
70+
authPath: c.authPath,
71+
provider: c.provider,
72+
class: providerClass
73+
};
74+
75+
AuthProvider.create(entry, function (err, data) {
76+
if (err) {
77+
console.log(err);
78+
}
79+
});
80+
81+
c.session = c.session !== false;
82+
passportConfigurator.configureProvider(s, c);
83+
}
84+
5985
}
6086

6187
}
@@ -82,4 +108,5 @@ module.exports = function (app) {
82108
});
83109

84110

85-
};
111+
}
112+
;

server/model-config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@
6262
"dataSource": "db",
6363
"public": true
6464
},
65+
"AuthProvider": {
66+
"dataSource": "db",
67+
"public": true
68+
},
6569
"user": {
6670
"dataSource": "db",
6771
"public": true

0 commit comments

Comments
 (0)