Skip to content

Commit 8b6cc13

Browse files
committed
Move passport/storage config to separate files. Remove default providers.json
1 parent 7ae7c74 commit 8b6cc13

4 files changed

Lines changed: 101 additions & 164 deletions

File tree

providers.json

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

server/boot/passport.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict';
2+
3+
module.exports = function (app) {
4+
5+
var bodyParser = require('body-parser');
6+
7+
// to support JSON-encoded bodies
8+
app.use(bodyParser.json());
9+
// to support URL-encoded bodies
10+
app.use(bodyParser.urlencoded({
11+
extended: true
12+
}));
13+
14+
// The access token is only available after boot
15+
app.use(app.loopback.token({
16+
model: app.models.accessToken
17+
}));
18+
19+
// Enable http session
20+
app.use(app.loopback.session({
21+
secret: 'kitty',
22+
saveUninitialized: true,
23+
resave: true
24+
}));
25+
26+
var config = false;
27+
try {
28+
config = require('../../providers.json');
29+
} catch (err) {
30+
console.error('Please configure your passport strategy in `providers.json`.');
31+
console.error('Copy `providers.json.template` to `providers.json` and replace the clientID/clientSecret values with your own.');
32+
}
33+
34+
35+
if (config) {
36+
console.log('Configuring passport');
37+
38+
var loopbackPassport = require('loopback-component-passport');
39+
40+
var PassportConfigurator = loopbackPassport.PassportConfigurator;
41+
var passportConfigurator = new PassportConfigurator(app);
42+
43+
44+
// Initialize passport
45+
passportConfigurator.init();
46+
47+
// Set up related models
48+
passportConfigurator.setupModels({
49+
userModel: app.models.user,
50+
userIdentityModel: app.models.userIdentity,
51+
userCredentialModel: app.models.userCredential
52+
});
53+
54+
// Configure passport strategies for third party auth providers
55+
for (var s in config) {
56+
var c = config[s];
57+
c.session = c.session !== false;
58+
passportConfigurator.configureProvider(s, c);
59+
}
60+
61+
}
62+
63+
var ensureLoggedIn = require('connect-ensure-login').ensureLoggedIn;
64+
65+
app.get('/auth/account', ensureLoggedIn('/login.html'), function (req, res, next) {
66+
res.render('pages/loginProfiles', {
67+
user: req.user,
68+
url: req.url
69+
});
70+
});
71+
72+
app.get('/link/account', ensureLoggedIn('/login.html'), function (req, res, next) {
73+
res.render('pages/linkedAccounts', {
74+
user: req.user,
75+
url: req.url
76+
});
77+
});
78+
79+
app.get('/auth/logout', function (req, res, next) {
80+
req.logout();
81+
res.redirect('/');
82+
});
83+
84+
85+
};

server/boot/storage.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
var path = require('path');
4+
5+
module.exports = function (app) {
6+
7+
var ds = app.loopback.createDataSource({
8+
connector: require('loopback-component-storage'),
9+
provider: 'filesystem',
10+
root: path.join(__dirname, '../', '../', 'storage')
11+
});
12+
var container = ds.createModel('container');
13+
14+
app.model(container);
15+
16+
};

server/server.js

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,6 @@ var path = require('path');
66
var app = module.exports = loopback();
77
var env = require('get-env')();
88

9-
/*
10-
* body-parser is a piece of express middleware that
11-
* reads a form's input and stores it as a javascript
12-
* object accessible through `req.body`
13-
*
14-
*/
15-
var bodyParser = require('body-parser');
16-
17-
// Passport configurators..
18-
var loopbackPassport = require('loopback-component-passport');
19-
20-
var PassportConfigurator = loopbackPassport.PassportConfigurator;
21-
var passportConfigurator = new PassportConfigurator(app);
22-
23-
//app.use(function(req, res, next) {
24-
// res.setHeader('X-Powered-By', 'Loopback Admin');
25-
// res.removeHeader('Vary');
26-
// next();
27-
//});
28-
299
// Set up the /favicon.ico
3010
app.use(loopback.favicon());
3111

@@ -34,84 +14,10 @@ app.use(loopback.compress());
3414

3515
// -- Add your pre-processing middleware here --
3616

37-
var ds = loopback.createDataSource({
38-
connector: require('loopback-component-storage'),
39-
provider: 'filesystem',
40-
root: path.join(__dirname, '../', 'storage')
41-
});
42-
var container = ds.createModel('container');
43-
44-
app.model(container);
4517

4618
// boot scripts mount components like REST API
4719
boot(app, __dirname);
4820

49-
// to support JSON-encoded bodies
50-
app.use(bodyParser.json());
51-
// to support URL-encoded bodies
52-
app.use(bodyParser.urlencoded({
53-
extended: true
54-
}));
55-
56-
// The access token is only available after boot
57-
app.use(loopback.token({
58-
model: app.models.accessToken
59-
}));
60-
61-
// Enable http session
62-
//app.use(loopback.cookieParser(app.get('cookieSecret')));
63-
app.use(loopback.session({
64-
secret: 'kitty',
65-
saveUninitialized: true,
66-
resave: true
67-
}));
68-
69-
// Load the provider configurations
70-
var config = {};
71-
try {
72-
config = require('../providers.json');
73-
} catch (err) {
74-
console.error('Please configure your passport strategy in `providers.json`.');
75-
console.error('Copy `providers.json.template` to `providers.json` and replace the clientID/clientSecret values with your own.');
76-
process.exit(1);
77-
}
78-
// Initialize passport
79-
passportConfigurator.init();
80-
81-
// Set up related models
82-
passportConfigurator.setupModels({
83-
userModel: app.models.user,
84-
userIdentityModel: app.models.userIdentity,
85-
userCredentialModel: app.models.userCredential
86-
});
87-
// Configure passport strategies for third party auth providers
88-
for (var s in config) {
89-
var c = config[s];
90-
c.session = c.session !== false;
91-
passportConfigurator.configureProvider(s, c);
92-
}
93-
94-
95-
var ensureLoggedIn = require('connect-ensure-login').ensureLoggedIn;
96-
97-
app.get('/auth/account', ensureLoggedIn('/login.html'), function (req, res, next) {
98-
res.render('pages/loginProfiles', {
99-
user: req.user,
100-
url: req.url
101-
});
102-
});
103-
104-
app.get('/link/account', ensureLoggedIn('/login.html'), function (req, res, next) {
105-
res.render('pages/linkedAccounts', {
106-
user: req.user,
107-
url: req.url
108-
});
109-
});
110-
111-
app.get('/auth/logout', function (req, res, next) {
112-
req.logout();
113-
res.redirect('/');
114-
});
11521

11622
// -- Mount static files here--
11723
// All static middleware should be registered at the end, as all requests

0 commit comments

Comments
 (0)