Skip to content

Commit eb391da

Browse files
added swagger, fix user auth bug, more linted code, remove lint gulp task
1 parent b7db462 commit eb391da

23 files changed

Lines changed: 495 additions & 44 deletions

.eslintignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
**/node_modules/**
22
**/cert/**
3-
**/static/**
4-
**/test/**
3+
**/static/**

bin/server.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ const app = new Koa()
1717
app.keys = [config.session]
1818

1919
// replace these with your certificate information
20-
const options = {
21-
cert: fs.readFileSync('./cert/localhost.cert'),
20+
const options = {
21+
cert: fs.readFileSync('./cert/localhost.cert'),
2222
key: fs.readFileSync('./cert/localhost.key')
2323
}
2424

2525
// --------------------- start -------------------------
26-
//Instead of calling convert for all legacy middlewares
27-
//just use the following to convert them all at once
26+
// Instead of calling convert for all legacy middlewares
27+
// just use the following to convert them all at once
2828

2929
const _use = app.use
3030
app.use = x => _use.call(app, convert(x))
3131

32-
//The code above avoids writting the following
33-
//app.use(convert(logger()))
32+
// The code above avoids writting the following
33+
// app.use(convert(logger()))
3434
// ---------------------- end --------------------------
3535

3636
mongoose.Promise = global.Promise
@@ -42,27 +42,32 @@ app.use(bodyParser())
4242
app.use(session())
4343
app.use(errorMiddleware())
4444

45-
//Mount static API documents generated by api-generator
45+
// Mount static API documents generated by api-generator
4646
app.use(mount('/docs', serve(`${process.cwd()}/docs`)))
4747

48-
//Using Passport for authentication
48+
// Using Passport for authentication
4949
require('../config/passport')
5050
app.use(passport.initialize())
5151
app.use(passport.session())
5252

53-
//Using module wise routing
53+
// Using module wise routing
5454
const modules = require('../src/modules')
5555
modules(app)
5656

57-
//Using http2 to work with http/2 instead of http/1.x
58-
http2
57+
// Show swagger only if the NODE_ENV is development
58+
if (!process.env.NODE_ENV || process.env.NODE_ENV === 'devlopment') {
59+
app.use(mount('/swagger', serve(`${process.cwd()}/swagger`)))
60+
}
61+
62+
// Using http2 to work with http/2 instead of http/1.x
63+
http2
5964
.createServer(options, app.callback())
6065
.listen(config.port, () => {
6166
console.log(`Server started on ${config.port}`)
62-
})
67+
})
6368

64-
//app.listen(config.port, () => {
65-
//console.log(`Server started on ${config.port}`)
66-
//})
69+
// app.listen(config.port, () => {
70+
// console.log(`Server started on ${config.port}`)
71+
// })
6772

6873
export default app

config/passport.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ passport.use('local', new Strategy({
3232
} catch (err) {
3333
done(err)
3434
}
35-
3635
} catch (err) {
3736
return done(err)
3837
}

gulpfile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const RequireDir = require('require-dir')
66
// Load tasks
77
RequireDir('./tasks')
88

9-
Gulp.task('dev', ['lint', 'dotenv', 'yaml2json', 'nodemon'])
10-
Gulp.task('prod', ['lint', 'pm2'])
9+
Gulp.task('dev', ['dotenv', 'yaml2json', 'nodemon'])
10+
Gulp.task('prod', ['pm2'])
1111

1212
Gulp.task('default', ['dev'])

src/models/users.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ User.pre('save', function preSave (next) {
2323
resolve(salt)
2424
})
2525
})
26-
.then(salt => {
27-
bcrypt.hash(user.password, salt, (err, hash) => {
28-
if (err) { throw new Error(err) }
26+
.then(salt => {
27+
bcrypt.hash(user.password, salt, (err, hash) => {
28+
if (err) { throw new Error(err) }
2929

30-
user.password = hash
30+
user.password = hash
3131

32-
next(null)
32+
next(null)
33+
})
3334
})
34-
})
35-
.catch(err => next(err))
35+
.catch(err => next(err))
3636
})
3737

3838
User.methods.validatePassword = function validatePassword (password) {

src/modules/auth/controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ import passport from 'koa-passport'
5151
*/
5252

5353
export async function authUser (ctx, next) {
54-
return passport.authenticate('local', (user) => {
55-
if (!user) {
54+
return passport.authenticate('local', (err, user) => {
55+
if (err || !user) {
5656
ctx.throw(401)
5757
}
5858

src/modules/home/controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ export async function getHome (ctx) {
44

55
export async function getLogin (ctx) {
66
ctx.body = '<b>The KOACH login page is WIP.</b>'
7-
}
7+
}

src/modules/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ exports = module.exports = function initModules (app) {
2121

2222
const lastHandler = handlers.pop()
2323

24-
instance[method.toLowerCase()](route, ...handlers, async function(ctx) {
24+
instance[method.toLowerCase()](route, ...handlers, async function (ctx) {
2525
await lastHandler(ctx)
2626
})
2727

swagger/favicon-16x16.png

445 Bytes
Loading

swagger/favicon-32x32.png

1.11 KB
Loading

0 commit comments

Comments
 (0)