Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
1. `git clone git@github.com:RisingStack/nodehero-authentication.git`
2. `cd nodehero-authentication`
3. `npm install`
4. `REDIS_STORE_URI=redis://localhost REDIS_STORE_SECRET=my-strong-secret npm start`
4. `npm start REDIS_STORE_HOST = redis://localhost`

## Pre requirements

Expand Down
2 changes: 1 addition & 1 deletion app/authentication/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function authenticationMiddleware () {
if (req.isAuthenticated()) {
return next()
}
res.redirect('/')
res.redirect('/login')
}
}

Expand Down
11 changes: 7 additions & 4 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ const passport = require('passport')
const session = require('express-session')
const RedisStore = require('connect-redis')(session)

require('dotenv').config();
const config = require('../config')
const app = express()

app.use(bodyParser.urlencoded({
extended: false
}))

require('./authentication').init(app)
app.use(bodyParser.json());

app.use(session({
secret: 'Secret key',
store: new RedisStore({
url: config.redisStore.url
host: config.redisStore.host,
port: config.redisStore.port
}),
secret: config.redisStore.secret,
resave: false,
saveUninitialized: false
}))
Expand All @@ -38,6 +39,8 @@ app.engine('.hbs', exphbs({
app.set('view engine', '.hbs')
app.set('views', path.join(__dirname))


require('./authentication').init(app)
require('./user').init(app)
require('./note').init(app)

Expand Down
11 changes: 8 additions & 3 deletions app/user/init.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
const passport = require('passport')

function initUser (app) {
app.get('/', renderWelcome)
app.get('/', passport.authenticationMiddleware(), renderWelcome)
app.get('/login', renderLogin)
app.get('/profile', passport.authenticationMiddleware(), renderProfile)
app.post('/login', passport.authenticate('local', {
successRedirect: '/profile',
failureRedirect: '/'
successRedirect: '/',
failureRedirect: '/login'
}))
}

function renderWelcome (req, res) {
res.render('user/welcome')
}

function renderLogin (req, res) {
res.render('user/login')
}

function renderProfile (req, res) {
res.render('user/profile', {
username: req.user.username
Expand Down
7 changes: 7 additions & 0 deletions app/user/login.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h3>Hey! You have to log in before using NodeHero :/</h3>

<form action="/login" method="post">
<input name="username" id="username" type="text" placeholder="Your username" />
<input name="password" id="password" type="password" placeholder="Your password"/>
<input type="submit" />
</form>
9 changes: 2 additions & 7 deletions app/user/welcome.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
<h3>Hey! You have to log in before using NodeHero :/</h3>

<form action="/login" method="post">
<input name="username" id="username" type="text" placeholder="Your username" />
<input name="password" id="password" type="password" placeholder="Your password"/>
<input type="submit" />
</form>
<h2>Welcome!</h2>
<a href="/profile">Click here to watch your profile</a>
3 changes: 1 addition & 2 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const config = {}

config.redisStore = {
url: process.env.REDIS_STORE_URI,
secret: process.env.REDIS_STORE_SECRET
host: process.env.REDIS_STORE_HOST
}

module.exports = config
Loading