-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
130 lines (106 loc) · 2.66 KB
/
index.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
/**
* Load in any environment variables in .env
*/
require('localenv');
/**
* DEBUG
*/
var debug = require('debug')('bare-auth');
/**
* Module Dependencies
*/
var browserify = require('browserify-middleware');
var bodyParser = require('body-parser');
var express = require('express');
var sign = require('./lib/sign');
var env = require('./lib/env');
var logger = require('morgan');
var throng = require('throng');
var cors = require('cors');
/**
* Providers
*/
var Google = require('bare-auth-google');
var Twitter = require('bare-auth-twitter');
var LinkedIn = require('bare-auth-linkedin');
var Facebook = require('bare-auth-facebook');
/**
* Setup express server
*/
var app = module.exports = express();
/**
* Middleware
*/
app.use(cors());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
/**
* Routing
*/
if (env.GOOGLE_CLIENT_SECRET) {
debug('enabling google authentication');
app.use(Google({
client_secret: env.GOOGLE_CLIENT_SECRET,
sign: sign
}))
}
if (env.TWITTER_CONSUMER_KEY && env.TWITTER_CONSUMER_SECRET) {
debug('enabling twitter authentication');
app.use(Twitter({
consumer_key: env.TWITTER_CONSUMER_KEY,
consumer_secret: env.TWITTER_CONSUMER_SECRET,
sign: sign
}))
}
if (env.LINKEDIN_CLIENT_SECRET) {
debug('enabling linkedin authentication');
app.use(LinkedIn({
client_secret: env.LINKEDIN_CLIENT_SECRET,
sign: sign
}));
}
if (env.FACEBOOK_CLIENT_SECRET) {
debug('enabling facebook authentication');
app.use(Facebook({
client_secret: env.FACEBOOK_CLIENT_SECRET,
sign: sign
}));
}
/**
* Enable the front-end
*/
if (env.FRONTEND) {
app.set('views', __dirname + '/client');
browserify.settings({ transform: ['envify'] })
app.get('/client.js', browserify(__dirname + '/client/client.js'));
app.get('/client.css', function(req, res, next) {
res.sendFile(__dirname + '/client/client.css')
})
env.FRONTEND = 'string' == typeof env.FRONTEND ? env.FRONTEND : '/';
app.get(env.FRONTEND, function(req, res, next) {
res.render('client.jade', {
twitter: env.TWITTER_CONSUMER_SECRET && env.TWITTER_CONSUMER_KEY,
facebook: env.FACEBOOK_CLIENT_SECRET && env.FACEBOOK_CLIENT_ID,
linkedin: env.LINKEDIN_CLIENT_SECRET && env.LINKEDIN_CLIENT_ID,
google: env.GOOGLE_CLIENT_SECRET && env.GOOGLE_CLIENT_ID
});
});
}
/**
* Listen function
*/
function listen() {
app.listen(env.PORT, function() {
var addr = this.address();
console.log('listening on [%s]:%s', addr.address, addr.port);
});
}
/**
* Bind to a port if we're not already
* requiring it.
*/
if (!module.parent) {
listen();
// throng(listen);
}