From a9adbdc856f8057cf31234410c62c3a689b97007 Mon Sep 17 00:00:00 2001 From: Kevin Thomas Date: Thu, 29 Jul 2021 22:24:39 -0700 Subject: [PATCH] Add session authentication --- server/app.js | 1 - server/boot/auth.js | 2 -- server/routes/auth.js | 16 +++------------- src/main.js | 7 ++++++- src/router/index.js | 2 +- 5 files changed, 10 insertions(+), 18 deletions(-) diff --git a/server/app.js b/server/app.js index 952407e..dbf6c05 100644 --- a/server/app.js +++ b/server/app.js @@ -40,7 +40,6 @@ app.use(function (req, res, next) { }) app.use(passport.initialize()) app.use(passport.authenticate('session')) -//app.use(passport.session()); // Define routes app.use('/', indexRouter) diff --git a/server/boot/auth.js b/server/boot/auth.js index 86251e2..69faa9c 100644 --- a/server/boot/auth.js +++ b/server/boot/auth.js @@ -39,14 +39,12 @@ module.exports = function () { // serializing, and querying the user record by ID from the database when // deserializing. passport.serializeUser(function (user, cb) { - console.log('serializing user: '); process.nextTick(function () { cb(null, { id: user.id, username: user.username }) }) }) passport.deserializeUser(function (user, cb) { - console.log("DESERIALIZE") process.nextTick(function () { return cb(null, user) }) diff --git a/server/routes/auth.js b/server/routes/auth.js index 44d5ce2..c42107a 100644 --- a/server/routes/auth.js +++ b/server/routes/auth.js @@ -4,24 +4,14 @@ const passport = require('passport') const router = express.Router() router.post('/login/password', passport.authenticate('local'), function (req, res, next) { - console.log('login cookies: ', req.cookies) - console.log('login is authenticated: ', req.isAuthenticated()) - console.log('/login/password req.user: ', req.user) - console.log('login session: ', req.session) - //res.json(req.user) - return res.send('You were authenticated & logged in!\n'); + return res.send('Authenticated and logged in') }) router.get('/auth', function (req, res) { - console.log('auth cookies: ', req.cookies) - console.log('in /auth') - console.log('auth is authenticated: ', req.isAuthenticated()) - console.log('/auth req.user: ', req.user) - console.log('auth session: ', req.session) if (req.user) { - res.status(200).end() + res.sendStatus(200) } else { - res.status(401).end() + res.sendStatus(401) } }) diff --git a/src/main.js b/src/main.js index c4ad277..fe4257a 100644 --- a/src/main.js +++ b/src/main.js @@ -4,7 +4,12 @@ import router from './router' import vuetify from './plugins/vuetify' import Axios from 'axios' -Vue.prototype.$http = Axios +const instance = Axios.create({ + baseURL: 'https://localhost:3000', + withCredentials: true +}) + +Vue.prototype.$http = instance Vue.config.productionTip = false diff --git a/src/router/index.js b/src/router/index.js index 1e744ff..f0e1a31 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -41,7 +41,7 @@ const router = new VueRouter({ }) router.beforeEach((to, from, next) => { - if (to.name === 'TMP_Home') { + if (to.name === 'Home') { instance.get('/auth') .then(response => { if (response.status === 200) {