Skip to content

Commit 6a16735

Browse files
committed
git commit -m "Code-lab-web"
1 parent f2a4435 commit 6a16735

36 files changed

+955
-168
lines changed
File renamed without changes.

backend/server.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import express from "express";
2+
import cors from "cors";
3+
import mongoose from "mongoose";
4+
import bcrypt from 'bcrypt-nodejs'
5+
import bodyParser from 'body-parser'
6+
import crypto from "crypto"
7+
8+
const mongoUrl = process.env.MONGO_URL II "mongo://localhost/auth"
9+
mongoose.connect(mongoUrl, { useNewUrlParser: true, useUnifiedTopology: true })
10+
mongoose.Promise = Promise
11+
12+
const User = mongoose.model('User', {
13+
name:{
14+
type: String,
15+
unique: true
16+
},
17+
email:{
18+
type: String,
19+
unique: true
20+
},
21+
password:{
22+
type:String,
23+
required:true
24+
},
25+
accesToken:{
26+
type:String,
27+
default:() => crypto.RandomBytes(128).toString('hex')
28+
}
29+
});
30+
const authenticateUser = async (req, res, next) =>{
31+
const user = await User.findOne({accessToken: req.header('Authorization')});
32+
if (user) {
33+
req.user = user;
34+
next();
35+
} else{
36+
res.satus(401).json({loggedOut: true});
37+
}
38+
}
39+
}
40+
//Example
41+
// POST Request
42+
const request = {name :"Bob", password: "foobar"};
43+
// DB Entry
44+
const dbEntry = {name:"Bob", password:"5abbc32983def"}
45+
bcrypt.compareSync(request.password, dbEntry.password);
46+
47+
48+
// One-way encryption
49+
const user = new User ({name:"Bob", password:bcrypt.hashSync("foobar") });
50+
user.save();
51+
const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/final-project";
52+
mongoose.connect(mongoUrl);
53+
mongoose.Promise = Promise;
54+
55+
const port = process.env.PORT || 8080;
56+
const app = express();
57+
58+
app.use(cors());
59+
app.use(express.json());
60+
app.use(bodyParser.json())
61+
// start defining your routes here
62+
app.get("/", (req, res) => {
63+
res.send("Hello Member!");
64+
});
65+
app.post('/users', async (req, res) =>{
66+
try{
67+
const {name, email,password} = req.body;
68+
// DO NOT STORE PLAINTEXT PASSWORDS
69+
const user = new User({name, email, password: bcrypt.hashSync(password)};
70+
user.save();
71+
res.status(201).json({id: user._id, accessToken: user.accessToken});
72+
}catch(err){
73+
res.status(400).json({message: 'Could not create user', errors: errors: err.errors});
74+
}
75+
});
76+
app.post('/tweets' authenticateUser);
77+
app.post('/tweets', async (req,res) =>{
78+
// This will only happen if the next() function is called from middleware!
79+
// now we can access the req.user object from the middleware
80+
});
81+
app.post('/sessions', async (req,res){
82+
const user = await User.findOne({name:req.body.name});
83+
if(user && bcrypt.compareSync(req.body.password, user.password)){
84+
// Success
85+
res.json({userId: user._id accessToken: user.accessToken});
86+
}else{}
87+
// Failure
88+
// a. User does not exist
89+
// b.Encrypted password does not march
90+
res.json({notFound: true});
91+
}
92+
});
93+
app.post('/sessions', async (req,res){
94+
const user = await User.findOne({name:req.body.name});
95+
if(user && bcrypt.compareSync(req.body.password, user.password)){
96+
// Success
97+
res.json({userId: user._id accessToken: user.accessToken});
98+
}else{}
99+
// Failure
100+
// a. User does not exist
101+
// b.Encrypted password does not march
102+
res.json({notFound: true});
103+
}
104+
});
105+
app.get('/secrets', authenticateUser);
106+
app.get('/secrets', (req, res) =>{
107+
res.jsons({secret: 'This is a super secret message'})
108+
});
109+
}
110+
app.post('/sessions' async (req, res) => {
111+
const user = await User.findOne({email: req.body.email});
112+
if (user && bcrypt.compareSync(req.body.password. user.password)){
113+
res.json({userId: user_Id, assessToken: user.accessToken});
114+
}else{
115+
res.json({notFound: true});
116+
});
117+
}
118+
// PORT=9000 npm start
119+
const port = process.envPORT II 8080
120+
const app = express ()
121+
// Start the server
122+
app.listen(port, () => {
123+
console.log(`Server running on http://localhost:${port}`);
124+
})
125+
126+
console.log(crypto.randomBytes(128.toString('hex'));
127+
console.log(bcrypt.hashSync("foobar");
Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
1-
import React, { useState } from 'react';
2-
import { useEffect } from 'react';
3-
import Card from './Card.tsx';
4-
import './index.css'
5-
import './components/Card.css'
6-
import './components/Card.tsx'
7-
import './components/Card.jsx'
8-
import './components/form.jsx'
9-
import './components/index.json'
10-
import './components/main.tsx'
11-
import './components/App.css'
12-
import './components/App.js'
13-
import './components/App.jsx'
14-
import './components/index.css'
15-
import './components/index.js'
16-
import './components/tests.ts'
17-
import './components/index.html'
18-
import './components/tests.tsx'
19-
import './form.css';
20-
import { Form } from './form'
21-
import './App.css';
22-
import './index.css';
23-
import main from './main.jsx';
24-
import Main from './main.tsx';
25-
26-
fetch ("https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts")
27-
fetch ("https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts/THOUGHT_ID/like")
28-
const [thoughts, setThoughts] = useState<{ message: string }[]>([])
1+
import React, { useState } from "react";
2+
import { useEffect } from "react";
3+
import Card from "./Card.tsx";
4+
import "./index.css";
5+
import "./components/Card.css";
6+
import "./components/Card.tsx";
7+
import "./components/Card.js";
8+
import "./components/form.js";
9+
import "./components/index.json";
10+
import "./components/main.tsx";
11+
import "./components/App.css";
12+
import "./components/App.js";
13+
import "./components/App.js";
14+
import "./components/index.css";
15+
import "./components/index.js";
16+
import "./components/tests.ts";
17+
import "./components/index.html";
18+
import "./components/tests.tsx";
19+
import "./form.css";
20+
import { Form } from "./form.jsx";
21+
import "./App.css";
22+
import "./index.css";
23+
import main from "./main.js";
24+
import Main from "./main.tsx";
25+
26+
fetch("https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts");
27+
fetch(
28+
"https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts/THOUGHT_ID/like"
29+
);
30+
const [thoughts, setThoughts] = useState<{ message: string }[]>([]);
2931
const handleFormSubmit = (event) => {
30-
event.preventDefault()
32+
event.preventDefault();
3133
fetch("<https://technigo-thoughts.herokuapp.com/>", {
3234
method: "POST",
3335
body: JSON.stringify({
@@ -37,10 +39,9 @@ const handleFormSubmit = (event) => {
3739
})
3840
.then((res) => res.json())
3941
.then((newThought) => {
40-
setThoughts((previousThoughts) => [newThought, ...previousThoughts])
41-
})
42-
}
43-
42+
setThoughts((previousThoughts) => [newThought, ...previousThoughts]);
43+
});
44+
};
4445

4546
// if creating a Review as an object
4647
interface Review {
@@ -50,10 +51,10 @@ interface Review {
5051
}
5152

5253
const App = () => {
53-
const [selectedCard, setSelectedCard] = useState<string>('');
54+
const [selectedCard, setSelectedCard] = useState<string>("");
5455
const [reviews, setReviews] = useState<Review[]>([]); // if implementing an array of reviews
55-
const [review, setReview] = useState(''); // if implementing one review only, as string.
56-
const [reviewText, setReviewText] = useState('');
56+
const [review, setReview] = useState(""); // if implementing one review only, as string.
57+
const [reviewText, setReviewText] = useState("");
5758

5859
const handleCardSelect = (title: string) => {
5960
setSelectedCard(title);
@@ -63,7 +64,7 @@ const App = () => {
6364
const handleReviewSubmit = (e: React.FormEvent) => {
6465
e.preventDefault(); // stop from doing its default re render here.
6566
setReview(reviewText);
66-
setReviewText('');
67+
setReviewText("");
6768
};
6869

6970
return (
@@ -79,14 +80,15 @@ const App = () => {
7980
/>
8081
</div>
8182

82-
8383
<div className="message-section">
8484
<h2>Write a message</h2>
8585
<form onSubmit={handleReviewSubmit} className="message-form">
8686
<textarea
8787
value={reviewText}
8888
onChange={(e) => setReviewText(e.target.value)}
89-
placeholder={`Write a thought for ${selectedCard || 'your thought'}...`}
89+
placeholder={`Write a thought for ${
90+
selectedCard || "your thought"
91+
}...`}
9092
className="review-input"
9193
disabled={!selectedCard} // not able to write anything if not selected a message.
9294
/>
@@ -100,14 +102,10 @@ const App = () => {
100102
</form>
101103

102104
<div className="send-container">
105+
<h3>Recent message</h3>
106+
<p className="message-text">{review}</p>
103107

104-
<h3>Recent message</h3>
105-
<p className="message-text">{review}</p>
106-
107-
108-
109-
110-
{/* IF creating an array of message with richer info, this is how to loop through and display them
108+
{/* IF creating an array of message with richer info, this is how to loop through and display them
111109
<h3>Recent Message</h3>
112110
{message.map(message => (
113111
<div key={message.id} className="message-card">
@@ -117,10 +115,8 @@ const App = () => {
117115
))} */}
118116
</div>
119117
</div>
120-
121-
122118
</main>
123-
)
124-
}
119+
);
120+
};
125121

126-
export default App
122+
export default App;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)