Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions cypress/e2e/login.cy.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,38 @@
describe('Login Component', () => {
})
describe("Login Component", () => {
beforeEach(() => {
cy.visit("/LoginForm"); // Change this route according to your setup
});

it("should render the login form with inputs and button", () => {
cy.get("form.login-form").should("exist");
cy.get('input[name="name"]').should("exist");
cy.get('input[name="password"]').should("exist");
cy.get('button[type="submit"]').should("contain", "Login");
});

it("should allow typing into name and password fields", () => {
cy.get('input[name="name"]').type("john");
cy.get('input[name="name"]').should("have.value", "john");

cy.get('input[name="password"]').type("secret123");
cy.get('input[name="password"]').should("have.value", "secret123");
});

it("should submit the form and log-in successfully", () => {
cy.get('input[name="name"]').type("sahil");
cy.get('input[name="password"]').type("12345678");
cy.get('button[type="submit"]').click();

cy.get("#welcome-screen", { timeout: 10000 }).should("be.visible");
});

it("should handle login error gracefully", () => {
cy.get('input[name="name"]').type("wronguser");
cy.get('input[name="password"]').type("wrongpass");
cy.get('button[type="submit"]').click();

cy.get("#login-error", { timeout: 10000 }).should("be.visible");

cy.contains("Invalid username or password").should("be.visible");
});
});
33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
"devDependencies": {
"@testing-library/cypress": "^10.0.3",
"@types/react": "^19.1.4",
"cypress": "^14.3.3",
"start-server-and-test": "^2.0.12"
}
Expand Down
46 changes: 23 additions & 23 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import React, { useState } from 'react';
import './App.css';
import LoginForm from './components/LoginForm';
import Welcome from './components/Welcome';
import React, { useState } from "react";
import "./App.css";
import LoginForm from "./components/LoginForm";
import Welcome from "./components/Welcome";
import {
AuthContext,
AuthProvider,
} from "./providers/AuthProvider/AuthProvider";
import { useUserLogin } from "./hooks/useUserLogin";
import { useContext } from "react";
import { useUserLogout } from "./hooks/useUserLogout";

function App() {
const [isLoggedIn, setIsLoggedIn] = useState(false);
const [userName, setUserName] = useState('');

const handleLogin = (formData) => {
// In a real app, you would validate credentials here
setIsLoggedIn(true);
setUserName(formData.name);
};

const handleLogout = () => {
setIsLoggedIn(false);
setUserName('');
};
const HTMLBody = () => {
const authInfo = useContext(AuthContext);
if (!authInfo.init) return <></>;

return (
<div className="App">
{isLoggedIn ? (
<Welcome userName={userName} onLogout={handleLogout} />
) : (
<LoginForm onLogin={handleLogin} />
)}
{authInfo?.user?.name ? <Welcome /> : <LoginForm />}
</div>
);
};

function App() {
return (
<AuthProvider>
<HTMLBody />
</AuthProvider>
);
}

export default App;
14 changes: 13 additions & 1 deletion src/components/LoginForm.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 400px;
margin: 0 auto;
}

.login-form h2 {
Expand Down Expand Up @@ -55,6 +56,7 @@
font-size: 1rem;
cursor: pointer;
transition: background-color 0.2s;
margin-bottom: 1rem;
}

.login-button:hover {
Expand All @@ -64,4 +66,14 @@
.login-button:focus {
outline: none;
box-shadow: 0 0 0 2px rgba(0, 102, 204, 0.2);
}
}

#login-error {
width: 100%;
padding: 0.75rem 0;
background-color: #dc3545;
color: white;
border: none;
border-radius: 4px;
font-size: 1rem;
}
37 changes: 28 additions & 9 deletions src/components/LoginForm.js → src/components/LoginForm.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
import React, { useState } from 'react';
import './LoginForm.css';
import { useState } from "react";
import "./LoginForm.css";
import { useUserLogin, LOGIN_STATUS } from "../hooks/useUserLogin";

function LoginForm() {
const { Login: onLogin, loginStatus, errorMessage } = useUserLogin();

function LoginForm({ onLogin }) {
const [formData, setFormData] = useState({
name: '',
password: ''
name: "",
password: "",
});

const handleChange = (e) => {
const { name, value } = e.target;
setFormData(prevState => ({

setFormData((prevState) => ({
...prevState,
[name]: value
[name]: value,
}));
};

const handleFormSubmit = (e) => {
e.preventDefault();

// Make Login API call
onLogin(formData.name, formData.password).catch((error) => {
console.error("Something went wrong while logging in");
console.error(error);
});
};

return (
<div className="login-form-container">
<form className="login-form">
<form className="login-form" onSubmit={handleFormSubmit}>
<h2>Login</h2>
<div className="form-group">
<label htmlFor="name">Name:</label>
Expand All @@ -28,6 +42,7 @@ function LoginForm({ onLogin }) {
value={formData.name}
onChange={handleChange}
required
disabled={loginStatus === LOGIN_STATUS.LOADING}
/>
</div>
<div className="form-group">
Expand All @@ -38,15 +53,19 @@ function LoginForm({ onLogin }) {
name="password"
value={formData.password}
onChange={handleChange}
disabled={loginStatus === LOGIN_STATUS.LOADING}
required
/>
</div>
<button type="submit" className="login-button">
Login
</button>
{loginStatus === LOGIN_STATUS.ERROR && (
<div id="login-error">{errorMessage || "Something went wrong"}</div>
)}
</form>
</div>
);
}

export default LoginForm;
export default LoginForm;
18 changes: 0 additions & 18 deletions src/components/Welcome.js

This file was deleted.

24 changes: 24 additions & 0 deletions src/components/Welcome.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from "react";
import "./Welcome.css";
import { AuthContext } from "../providers/AuthProvider/AuthProvider";
import { useContext } from "react";
import { useUserLogout } from "../hooks/useUserLogout";

function Welcome() {
const authInfo = useContext(AuthContext);
const { Logout: onLogout } = useUserLogout();

return (
<div id="welcome-screen" className="welcome-container">
<div className="welcome-card">
<h1>Welcome, {authInfo?.user?.name}!</h1>
<p>You have successfully logged in.</p>
<button onClick={onLogout} className="logout-button">
Logout
</button>
</div>
</div>
);
}

export default Welcome;
1 change: 1 addition & 0 deletions src/constants/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const LOCALSTORAGE_USERNAME_KEY = "username";
38 changes: 38 additions & 0 deletions src/hooks/useUserLogin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useContext } from "react";
import { AuthContext } from "../providers/AuthProvider/AuthProvider";
import { useState } from "react";
import { LOCALSTORAGE_USERNAME_KEY } from "../constants/auth";

export const LOGIN_STATUS = {
LOADING: 1,
ERROR: 2,
SUCCESS: 3,
};

export const useUserLogin = () => {
const authContext = useContext(AuthContext);
const [loginStatus, setLoginStatus] = useState(0);
const [errorMessage, setErrorMessage] = useState("");

/**
* @param name {string}
* @param password {string}
*/
const Login = async (name, password) => {
if (loginStatus === LOGIN_STATUS.LOADING) return;
setLoginStatus(LOGIN_STATUS.LOADING);

// Make API call to Backend server, but since this is a small frontend only assignment.
// i'll just hardcode username & password
if (name !== "sahil" || password !== "12345678") {
setErrorMessage("Invalid username or password");
setLoginStatus(LOGIN_STATUS.ERROR);
} else {
authContext.setUser({ name });
localStorage.setItem(LOCALSTORAGE_USERNAME_KEY, name);
setLoginStatus(LOGIN_STATUS.SUCCESS);
}
};

return { Login, loginStatus, errorMessage };
};
30 changes: 30 additions & 0 deletions src/hooks/useUserLogout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useContext } from "react";
import { AuthContext } from "../providers/AuthProvider/AuthProvider";
import { useState } from "react";
import { LOCALSTORAGE_USERNAME_KEY } from "../constants/auth";

export const LOGOUT_STATUS = {
LOADING: 1,
ERROR: 2,
SUCCESS: 3,
};

export const useUserLogout = () => {
const authContext = useContext(AuthContext);
const [logoutStatus, setLogoutStatus] = useState(0);

const Logout = async () => {
if (logoutStatus === LOGOUT_STATUS.LOADING) return;
setLogoutStatus(LOGOUT_STATUS.LOADING);

// Make API call to Backend server to logout and invalidate the current credentials,
// but since this is a small frontend only assignment.
// i'll just remove the user from Browser's memory
authContext.setUser(undefined);
localStorage.removeItem(LOCALSTORAGE_USERNAME_KEY);

setLogoutStatus(LOGOUT_STATUS.SUCCESS);
};

return { Logout, logoutStatus };
};
Loading