I'm writing an auction app in JS, using Vue and express-session, with passport-local authentication. I've coded the API already, tested it with Postman, and everything seems to be working just fine. My Vue app actions are basically API calls, so imo everything also should be ok. But for some reason while calling API in Postman you can get the user from session without a sweat, and it's not working when I try to do the same in my Vue app. The only function that is currently working for both API and Vue is login - the rest of them in Vue fails to get the session data from my MongoDB. But I don't know why. I'm a complete beginner to JS.I'm stuck in my assignment because I've no idea what's wrong. I've tried so many approaches to this problem that I'd have a problem counting them all. I'd really, really appreciate your help.I hope these examples of my code will help you find the source of my problems:index.js:const express = require('express'); const mongoose = require('mongoose'); const bodyParser = require('body-parser'); const cookieParser = require('cookie-parser'); const session = require('express-session'); const cors = require('cors'); const path = require('path'); const passport = require('passport'); const app = express(); const https = require('https'); const fs = require('fs'); const MongoStore = require("connect-mongo")(session); const sessionStore = new MongoStore({ mongooseConnection: mongoose.connection }); const options = { key: fs.readFileSync('key.pem'), cert: fs.readFileSync('cert.pem') }; app.use(session({ store: sessionStore, secret: ['6ajvmso28tnxlao28yppx//f0s@q4=6822!ssgielwvz[q[iy;wmvcx'], resave: false, saveUninitialized: false })); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); app.use(cookieParser('6ajvmso28tnxlao28yppx//f0s@q4=6822!ssgielwvz[q[iy;wmvcx')); app.use(cors()); app.use(express.static(path.join(__dirname, 'public'))); app.use(passport.initialize()); app.use(passport.session()); require('./config/passport')(passport); const db = require('./config/keys').mongoURI; mongoose.connect(db, {useNewUrlParser: true}).then(() => { console.log(`Database connected successfully ${db}`) }).catch(err => { console.log(`Unable to connect to database; ${err}`) }); const users = require('./routes/api/users'); app.use('/api/users', users); const offers = require('./routes/api/offers'); app.use('/api/offers', offers); const port = process.env.PORT || 8080; var httpsServer = https.createServer(options, app); httpsServer.listen(port); /login API route, the only thing that's working with Vue rnrouter.get('/profile', function (req, res) { return res.json(req.session.passport.user); }); /logout that works tested on Postman, but doesn't remove the object from database when used from Vuerouter.get('/logout', function (req, res) { req.logOut(); req.session.destroy(function (err) { res.json({ msg: "Logged out!" }); }); }); /profile is one of the routes that try to get req.session.passport.user but it's undefinedrouter.get('/profile', function (req, res) { return res.json(req.session.passport.user); }); Vue Auth.js warehouse (store?) moduleimport Router from '../router/index'; import Axios from "axios"; const state = { user: {}, test: false, status: '' }; const getters = { isLoggedIn: function(state){ return state.test }, authState: state => state.status, user: state => state.user, test: state => state.test }; const actions = { async login({ commit }, user) { commit('auth_request'); let res = await Axios.post('https://localhost:8080/api/users/login', user); console.log(res); if(res.statusText === "OK"){ commit('auth_success', res.data); } return res; }, async register({ commit }, userData) { commit('register_request'); let res = await Axios.post('https://localhost:8080/api/users/register', userData); if (res.statusText === "OK"){ commit('register_success'); } return res; }, async getProfile({commit}){ commit('profile_request'); let res = await Axios.get('https://localhost:8080/api/users/profile'); console.log(res); commit('user_profile', res.data); return res; }, async logout({commit}){ await Axios.get('https://localhost:8080/api/users/logout'); commit('logout'); Router.push('/login'); return } }; const mutations = { auth_request(state){ state.status = 'loading' }, auth_success(state, user){ state.user = user state.test = true state.status = 'success' }, register_request(state){ state.status = 'loading' }, register_success(state){ state.status = 'success' }, logout(state){ state.test = false state.status = '' state.user = {} }, profile_request(state){ state.status = 'loading' }, user_profile(state, user){ state.user = user state.status = 'success' } }; export default { state, actions, mutations, getters };
Submitted June 01, 2020 at 12:26PM by MHDRmlekoo
No comments:
Post a Comment