Skip to content

Commit 4ffa06c

Browse files
author
cory
committed
second commit. adding auth code to files. bug fixes.
1 parent 69318c9 commit 4ffa06c

8 files changed

+182
-125
lines changed

.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["@babel/preset-env"]
3+
}

authMid.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ const generateToken = (user) => {
77

88
// Middleware for verifying JWT
99
const verifyToken = (req, res, next) => {
10-
const token = req.headers.authorization;
10+
const token = req.headers.authorization.split(' ');
1111

1212
if (!token) {
1313
return res.status(401).json({ message: 'No token provided' });
1414
}
1515

16-
jwt.verify(token, 'your_secret_key', (err, decoded) => {
16+
jwt.verify(token[1], 'your_secret_key', (err, decoded) => {
1717
if (err) {
1818
return res.status(401).json({ message: 'Invalid token' });
1919
}

package-lock.json

+6-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"author": "cory_martinez@rocketmail.com",
1111
"license": "MIT",
1212
"dependencies": {
13+
"@babel/core": "^7.24.5",
14+
"@babel/preset-env": "^7.24.5",
1315
"cors": "^2.8.5",
1416
"express": "^4.19.2",
1517
"jsonwebtoken": "^9.0.2",

public/alldata.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function AllData() {
44
React.useEffect(() => {
55
const fetchData = async () => {
66
try {
7-
// Check for JWT token in local storage (assuming you store it there)
7+
// Check for JWT token in local storage
88
const token = localStorage.getItem('token');
99
let headers = {};
1010

@@ -13,7 +13,7 @@ function AllData() {
1313
Authorization: `Bearer ${token}`,
1414
};
1515
}
16-
16+
1717
console.log('Request Headers:', headers);
1818
const response = await fetch('/account/all', { headers });
1919

public/balance.js

+71-54
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,86 @@
1-
function Balance(){
2-
const [show, setShow] = React.useState(true);
3-
const [status, setStatus] = React.useState('');
1+
function Balance() {
2+
const [show, setShow] = React.useState(true);
3+
const [status, setStatus] = React.useState('');
4+
const [balance, setBalance] = React.useState('');
5+
const [email, setEmail] = React.useState('');
6+
7+
async function handle() {
8+
const token = localStorage.getItem('token');
9+
if (!token) {
10+
setStatus('Please login to check balance');
11+
return;
12+
}
13+
14+
try {
15+
const response = await fetch(`/account/findOne/${email}`, {
16+
headers: {
17+
Authorization: `Bearer ${token}`,
18+
},
19+
});
20+
21+
if (!response.ok) {
22+
throw new Error(`API request failed with status: ${response.status}`);
23+
}
24+
25+
const text = await response.text();
26+
const data = JSON.parse(text);
27+
28+
setStatus('');
29+
setShow(false);
30+
setBalance(data.balance);
31+
} catch (err) {
32+
console.error('Error fetching balance:', err);
33+
setStatus('Error fetching balance');
34+
}
35+
}
436

537
return (
638
<Card
739
bgcolor="info"
840
header="Balance"
941
status={status}
10-
body={show ?
11-
<BalanceForm setShow={setShow} setStatus={setStatus}/> :
12-
<BalanceMsg setShow={setShow} setStatus={setStatus}/>}
42+
body={show ? (
43+
<BalanceForm setShow={setShow} setStatus={setStatus} handle={handle} setEmail={setEmail} />
44+
) : (
45+
<BalanceMsg setShow={setShow} setStatus={setStatus} balance={balance} />
46+
)}
1347
/>
14-
)
15-
48+
);
1649
}
1750

18-
function BalanceMsg(props){
19-
return(<>
20-
<h5>Success</h5>
21-
<button type="submit"
22-
className="btn btn-light"
23-
onClick={() => {
24-
props.setShow(true);
25-
props.setStatus('');
26-
}}>
51+
function BalanceMsg(props) {
52+
return (
53+
<>
54+
<h5>Success! Your balance is: ${props.balance}</h5>
55+
<button
56+
type="submit"
57+
className="btn btn-light"
58+
onClick={() => {
59+
props.setShow(true);
60+
props.setStatus('');
61+
}}
62+
>
2763
Check balance again
28-
</button>
29-
</>);
64+
</button>
65+
</>
66+
);
3067
}
3168

32-
function BalanceForm(props){
33-
const [email, setEmail] = React.useState('');
34-
const [balance, setBalance] = React.useState('');
35-
36-
function handle(){
37-
fetch(`/account/findOne/${email}`)
38-
.then(response => response.text())
39-
.then(text => {
40-
try {
41-
const data = JSON.parse(text);
42-
props.setStatus(text);
43-
props.setShow(false);
44-
setBalance(user.balance);
45-
console.log('JSON:', data);
46-
} catch(err) {
47-
props.setStatus(text)
48-
console.log('err:', text);
49-
}
50-
});
51-
}
52-
53-
return (<>
54-
55-
Email<br/>
56-
<input type="input"
57-
className="form-control"
58-
placeholder="Enter email"
59-
value={email}
60-
onChange={e => setEmail(e.currentTarget.value)}/><br/>
69+
function BalanceForm(props) {
70+
return (
71+
<>
72+
Email<br />
73+
<input
74+
type="input"
75+
className="form-control"
76+
placeholder="Enter email"
77+
value={props.email}
78+
onChange={(e) => props.setEmail(e.currentTarget.value)}
79+
/><br />
6180

62-
<button type="submit"
63-
className="btn btn-light"
64-
onClick={handle}>
81+
<button type="submit" className="btn btn-light" onClick={props.handle}>
6582
Check Balance
66-
</button>
67-
68-
</>);
83+
</button>
84+
</>
85+
);
6986
}

public/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ const logout = () => {
66
// Redirect to the login page after 1 second
77
setTimeout(() => {
88
localStorage.removeItem('token');
9-
window.location.href = '/login';
9+
window.location.href = '/#/login/';
1010
}, 1000);
1111
};
1212

1313
function Spa() {
1414
return (
1515
<HashRouter>
1616
<div>
17-
<NavBar/>
17+
<NavBar logout={logout}/>
1818
<UserContext.Provider value={{users:[{name:'abel',email:'abel@mit.edu',password:'secret',balance:100}]}}>
1919
<div className="container" style={{padding: "20px"}}>
2020
<Route path="/" exact component={Home} />

0 commit comments

Comments
 (0)