I am working with the example of auth routes to create a persistent login system but every time I try to include the call to the api to get user data to fill "req.session.authUser" in "/api/index.js" doesn't work. The store doesn't recognize the route.
驴Someone's have a example of this but working with a api call?
Hi @boicott, what do you mean by "The store doesn't recognize the route"? Do you have an error or some code you could share so we can reproduce the issue?
Yes, the code is the same as the one in the example. I only changed the if conditional for a call to my api to fill "req.session.authUser" in '/API/index.js' on this block.:
// Add POST - /api/login
router.post('/login', (req, res) => {
axios.post('https://www.xxxxx.com/e/account/login', { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).then((data) => {
req.session.authUser = { username: data.user.username }
return res.json({ username: data.user.username })
})
})
When I run the example it returns an error 401 in the path / api / login
nuxt:render Rendering url /api/login +19s
{ statusCode: 404,
path: '/api/login',
message: 'This page could not be found' }
Maybe is my fault but i cant see how to make this example with a api call to validate user and store his "session"
This should work assuming the endpoint https://www.xxxxx.com/e/account/login actually exists and returns something in data.user.username. Did you change anything else in the original example? Also, make sure your Node version is >= 8.
Yes, the endpoint exists and yes returns the data of the user. I don't change anything else from the example and Node is >= 8
@paulgv I have recreated as much as possible my example using the real api:
DELETED!
@boicott I'm getting a gateway timeout when trying to access your glitch.me :/
@paulgv Edit! :/
Thanks for the repro @boicott !
I pulled the project and had to make some minor changes to make it work:
Here's the code:
// api/index.js
const axios = require('axios')
const express = require('express')
// Create express router
const router = express.Router()
// Transform req & res to have the same API as express
// So we can use res.status() & res.json()
var app = express()
router.use((req, res, next) => {
Object.setPrototypeOf(req, app.request)
Object.setPrototypeOf(res, app.response)
req.res = res
res.req = req
next()
})
// Add POST - /api/login
router.post('/login', (req, res) => {
axios.post('https://www.yourapi.com/e/account/login', { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
.then((data) => {
return res.json({ data: data.data })
})
.catch((error) => {
return res.json({ error: error })
})
})
// Add POST - /api/logout
router.post('/logout', (req, res) => {
delete req.session.authUser
res.json({ ok: true })
})
// Export the server middleware
module.exports = {
path: '/api',
handler: router
}
Give it a try and adapt it so you get actual user data, hope it works for you too!
Yes! THANK YOU! I see what happened. Now, i will try how can i pass my login params to this call!
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
Thanks for the repro @boicott !
I pulled the project and had to make some minor changes to make it work:
Here's the code:
Give it a try and adapt it so you get actual user data, hope it works for you too!