Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Commit

Permalink
feat(server/express/index): add auth middleware, add morgan
Browse files Browse the repository at this point in the history
  • Loading branch information
Metnew committed Jul 15, 2017
1 parent 6054833 commit c228820
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/server/express/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
// Express stuff
// Express-related stuff
import express from 'express'
import expressHelmet from 'helmet'
import compression from 'compression'
import path from 'path'
import fs from 'fs'
import cookieParser from 'cookie-parser'
import morgan from 'morgan'
// Application-related stuff
// Do you remember that we use webpack aliases provided by cool babel plugin in .babelrc
import {JWT_TOKEN} from 'common/api'
//
const language = process.env.APP_LANGUAGE || 'en'
const distPath = `../../../dist/${language}`
const app = express()
// add express stuff
app.use(compression())
app.use(morgan('dev'))
app.use(cookieParser())
app.use(
express.static(path.join(__dirname, distPath), {
Expand All @@ -21,4 +26,18 @@ app.use(
app.disable('x-powered-by')
app.use(expressHelmet())

// Auth-related middleware, check that user is logged in and token is valid
app.use((req, res, next) => {
// Of course, you can make auth with jwt or cookies or session only
// It's a starter project and I implemented a hardcoded solution
const token = req.cookies[JWT_TOKEN]
const isLoggedIn = token === 'nothing'
console.log(`USER IS LOGGED IN: ${isLoggedIn ? 'YES': 'NO'}`)
req.user = {
token,
isLoggedIn
}
next()
})

export default app

0 comments on commit c228820

Please sign in to comment.