forked from JavaScriptKC/stalkqr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.coffee
executable file
·120 lines (90 loc) · 3.53 KB
/
app.coffee
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
express = require 'express'
app = express.createServer()
port = process.env.PORT || 3000
url = require 'url'
path = require 'path'
stylus = require 'stylus'
passport = require 'passport'
GitHubStrategy = require('passport-github').Strategy
TwitterStrategy = require('passport-twitter').Strategy;
codes = require './controllers/codes'
TWITTER_CONSUMER_KEY = process.env.TWITTER_CONSUMER_KEY
TWITTER_CONSUMER_SECRET = process.env.TWITTER_CONSUMER_SECRET
TWITTER_CALLBACK_URL = url.resolve process.env.CALLBACK_BASE_URL, '/auth/twitter/callback'
GITHUB_CLIENT_ID = process.env.GITHUB_CLIENT_ID
GITHUB_CLIENT_SECRET = process.env.GITHUB_CLIENT_SECRET
GITHUB_CALLBACK_URL = url.resolve process.env.CALLBACK_BASE_URL, '/auth/github/callback'
SESSION_SECRET = process.env.SESSION_SECRET || 'stalkqr'
ensureAuthenticated = (req, res, next) ->
return next() if req.isAuthenticated()
res.redirect '/login'
passport.serializeUser (user, done) ->
done(null, user);
passport.deserializeUser (obj, done) ->
done(null, obj);
twitterDone = (accessToken, refreshToken, profile, done) ->
done null, profile
passport.use new GitHubStrategy
clientID: GITHUB_CLIENT_ID
clientSecret: GITHUB_CLIENT_SECRET
callbackURL: GITHUB_CALLBACK_URL
twitterDone
githubDone = (accessToken, tokenSecret, profile, done) ->
done null, profile
passport.use new TwitterStrategy
consumerKey: TWITTER_CONSUMER_KEY
consumerSecret: TWITTER_CONSUMER_SECRET
callbackURL: TWITTER_CALLBACK_URL
githubDone
app.configure () ->
app.use express.logger format: ':method :url :status'
app.use express.cookieParser()
app.use express.bodyParser()
app.use express.session secret: SESSION_SECRET
app.use passport.initialize()
app.use passport.session()
app.use express.static path.join __dirname, 'public'
app.set 'views', path.join __dirname, 'views'
app.set 'view engine', 'jade'
app.configure 'development', () ->
app.use stylus.middleware
src: path.join __dirname, 'public'
app.configure 'production', () ->
app.use stylus.middleware
src: path.join __dirname, 'public'
compress: true
app.get '/', (req, res) ->
res.render 'index',
user: req.user
isLoggedIn: req.isAuthenticated()
app.get '/account', ensureAuthenticated, (req, res) ->
res.render 'account', user: req.user
app.get '/login', (req, res) ->
res.render 'login', res.data
app.get '/auth/github', passport.authenticate('github')
app.get '/auth/twitter', passport.authenticate('twitter')
app.get '/auth/github/callback',
passport.authenticate('github', failureRedirect: '/login'), (req, res) ->
res.redirect '/'
app.get '/auth/twitter/callback',
passport.authenticate('twitter', failureRedirect: '/login'), (req, res) ->
res.redirect '/'
app.get '/generate', (req, res) ->
data = codes.generateOne req.header ('host')
res.render 'generate', data
app.get '/generate/:number', (req, res) ->
data = codes.generateMany(req.header('host'), req.params.number, null)
res.render 'generate_bulk', data
app.get '/generate/event/:event', (req, res) ->
data = codes.generateOne(req.header('host'), req.params.event)
console.log data
res.render 'generate', data
app.get '/generate/:number/event/:event', (req, res) ->
data = codes.generateMany(req.header('host'), req.params.number, req.params.event)
res.render 'generate_bulk', data
app.get '/scan/:code', (req, res) ->
res.redirect '/activate/' + req.params.code, 301
app.get '/activate/:code', (req, res) ->
res.render 'activate', code: req.params.code
app.listen port
console.log 'server listening on port ' + port