Skip to content

Commit

Permalink
Merge pull request #183 from mr-loop-1/feature/signup-errors2
Browse files Browse the repository at this point in the history
Moved user exists error to email field on Signup page
  • Loading branch information
erenfn authored Sep 9, 2024
2 parents fedad32 + 9ed00e4 commit d73f9b2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion backend/src/controllers/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const register = async (req, res) => {
try {
const { name, surname, email, password } = req.body;
const existingUser = await User.findOne({ where: { email } });
if (existingUser) return res.status(400).json({ error: "User already exists" });
if (existingUser) return res.status(400).json({ error: "Email already exists" });

const hashedPassword = await bcrypt.hash(password, 10);
const newUser = await User.create({ name, surname, email, password: hashedPassword });
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/scenes/login/CreateAccountPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ function CreateAccountPage() {
navigate('/');
} catch (error) {
if (error.response && error.response.data) {
if (error.response.data.error === 'User already exists') {
setError('User already exists');
if (error.response.data.error === 'Email already exists') {
setError('Email already exists');
} else {
setError('An error occurred. Please try again.');
}
Expand All @@ -89,7 +89,6 @@ function CreateAccountPage() {
onChange={handleInputChange}
placeholder="Enter your name"
/>
{error && <div className="error-message">{error}</div>}
</div>

<div className="form-group">
Expand Down Expand Up @@ -120,6 +119,7 @@ function CreateAccountPage() {
onChange={handleInputChange}
placeholder="Enter your email"
/>
{error && <div className="error-message">{error}</div>}
</div>

<div className="form-group">
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/tests/scenes/login/CreateAccountPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ describe('CreateAccountPage', () => {
// Add more assertions as needed
});

it('handles sign up failure with user already exists error', async () => {
it('handles sign up failure with email already exists error', async () => {
signUp.mockRejectedValueOnce({
response: {
data: { error: 'User already exists' },
data: { error: 'Email already exists' },
status: 400,
},
});
Expand All @@ -135,7 +135,7 @@ describe('CreateAccountPage', () => {
});

await waitFor(() => {
expect(screen.getByText('User already exists')).to.exist;
expect(screen.getByText('Email already exists')).to.exist;
});

expect(signUp).toHaveBeenCalledWith({ name: 'testname', surname: 'testsurname', email: 'test@example.com', password: 'Password1!' });
Expand Down

0 comments on commit d73f9b2

Please sign in to comment.