Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending valid error messages from backend during sign in/ signup to the forntend #199

Open
1 of 2 tasks
piotrkosinski137 opened this issue Oct 21, 2020 · 6 comments
Open
1 of 2 tasks
Labels
enhancement New feature or request up-for-grabs up-for-grabs
Projects

Comments

@piotrkosinski137
Copy link

piotrkosinski137 commented Oct 21, 2020

⚠️ Is your feature request related to a problem? Please describe

I've tried to sign in with to short password via UI. I got "An error occurred!" error message.

💡 Describe the solution you'd like

We should create error object which will be passed to the frontend. Error object should contain error message, which will be displayed on UI

🤚 Do you want to develop this feature yourself?

Yes I can 👍

  • Yes
  • No
@jmprathab jmprathab added the enhancement New feature or request label Oct 21, 2020
@jmprathab
Copy link
Owner

Welcome on board. Feel free to raise questions or clarifications if any. Awaiting your PR 😊!

@piotrkosinski137
Copy link
Author

#201

@bayotheman
Copy link

Hi @mslowiak @jmprathab could I take this task?

@Bharath-Ganesh
Copy link

@jmprathab @mslowiak Is it for up-for-grabs? I would love to spend more time on it then

@Ak55915
Copy link

Ak55915 commented Apr 7, 2023

Hi @jmprathab @mslowiak Could i take this issue?

@Ganesh1521
Copy link

Here is Code

Javascript Code for Frontend

<title>Sign Up</title>

Sign Up

Username:
    <label for="password">Password:</label>
    <input type="password" id="password" name="password" required>
    
    <button type="submit">Sign Up</button>
</form>

<div id="error-message" style="color: red;"></div>

<script>
    document.getElementById('signup-form').addEventListener('submit', async (e) => {
        e.preventDefault();
        
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;
        
        const response = await fetch('/signup', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ username, password })
        });
        
        if (response.status === 200) {
            document.getElementById('error-message').innerText = 'Signup successful!';
        } else {
            const data = await response.json();
            document.getElementById('error-message').innerText = data.error;
        }
    });
</script>

Backend (Node.js with Express.js):

const express = require('express');
const app = express();
const bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.post('/signup', (req, res) => {
const { username, password } = req.body;

if (!username || !password) {
return res.status(400).json({ error: 'Username and password are required.' });
}

res.status(200).json({ message: 'Signup successful!' });

});

app.listen(3000, () => {
console.log('Server is running on port 3000');
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request up-for-grabs up-for-grabs
Projects
Features
  
To do
7 participants