Skip to content

Commit 69a9f7c

Browse files
committed
refactor(app.js, auth.js): Extract all auth method to individual modules
1 parent 7660223 commit 69a9f7c

12 files changed

Lines changed: 406 additions & 351 deletions

File tree

app.js

Lines changed: 1 addition & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ var getImageMimeType = require('./lib/utils.js').getImageMimeType
2929
// core
3030
var config = require('./lib/config.js')
3131
var logger = require('./lib/logger.js')
32-
var auth = require('./lib/auth.js')
3332
var response = require('./lib/response.js')
3433
var models = require('./lib/models')
3534

@@ -165,7 +164,6 @@ app.use(flash())
165164
// passport
166165
app.use(passport.initialize())
167166
app.use(passport.session())
168-
auth.registerAuthMethod()
169167

170168
// serialize and deserialize
171169
passport.serializeUser(function (user, done) {
@@ -200,164 +198,10 @@ app.engine('ejs', ejs.renderFile)
200198
// set view engine
201199
app.set('view engine', 'ejs')
202200

203-
function setReturnToFromReferer (req) {
204-
var referer = req.get('referer')
205-
if (!req.session) req.session = {}
206-
req.session.returnTo = referer
207-
}
208-
209-
// facebook auth
210-
if (config.facebook) {
211-
app.get('/auth/facebook', function (req, res, next) {
212-
setReturnToFromReferer(req)
213-
passport.authenticate('facebook')(req, res, next)
214-
})
215-
// facebook auth callback
216-
app.get('/auth/facebook/callback',
217-
passport.authenticate('facebook', {
218-
successReturnToOrRedirect: config.serverurl + '/',
219-
failureRedirect: config.serverurl + '/'
220-
}))
221-
}
222-
// twitter auth
223-
if (config.twitter) {
224-
app.get('/auth/twitter', function (req, res, next) {
225-
setReturnToFromReferer(req)
226-
passport.authenticate('twitter')(req, res, next)
227-
})
228-
// twitter auth callback
229-
app.get('/auth/twitter/callback',
230-
passport.authenticate('twitter', {
231-
successReturnToOrRedirect: config.serverurl + '/',
232-
failureRedirect: config.serverurl + '/'
233-
}))
234-
}
235-
// github auth
236-
if (config.github) {
237-
app.get('/auth/github', function (req, res, next) {
238-
setReturnToFromReferer(req)
239-
passport.authenticate('github')(req, res, next)
240-
})
241-
// github auth callback
242-
app.get('/auth/github/callback',
243-
passport.authenticate('github', {
244-
successReturnToOrRedirect: config.serverurl + '/',
245-
failureRedirect: config.serverurl + '/'
246-
}))
247-
if (!config.gitlab.scope || config.gitlab.scope === 'api') {
248-
// gitlab callback actions
249-
app.get('/auth/gitlab/callback/:noteId/:action', response.gitlabActions)
250-
}
251-
}
252-
// gitlab auth
253-
if (config.gitlab) {
254-
app.get('/auth/gitlab', function (req, res, next) {
255-
setReturnToFromReferer(req)
256-
passport.authenticate('gitlab')(req, res, next)
257-
})
258-
// gitlab auth callback
259-
app.get('/auth/gitlab/callback',
260-
passport.authenticate('gitlab', {
261-
successReturnToOrRedirect: config.serverurl + '/',
262-
failureRedirect: config.serverurl + '/'
263-
}))
264-
// gitlab callback actions
265-
app.get('/auth/gitlab/callback/:noteId/:action', response.gitlabActions)
266-
}
267-
// dropbox auth
268-
if (config.dropbox) {
269-
app.get('/auth/dropbox', function (req, res, next) {
270-
setReturnToFromReferer(req)
271-
passport.authenticate('dropbox-oauth2')(req, res, next)
272-
})
273-
// dropbox auth callback
274-
app.get('/auth/dropbox/callback',
275-
passport.authenticate('dropbox-oauth2', {
276-
successReturnToOrRedirect: config.serverurl + '/',
277-
failureRedirect: config.serverurl + '/'
278-
}))
279-
}
280-
// google auth
281-
if (config.google) {
282-
app.get('/auth/google', function (req, res, next) {
283-
setReturnToFromReferer(req)
284-
passport.authenticate('google', { scope: ['profile'] })(req, res, next)
285-
})
286-
// google auth callback
287-
app.get('/auth/google/callback',
288-
passport.authenticate('google', {
289-
successReturnToOrRedirect: config.serverurl + '/',
290-
failureRedirect: config.serverurl + '/'
291-
}))
292-
}
293-
// ldap auth
294-
if (config.ldap) {
295-
app.post('/auth/ldap', urlencodedParser, function (req, res, next) {
296-
if (!req.body.username || !req.body.password) return response.errorBadRequest(res)
297-
setReturnToFromReferer(req)
298-
passport.authenticate('ldapauth', {
299-
successReturnToOrRedirect: config.serverurl + '/',
300-
failureRedirect: config.serverurl + '/',
301-
failureFlash: true
302-
})(req, res, next)
303-
})
304-
}
305-
// email auth
306-
if (config.email) {
307-
if (config.allowemailregister) {
308-
app.post('/register', urlencodedParser, function (req, res, next) {
309-
if (!req.body.email || !req.body.password) return response.errorBadRequest(res)
310-
if (!validator.isEmail(req.body.email)) return response.errorBadRequest(res)
311-
models.User.findOrCreate({
312-
where: {
313-
email: req.body.email
314-
},
315-
defaults: {
316-
password: req.body.password
317-
}
318-
}).spread(function (user, created) {
319-
if (user) {
320-
if (created) {
321-
if (config.debug) {
322-
logger.info('user registered: ' + user.id)
323-
}
324-
req.flash('info', "You've successfully registered, please signin.")
325-
} else {
326-
if (config.debug) {
327-
logger.info('user found: ' + user.id)
328-
}
329-
req.flash('error', 'This email has been used, please try another one.')
330-
}
331-
return res.redirect(config.serverurl + '/')
332-
}
333-
req.flash('error', 'Failed to register your account, please try again.')
334-
return res.redirect(config.serverurl + '/')
335-
}).catch(function (err) {
336-
logger.error('auth callback failed: ' + err)
337-
return response.errorInternalError(res)
338-
})
339-
})
340-
}
341201
app.use(require('./lib/web/baseRouter'))
342202
app.use(require('./lib/web/statusRouter'))
203+
app.use(require('./lib/web/auth'))
343204

344-
app.post('/login', urlencodedParser, function (req, res, next) {
345-
if (!req.body.email || !req.body.password) return response.errorBadRequest(res)
346-
if (!validator.isEmail(req.body.email)) return response.errorBadRequest(res)
347-
setReturnToFromReferer(req)
348-
passport.authenticate('local', {
349-
successReturnToOrRedirect: config.serverurl + '/',
350-
failureRedirect: config.serverurl + '/',
351-
failureFlash: 'Invalid email or password.'
352-
})(req, res, next)
353-
})
354-
}
355-
// logout
356-
app.get('/logout', function (req, res) {
357-
if (config.debug && req.isAuthenticated()) { logger.info('user logout: ' + req.user.id) }
358-
req.logout()
359-
res.redirect(config.serverurl + '/')
360-
})
361205
var history = require('./lib/history.js')
362206
// get history
363207
app.get('/history', history.historyGet)

lib/auth.js

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

0 commit comments

Comments
 (0)