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
50 changes: 25 additions & 25 deletions client/package-lock.json

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

2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.10.0"
"react-router-dom": "^6.11.1"
},
"devDependencies": {
"@types/react": "^18.0.28",
Expand Down
6 changes: 6 additions & 0 deletions client/src/Components/Login/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import {backendUrl} from "../../constants.js";
const Login = () => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [UserMsg, setUserMsg] = useState('');

return (
<div id="login" className='flex-col'>
<h1>Login</h1>
<p>{UserMsg}</p>
<div className='signup-form'>
<div className='subform'>
<label htmlFor="email">Email: </label>
Expand All @@ -27,13 +29,17 @@ const Login = () => {
<button type="submit" id="test" onClick={async (e) => {
const response = await fetch(`${backendUrl}/login`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
email: email,
password: password
})
});

const json = await response.json();
setUserMsg(json.msg);
localStorage.setItem("token", json.token);
}}>Login</button>
</div>
Expand Down
8 changes: 7 additions & 1 deletion client/src/Components/Signup/Signup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { backendUrl } from "../../constants.js";
const Signup = () => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [UserMsg, setUserMsg] = useState("") ;

return (
<div id='signup' className='flex-col'>
<h1>Signup</h1>
<p>{UserMsg}</p>
<div className='signup-form'>
<div className='subform'>
<label htmlFor='email'>Email: </label>
Expand Down Expand Up @@ -36,16 +38,20 @@ const Signup = () => {
type='submit'
id='test'
onClick={async (e) => {
// change backendUrl to localhost:3000 for development
const response = await fetch(`${backendUrl}/signup`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email: email,
password: password,
}),
});

const json = await response.json();
console.log(json);
setUserMsg(json.msg) ;
}}
>
SIGNUP
Expand Down
41 changes: 32 additions & 9 deletions client/src/Constants/Navbar/Navbar.jsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,49 @@
import React from 'react'
import React, { useEffect , useState } from 'react'
import { Link } from 'react-router-dom'

import './Navbar.css'

const Navbar = () => {
const [Token, setToken] = useState(undefined);
useEffect( () => {
const getToken = localStorage.token;
if (getToken !== undefined){
setToken(getToken) ;
}
}, [])

const clearLocalStorage = () => {
localStorage.clear() ;
setToken(undefined) ;
}
return (
<div id='navbar-main' className='flex-row'>
<Link to={'/'}>
<a href={'/'}>
<div className="logo-box flex-row">
<img className='logo' src="https://user-images.githubusercontent.com/63964149/152531278-5e01909d-0c2e-412a-8acc-4a06863c244d.png" alt="logo" />
<p>PeetCode</p>
</div>
</Link>
</a>
<div className="nav-options">
<Link to={'/problemset/all/'} >Problems</Link>
</div>
<div className="nav-options">
<Link to={'/signup'} >Signup</Link>
</div>
<div className="nav-options">
<Link to={'/login'} >Login</Link>
</div>

{ (Token === undefined)? (
<>
<div className="nav-options">
<Link to={'/signup'} >Signup</Link>
</div>
<div className="nav-options">
<Link to={'/login'} >Login</Link>
</div>
</>
) :
(
<div className="nav-options">
<Link onClick={clearLocalStorage} to={'/signup'} >Logout</Link>
</div>
)}

</div>
)
}
Expand Down
3 changes: 3 additions & 0 deletions client/src/constants.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export const backendUrl = "https://api.peetcode.com";

// For development and testing
// export const backendUrl = "http://localhost:3000";
3 changes: 2 additions & 1 deletion server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ app.post("/login", (req, res) => {
JWT_SECRET
);

return res.json({ token });
return res.json({ token , msg: "Successfully logged in" });

});

app.listen(port, () => {
Expand Down