this works if the cookie is already set (gets the user's data in the database in another route "/"), the problem is when the cookie is set by the server
Node Server
app.get('/', cors(corsOptions),async ( req,res )=>{
if (req.headers.key) {
let ck = req.headers.key.split('=')
const sendData = await mysql( 'list', ck[1], 'id' )
sendData[0].email = undefined
sendData[0].password = undefined
res.json( sendData[0] )
}else{
res.redirect('/login')
}
})
React Client
export default function Login(){
while (!document.cookie) {
return(
<>
<form method={'POST'} action={'http://jj.me:8080/login'}>
<input type={'email'} name={'login'} placeholder={'login'} required/>
<input type={'password'} name={'password'} placeholder={'password'} required/>
<input type={'submit'} value={'Submit'} />
</form>
</>
)
}
if(document.cookie){
window.location.href = 'http://jj.me:3000'
}
}
[this is the error in the browser when redirect to /login (server)]
ERR_CONNECTION_REFUSED
CodePudding user response:
I set the name "localhost" to "jj.me", I heard that set.cookie() doesn't work with the name localhost
CodePudding user response:
this is the code of post request
app.post('/login', cors(corsOptions),async ( req,res )=>{
const login = req.body.login
const password = req.body.password
const dadosDB = await mysql('list',login,'email')
if (dadosDB.lenght > 0) {
const passwd = await bcrypt.compare( password, dadosDB[0].password )
if (dadosDB[0].email == login && dadosDB[0].password == passwd) {
cookie = dadosDB[0].id.split('=')
let options = {
path:'*/*',
domain:'jj.me',
httpOnly: true,
maxAge: (1000 * 60 * 60 * 24)
}
res.cookie('key',cookie[1],options)
//res.redirect('/')
}
}
})
CodePudding user response:
I modified the corsOptions, now it accesses the database, (it showed in the console) but it loads infinitely and doesn't set the cookie