Skip to content

Commit eea454b

Browse files
committed
Attaching Auth Tokens to Outgoing Requests
1 parent 960af5a commit eea454b

File tree

6 files changed

+21
-5
lines changed

6 files changed

+21
-5
lines changed

backend/events.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"events":[{"title":"A new event","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","date":"2022-10-01","description":"Some awesome event!","id":"2a42fcc4-ea21-4bdd-abf6-c40006dc66a9"}],"users":[{"email":"[email protected]","password":"$2a$12$DpO//Ez6vBSVTwqTCDVQpOsrJPEMZ1GphoJMTy8.g7TWHiBP97dQ.","id":"0eb4897f-2f6d-4b30-bd04-9ce763f96697"},{"email":"[email protected]","password":"$2a$12$3m6KUOiNwanEyfkY61rBkeADcW2rW3FSIAd9bS4M5grlHMQkMDV/S","id":"e54095a1-a14c-46d6-b7dc-b316c3a6ccfd"},{"email":"[email protected]","password":"$2a$12$J9lpeDvXxDB3onD8FpMcGu2srQyzCpiMrFPdp/BezkQCSjXCZQlfC","id":"538c7b46-8b5c-482d-a4ab-106cd9e63b75"}]}
1+
{"events":[{"title":"Another event","image":"https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse1.mm.bing.net%2Fth%3Fid%3DOIP.pzsIeXOfUfiGxZUFwJ_VwwHaE4%26pid%3DApi&f=1&ipt=e469e7b3657ed87d395649be2b4cc18e3794727ecebc5f8ddf2f69333c75be69&ipo=images","date":"2024-01-27","description":"Another awesome event!!","id":"b5b592cf-c1cc-4c36-bd00-7670d1494ff6"},{"title":"A new event","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","date":"2022-10-01","description":"Some awesome event!","id":"2a42fcc4-ea21-4bdd-abf6-c40006dc66a9"}],"users":[{"email":"[email protected]","password":"$2a$12$DpO//Ez6vBSVTwqTCDVQpOsrJPEMZ1GphoJMTy8.g7TWHiBP97dQ.","id":"0eb4897f-2f6d-4b30-bd04-9ce763f96697"},{"email":"[email protected]","password":"$2a$12$3m6KUOiNwanEyfkY61rBkeADcW2rW3FSIAd9bS4M5grlHMQkMDV/S","id":"e54095a1-a14c-46d6-b7dc-b316c3a6ccfd"},{"email":"[email protected]","password":"$2a$12$J9lpeDvXxDB3onD8FpMcGu2srQyzCpiMrFPdp/BezkQCSjXCZQlfC","id":"538c7b46-8b5c-482d-a4ab-106cd9e63b75"}]}

frontend/src/components/EventForm.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import {
44
useNavigation,
55
useActionData,
66
json,
7-
redirect
7+
redirect,
88
} from 'react-router-dom';
99

1010
import classes from './EventForm.module.css';
11+
import { getAuthToken } from '../util/auth';
1112

1213
function EventForm({ method, event }) {
1314
const data = useActionData();
@@ -101,10 +102,13 @@ export async function action({ request, params }) {
101102
url = 'http://localhost:8080/events/' + eventId;
102103
}
103104

105+
const token = getAuthToken();
106+
104107
const response = await fetch(url, {
105108
method: method,
106109
headers: {
107110
'Content-Type': 'application/json',
111+
Authorization: 'Bearer ' + token,
108112
},
109113
body: JSON.stringify(eventData),
110114
});
@@ -119,4 +123,3 @@ export async function action({ request, params }) {
119123

120124
return redirect('/events');
121125
}
122-

frontend/src/pages/Authentication.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,10 @@ export async function action({ request }) {
3737
throw json({ message: 'Could not authenticate user.' }, { status: 500 });
3838
}
3939

40+
const resData = await response.json();
41+
const token = resData.token;
42+
43+
localStorage.setItem('token', token);
44+
4045
return redirect('/');
4146
}

frontend/src/pages/Error.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { useRouteError } from 'react-router-dom';
2-
import MainNavigation from '../components/MainNavigation';
32

43
import PageContent from '../components/PageContent';
54

@@ -20,7 +19,6 @@ function ErrorPage() {
2019

2120
return (
2221
<>
23-
<MainNavigation />
2422
<PageContent title={title}>
2523
<p>{message}</p>
2624
</PageContent>

frontend/src/pages/EventDetail.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99

1010
import EventItem from '../components/EventItem';
1111
import EventsList from '../components/EventsList';
12+
import { getAuthToken } from '../util/auth';
1213

1314
function EventDetailPage() {
1415
const { event, events } = useRouteLoaderData('event-detail');
@@ -78,8 +79,13 @@ export async function loader({ request, params }) {
7879

7980
export async function action({ params, request }) {
8081
const eventId = params.eventId;
82+
83+
const token = getAuthToken();
8184
const response = await fetch('http://localhost:8080/events/' + eventId, {
8285
method: request.method,
86+
headers: {
87+
Authorization: 'Bearer ' + token,
88+
},
8389
});
8490

8591
if (!response.ok) {

frontend/src/util/auth.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function getAuthToken() {
2+
const token = localStorage.getItem('token');
3+
return token;
4+
}

0 commit comments

Comments
 (0)