From 10786e48227cbdbc1af2834d62433bd3ea9364d9 Mon Sep 17 00:00:00 2001 From: Edgar Dewsbery Date: Thu, 13 Apr 2017 12:17:59 +0300 Subject: [PATCH] #8 require authentication on all routes except for /login --- src/router.js | 24 ++++++++++++++++++++++++ src/server.js | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/router.js b/src/router.js index 3e63e47..012205f 100644 --- a/src/router.js +++ b/src/router.js @@ -8,6 +8,12 @@ const jwt = require('jsonwebtoken'); const staticFiles = { method: 'GET', path: '/{file}', + config: { + auth: { + strategy: 'jwt', + mode: 'optional' + } + }, handler: { directory: { path: path.join(__dirname, '../public') @@ -18,6 +24,12 @@ const staticFiles = { const loginButton = { method: 'GET', path: '/', + config: { + auth: { + strategy: 'jwt', + mode: 'optional' + } + }, handler: (request, reply) => { const loginContent = { text: 'Login' @@ -31,6 +43,12 @@ const loginButton = { const githubOAuth = { method: 'GET', path: '/login', + config: { + auth: { + strategy: 'jwt', + mode: 'optional' + } + }, handler: (request, reply) => { const base = 'https://github.com/login/oauth/authorize?'; const oAuthParams = { @@ -45,6 +63,12 @@ const githubOAuth = { const welcome = { method: 'GET', path: '/welcome', + config: { + auth: { + strategy: 'jwt', + mode: 'optional' + } + }, handler: (request, reply) => { const data = { client_id: process.env.CLIENT_ID, diff --git a/src/server.js b/src/server.js index e46e13d..244a1d2 100644 --- a/src/server.js +++ b/src/server.js @@ -29,7 +29,7 @@ server.register([inert, vision, hapiAuth], (err) => { } return callback(null, true); }; - server.auth.strategy('jwt', 'jwt', { + server.auth.strategy('jwt', 'jwt', 'required', { key: process.env.SECRET, validateFunc: validate, verifyOptions: {