Skip to content

Commit 0968751

Browse files
committed
Implementing the Auth Action
1 parent e79ff57 commit 0968751

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

backend/events.json

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1 @@
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-
}
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"}]}

frontend/src/App.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import NewEventPage from './pages/NewEvent';
1313
import RootLayout from './pages/Root';
1414
import { action as manipulateEventAction } from './components/EventForm';
1515
import NewsletterPage, { action as newsletterAction } from './pages/Newsletter';
16-
import AuthenticationPage from './pages/Authentication';
16+
import AuthenticationPage, {
17+
action as authAction,
18+
} from './pages/Authentication';
1719

1820
const router = createBrowserRouter([
1921
{
@@ -25,6 +27,7 @@ const router = createBrowserRouter([
2527
{
2628
path: 'auth',
2729
element: <AuthenticationPage />,
30+
action: authAction,
2831
},
2932
{
3033
path: 'events',

frontend/src/pages/Authentication.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,41 @@
1+
import { json, redirect } from 'react-router-dom';
12
import AuthForm from '../components/AuthForm';
23

34
function AuthenticationPage() {
45
return <AuthForm />;
56
}
67

7-
export default AuthenticationPage;
8+
export default AuthenticationPage;
9+
10+
export async function action({ request }) {
11+
const searchParams = new URL(request.url).searchParams;
12+
const mode = searchParams.get('mode') || 'login';
13+
14+
if (mode !== 'login' && mode !== 'signup') {
15+
throw json({ message: 'Unsupported mode.' }, { status: 422 });
16+
}
17+
18+
const data = await request.formData();
19+
const authData = {
20+
email: data.get('email'),
21+
password: data.get('password'),
22+
};
23+
24+
const response = await fetch('http://localhost:8080/' + mode, {
25+
method: 'POST',
26+
headers: {
27+
'Content-Type': 'application/json',
28+
},
29+
body: JSON.stringify(authData),
30+
});
31+
32+
if (response.status === 422 || response.status === 401) {
33+
return response;
34+
}
35+
36+
if (!response.ok) {
37+
throw json({ message: 'Could not authenticate user.' }, { status: 500 });
38+
}
39+
40+
return redirect('/');
41+
}

0 commit comments

Comments
 (0)