|
@@ -0,0 +1,100 @@
|
|
|
+import express from 'express'
|
|
|
+import bodyParser from 'body-parser'
|
|
|
+import morgan from 'morgan'
|
|
|
+import mongoose from 'mongoose'
|
|
|
+import passport from 'passport'
|
|
|
+import jwt from 'jwt-simple'
|
|
|
+
|
|
|
+import config from './config/database'
|
|
|
+import { configPassport } from './config/passport'
|
|
|
+import User from './models/user'
|
|
|
+
|
|
|
+const port = process.env.PORT || 8080
|
|
|
+const app = express()
|
|
|
+
|
|
|
+app.use(bodyParser.urlencoded({ extended: false }))
|
|
|
+app.use(bodyParser.json())
|
|
|
+app.use(morgan('dev'))
|
|
|
+app.use(passport.initialize())
|
|
|
+
|
|
|
+app.get('/', (req, res) => {
|
|
|
+ res.send(`Express API at http://localhost:${port}/api`)
|
|
|
+})
|
|
|
+
|
|
|
+mongoose.connect(config.database)
|
|
|
+configPassport(passport)
|
|
|
+const apiRoutes = express.Router()
|
|
|
+
|
|
|
+apiRoutes.post('/signup', (req, res) => {
|
|
|
+ if (!req.body.name || !req.body.password) {
|
|
|
+ res.json({ success: false, msg: 'Please pass name and password.' })
|
|
|
+ } else {
|
|
|
+ const newUser = new User({
|
|
|
+ name: req.body.name,
|
|
|
+ password: req.body.password
|
|
|
+ })
|
|
|
+ newUser.save(err => {
|
|
|
+ if (err) {
|
|
|
+ return res.json({ success: false, msg: 'Username already exists.' })
|
|
|
+ }
|
|
|
+ res.json({ success: true, msg: 'Successfully created user.' })
|
|
|
+ })
|
|
|
+ }
|
|
|
+})
|
|
|
+
|
|
|
+apiRoutes.post('/authenticate', (req, res) => {
|
|
|
+ User.findOne({ name: req.body.name }, (err, user) => {
|
|
|
+ if (err) {
|
|
|
+ throw err
|
|
|
+ }
|
|
|
+ if (!user) {
|
|
|
+ res.send({ success: false, msg: 'Authentication failed. User not found.' })
|
|
|
+ } else {
|
|
|
+ user.comparePassword(req.body.password, (err, isMatch) => {
|
|
|
+ if (isMatch && !err) {
|
|
|
+ const token = jwt.encode(user, config.secret)
|
|
|
+ res.json({ success: true, token: `JWT ${token}` })
|
|
|
+ } else {
|
|
|
+ res.send({ soccess: false, msg: 'Authentication failed. Wrong password.' })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+})
|
|
|
+
|
|
|
+apiRoutes.get('/memberinfo', passport.authenticate('jwt', { session: false }), (req, res) => {
|
|
|
+ const token = getToken(req.headers)
|
|
|
+ if (token) {
|
|
|
+ const decoded = jwt.decode(token, config.secret)
|
|
|
+ User.findOne({ name: decoded.name }, (err, user) => {
|
|
|
+ if (err) {
|
|
|
+ throw err
|
|
|
+ }
|
|
|
+ if (!user) {
|
|
|
+ return res.status(403).send({ success: false, msg: 'Authentication failed. User not found.' })
|
|
|
+ } else {
|
|
|
+ res.json({ success: true, msg: `Welcome in the member area, ${user.name}!` })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ return res.status(403).send({ success: false, msg: 'No token provided.' })
|
|
|
+ }
|
|
|
+})
|
|
|
+
|
|
|
+function getToken (headers) {
|
|
|
+ if (headers && headers.authorization) {
|
|
|
+ const parted = headers.authorization.split(' ')
|
|
|
+ if (parted.length === 2) {
|
|
|
+ return parted[1]
|
|
|
+ } else {
|
|
|
+ return null
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return null
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+app.use('/api', apiRoutes)
|
|
|
+
|
|
|
+app.listen(port)
|
|
|
+console.log('Server running.')
|