Skip to content

Commit 6423060

Browse files
committed
Initial Commit
0 parents  commit 6423060

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+20043
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Adding Authentication to React Apps

backend/app.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const bodyParser = require('body-parser');
2+
const express = require('express');
3+
4+
const eventRoutes = require('./routes/events');
5+
const authRoutes = require('./routes/auth');
6+
7+
const app = express();
8+
9+
app.use(bodyParser.json());
10+
app.use((req, res, next) => {
11+
res.setHeader('Access-Control-Allow-Origin', '*');
12+
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PATCH,DELETE');
13+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type,Authorization');
14+
next();
15+
});
16+
17+
app.use(authRoutes);
18+
19+
app.use('/events', eventRoutes);
20+
21+
app.use((error, req, res, next) => {
22+
const status = error.status || 500;
23+
const message = error.message || 'Something went wrong.';
24+
res.status(status).json({ message: message });
25+
});
26+
27+
app.listen(8080);

backend/data/event.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const { v4: generateId } = require('uuid');
2+
3+
const { NotFoundError } = require('../util/errors');
4+
const { readData, writeData } = require('./util');
5+
6+
async function getAll() {
7+
const storedData = await readData();
8+
if (!storedData.events) {
9+
throw new NotFoundError('Could not find any events.');
10+
}
11+
return storedData.events;
12+
}
13+
14+
async function get(id) {
15+
const storedData = await readData();
16+
if (!storedData.events || storedData.events.length === 0) {
17+
throw new NotFoundError('Could not find any events.');
18+
}
19+
20+
const event = storedData.events.find((ev) => ev.id === id);
21+
if (!event) {
22+
throw new NotFoundError('Could not find event for id ' + id);
23+
}
24+
25+
return event;
26+
}
27+
28+
async function add(data) {
29+
const storedData = await readData();
30+
storedData.events.unshift({ ...data, id: generateId() });
31+
await writeData(storedData);
32+
}
33+
34+
async function replace(id, data) {
35+
const storedData = await readData();
36+
if (!storedData.events || storedData.events.length === 0) {
37+
throw new NotFoundError('Could not find any events.');
38+
}
39+
40+
const index = storedData.events.findIndex((ev) => ev.id === id);
41+
if (index < 0) {
42+
throw new NotFoundError('Could not find event for id ' + id);
43+
}
44+
45+
storedData.events[index] = { ...data, id };
46+
47+
await writeData(storedData);
48+
}
49+
50+
async function remove(id) {
51+
const storedData = await readData();
52+
const updatedData = storedData.events.filter((ev) => ev.id !== id);
53+
await writeData({ ...storedData, events: updatedData });
54+
}
55+
56+
exports.getAll = getAll;
57+
exports.get = get;
58+
exports.add = add;
59+
exports.replace = replace;
60+
exports.remove = remove;

backend/data/user.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const { hash } = require('bcryptjs');
2+
const { v4: generateId } = require('uuid');
3+
4+
const { NotFoundError } = require('../util/errors');
5+
const { readData, writeData } = require('./util');
6+
7+
async function add(data) {
8+
const storedData = await readData();
9+
const userId = generateId();
10+
const hashedPw = await hash(data.password, 12);
11+
if (!storedData.users) {
12+
storedData.users = [];
13+
}
14+
storedData.users.push({ ...data, password: hashedPw, id: userId });
15+
await writeData(storedData);
16+
return { id: userId, email: data.email };
17+
}
18+
19+
async function get(email) {
20+
const storedData = await readData();
21+
if (!storedData.users || storedData.users.length === 0) {
22+
throw new NotFoundError('Could not find any users.');
23+
}
24+
25+
const user = storedData.users.find((ev) => ev.email === email);
26+
if (!user) {
27+
throw new NotFoundError('Could not find user for email ' + email);
28+
}
29+
30+
return user;
31+
}
32+
33+
exports.add = add;
34+
exports.get = get;

backend/data/util.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const fs = require('node:fs/promises');
2+
3+
async function readData() {
4+
const data = await fs.readFile('events.json', 'utf8');
5+
return JSON.parse(data);
6+
}
7+
8+
async function writeData(data) {
9+
await fs.writeFile('events.json', JSON.stringify(data));
10+
}
11+
12+
exports.readData = readData;
13+
exports.writeData = writeData;

backend/events.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"events": [
3+
{
4+
"title": "A new event",
5+
"image": "https://cdn.muenchen-p.de/fl_progressive,q_65/.imaging/stk/responsive/teaser300/dms/va-2016/muenchner-christkindlmarkt/christkindlmarkt-marienplat-logo-hp/document/christkindlmarkt-marienplat-logo-hp.jpg",
6+
"date": "2022-10-01",
7+
"description": "Some awesome event!",
8+
"id": "2a42fcc4-ea21-4bdd-abf6-c40006dc66a9"
9+
}
10+
],
11+
"users": []
12+
}

0 commit comments

Comments
 (0)