Skip to content

Commit

Permalink
release 0.0.3 (#5)
Browse files Browse the repository at this point in the history
* feat: authentication (#4)
  • Loading branch information
SchneiderNicolas authored Sep 19, 2023
1 parent 68a999f commit 9841180
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 33 deletions.
1 change: 1 addition & 0 deletions front/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
.env.development.local
.env.test.local
.env.production.local
.env

npm-debug.log*
yarn-debug.log*
Expand Down
4 changes: 2 additions & 2 deletions front/src/components/ProtectedRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { useCookies } from 'react-cookie';

const ProtectedRoute: React.FC = (): JSX.Element => {
const navigate = useNavigate();
const [cookies] = useCookies(['authToken']);
const [cookies] = useCookies(['accessToken']);
const [isAllowed, setIsAllowed] = useState(false);
const [isLoading, setLoading] = useState(true);

useEffect(() => {
async function checkSubscription() {
if (cookies.authToken !== undefined) {
if (cookies.accessToken !== undefined) {
setLoading(false);
setIsAllowed(true);
}
Expand Down
3 changes: 3 additions & 0 deletions front/src/config/config.dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
API_BASE_URL: 'http://localhost:8080',
};
9 changes: 9 additions & 0 deletions front/src/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let config;

if (process.env.NODE_ENV === 'development') {
config = require('./config.dev.js');
} else {
config = require('./config.prod');
}

module.exports = config;
3 changes: 3 additions & 0 deletions front/src/config/config.prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
API_BASE_URL: 'https://api.pwa.nicolas-schneider.fr',
};
33 changes: 27 additions & 6 deletions front/src/pages/SignIn.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,38 @@ import React, { useState } from 'react';
import Button from '../components/Button.tsx';
import Input from '../components/Input.tsx';
import { useCookies } from 'react-cookie';
import { useNavigate } from 'react-router-dom';
import config from '../config/config';

const SignIn = () => {
const [userName, setUserName] = useState('');
const [email, setEmail] = useState('');
const [pass, setPass] = useState('');
const [loginError, setLoginError] = useState(false);
const [, setCookie] = useCookies(['authToken']);
const [, setCookie] = useCookies(['accessToken', 'userName']);
const navigate = useNavigate();

const onSubmit = async () => {
console.log(userName + pass);
setLoginError(true);
setCookie('authToken', 'test', { path: '/' });
const response = await fetch(`${config.API_BASE_URL}/auth/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: email,
password: pass,
}),
});

const data = await response.json();

if (!response.ok) {
setLoginError(data.message);
return;
} else {
setCookie('accessToken', data.accessToken, { path: '/' });
setCookie('userName', data.name, { path: '/' });
navigate('/');
}
};

return (
Expand All @@ -24,7 +45,7 @@ const SignIn = () => {
<Input
placeholder="E-mail"
onChange={(e) => {
setUserName(e.target.value);
setEmail(e.target.value);
}}
id="input_email"
/>
Expand Down
40 changes: 18 additions & 22 deletions front/src/pages/SignUp.jsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,42 @@
import React, { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import Button from '../components/Button.tsx';
import Input from '../components/Input.tsx';
import config from '../config/config';

const SignUpPage = () => {
const [userName, setUserName] = useState('');
const [pass, setPass] = useState('');
const [email, setUserEmail] = useState('');
const [confirmPass, setConfirmPass] = useState('');
const [signUpError, setSignUpError] = useState('');
const navigate = useNavigate();

const onSubmit = async () => {
if (pass.current !== confirmPass.current) {
setSignUpError('Les mots de passe ne correspondent pas.');
if (pass !== confirmPass) {
setSignUpError('Passwords do not match.');
return;
}

console.log(userName + email + pass + confirmPass);
setSignUpError(true);

/*
const response = await fetch(
`${process.env.NEXT_PUBLIC_BASE_URL}/api/user`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: userName.current,
email: email.current,
password: pass.current,
}),
const response = await fetch(`${config.API_BASE_URL}/auth/register`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
);
body: JSON.stringify({
name: userName,
email: email,
password: pass,
}),
});

const data = await response.json();

if (!response.ok) {
setSignUpError(data.message);
return;
} else {
navigate('/signIn');
}
*/
};

return (
Expand Down Expand Up @@ -81,7 +77,7 @@ const SignUpPage = () => {
id="input_confirm_password"
/>
<p
className={`text-red-600 text-center text-sm -mt-2 ${
className={`text-red-600 text-center text-sm -mt-2 capitalize ${
signUpError ? 'animate-shake' : 'invisible'
}`}
>
Expand Down
2 changes: 1 addition & 1 deletion front/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["src"]
"include": ["src", "src/config"]
}
4 changes: 2 additions & 2 deletions server/src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const registerUser = async (req: Request, res: Response) => {
res.status(201).json(result);
} catch (error) {
const e = error as Error;
res.status(500).json({ message: "An error occurred", error: e.message });
res.status(500).json({ message: e.message, error: "An error occurred" });
}
};

Expand Down Expand Up @@ -96,6 +96,6 @@ export const loginUser = async (req: Request, res: Response) => {
res.json({ ...userWithoutSensitiveInfo, accessToken });
} catch (error) {
const e = error as Error;
res.status(500).json({ message: "An error occurred", error: e.message });
res.status(500).json({ message: e.message, error: "An error occurred" });
}
};

0 comments on commit 9841180

Please sign in to comment.