Click here to Skip to main content
15,867,962 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
here is my html code:


HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="script.js"></script>
    <title>Document</title>
</head>
<body>
    <form action="/login" method="post">
        <input name="username" type="text" placeholder="username">
        <input name="password" type="password" placeholder="password">
        <button type="submit">Login</button>
    </form>
</body>
</html>

and this is my server side code:


JavaScript
app.get('/login', (req, res) => {
    res.sendFile(__dirname + '/login.html')
})

app.post('/login', (req, res) => {
    const { username, password } = req.body
    const foundUser = users.find(u => {
        return u.username === username
    })
    if(foundUser.password !== password) {
        res.send('wrong password')
    } else {
        const accessToken = jwt.sign({ 
            username: foundUser.username, 
            password: foundUser.password
        }, jwtSecret)
        res.json({
            accessToken: accessToken
        })
    }
})
<pre lang="text">so after I make a post request to /login route and log in, my server will respond with some JSON data but I'm not sure how to get or access that data from the client side. should I do it from script.js? if so how should I do it? I would appreciate any help


What I have tried:

i've tried using fetch to fetch json response
Posted
Comments
Member 15627495 15-Oct-22 2:45am    
hello !

are you looking for 'ajax' response ?

in your code , by 'get' / 'post' , you're requesting synchronous response ( with a reload of the page ), this way you have to handle the response in the same page script.
Richard Deeming 17-Oct-22 5:58am    
NB: You are storing your users' passwords in plain text. Don't do that! Secure Password Authentication Explained Simply[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900