Skip to content

Commit 471d4b3

Browse files
authored
Merge pull request #132 from webdev-20/refactor/auth
Fix auth, add localstorage usage for auth, rename register to signup to be consistent, and match the design
2 parents d3bb52b + 475c405 commit 471d4b3

File tree

23 files changed

+485
-341
lines changed

23 files changed

+485
-341
lines changed

client/cypress/e2e/shorti_app.cy.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ describe('Shorti App', () => {
1111
cy.visit('/');
1212
cy.get('#urlInput').should('be.visible');
1313

14-
// TODO: logout should be visible if logged in ( login register, should not be visible)
14+
// TODO: logout should be visible if logged in ( login signup, should not be visible)
1515
});
1616

1717
it('Login and Sign Up buttons when not logged in', function () {
18-
// TODO: login and register should be visible if not logged in
18+
// TODO: login and signup should be visible if not logged in
1919
cy.contains('Login').should('be.visible');
2020
cy.contains('Sign Up').should('be.visible');
2121
cy.contains('Log Out').should('not.exist');
@@ -46,7 +46,7 @@ describe('Shorti App', () => {
4646
});
4747

4848
describe('authentication', () => {
49-
it('user can register', () => {
49+
it('user can signup', () => {
5050
cy.visit('/');
5151
cy.contains('Sign Up').click();
5252
cy.intercept('POST', '**/api/users/signup', (req) => {
@@ -60,7 +60,7 @@ describe('Shorti App', () => {
6060
cy.get('button[type="submit"]').click();
6161
cy.contains('p', 'Sign up successful.').should('be.visible');
6262

63-
cy.wait('@register')
63+
cy.wait('@signup')
6464
.its('response')
6565
.should('deep.include', {
6666
statusCode: 200,
@@ -72,7 +72,7 @@ describe('Shorti App', () => {
7272
});
7373
});
7474

75-
it('user cannot register with existing username', () => {
75+
it('user cannot signup with existing username', () => {
7676
cy.visit('/');
7777
cy.contains('Sign Up').click();
7878
cy.intercept('POST', '**/api/users/signup', (req) => {
@@ -85,7 +85,7 @@ describe('Shorti App', () => {
8585
cy.get('input[name="confirmPassword"]').type(password);
8686
cy.get('button[type="submit"]').click();
8787

88-
cy.wait('@register')
88+
cy.wait('@signup')
8989
.its('response')
9090
.should('deep.include', {
9191
statusCode: 409,
@@ -99,7 +99,7 @@ describe('Shorti App', () => {
9999
cy.contains('p', 'Account with that email address already exists.').should('be.visible');
100100
});
101101

102-
it('user cannot register with an invalid email', () => {
102+
it('user cannot signup with an invalid email', () => {
103103
cy.visit('/');
104104
cy.contains('Sign Up').click();
105105
cy.intercept('POST', '**/api/users/signup', (req) => {
@@ -112,7 +112,7 @@ describe('Shorti App', () => {
112112
cy.get('input[name="confirmPassword"]').type(password);
113113
cy.get('button[type="submit"]').click();
114114

115-
cy.wait('@register')
115+
cy.wait('@signup')
116116
.its('response')
117117
.should('deep.include', {
118118
statusCode: 400,
@@ -126,7 +126,7 @@ describe('Shorti App', () => {
126126
cy.contains('p', 'Please enter a valid email address.').should('be.visible');
127127
});
128128

129-
it('user cannot register with an invalid password', () => {
129+
it('user cannot signup with an invalid password', () => {
130130
cy.visit('/');
131131
cy.contains('Sign Up').click();
132132

client/src/App.jsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,36 @@ import { Route, Routes } from 'react-router-dom';
22

33
import './App.css';
44
import { LinksProvider } from './context/links';
5-
import HomePage from './routes/HomePage';
65
import RedirectTo from './routes/RedirectTo.jsx';
76
import LandingPage from './routes/LandingPage.jsx';
87
import NotFoundPage from './routes/NotFound';
98
import LoginPage from './routes/LoginPage';
10-
import RegisterPage from './routes/RegisterPage.jsx';
11-
import { AuthProvider } from './context/AuthProvider';
9+
import SignupPage from './routes/SignupPage.jsx';
10+
import { AuthContextProvider } from './context/AuthProvider';
1211
import Layout from './components/Layout/Layout.jsx';
1312
import RequireAuth from './components/Auth/RequireAuth';
1413

1514
function App() {
1615
return (
17-
<AuthProvider>
16+
<AuthContextProvider>
1817
<LinksProvider>
1918
<Routes>
2019
<Route path=":short" element={<RedirectTo />} />
2120
<Route path="/" element={<Layout />}>
2221
{/* public routes */}
2322
<Route path="/" element={<LandingPage />} />
2423
<Route path="login" element={<LoginPage />} />
25-
<Route path="register" element={<RegisterPage />} />
24+
<Route path="signup" element={<SignupPage />} />
2625
{/* private routes */}
2726
<Route element={<RequireAuth />}>
28-
<Route path="home" element={<HomePage />} />
27+
<Route path="home" element={<LandingPage />} />
2928
</Route>
3029
{/* catch all */}
3130
<Route path="*" element={<NotFoundPage />} />
3231
</Route>
3332
</Routes>
3433
</LinksProvider>
35-
</AuthProvider>
34+
</AuthContextProvider>
3635
);
3736
}
3837

client/src/components/Auth/LoginForm.jsx

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import { useState } from 'react';
22
import { login } from '../../services/auth.js';
3-
import useAuth from '../../hooks/useAuth.js';
3+
// import useAuth from '../../hooks/useAuth.js';
44
import { useNavigate, useLocation } from 'react-router-dom';
5+
import { useLogin } from '../../hooks/useLogin.js';
56

67
const LoginForm = () => {
7-
const { setAuth } = useAuth();
8+
// const { setAuth } = useAuth();
89

910
const navigate = useNavigate();
1011
const location = useLocation();
1112
const from = location.state?.from.pathname || '/';
1213

1314
const [state, setState] = useState({
14-
email: '',
15-
password: '',
15+
16+
password: 'password',
1617
});
1718
const [message, setMessage] = useState(null);
1819
//const [success, setSuccess] = useState(false);
20+
const { login, error, isLoading } = useLogin();
1921

2022
const handleChange = (e) => {
2123
setState({
@@ -28,20 +30,6 @@ const LoginForm = () => {
2830
e.preventDefault();
2931
try {
3032
const res = await login(state);
31-
32-
setMessage(res?.message);
33-
if (res.success) {
34-
setAuth({
35-
user: state.email,
36-
password: state.password,
37-
token: res.token,
38-
});
39-
setState({
40-
email: '',
41-
password: '',
42-
});
43-
navigate(from, { replace: true });
44-
}
4533
} catch (e) {
4634
// other errors not catched by services/auth/login
4735
console.error(`Login Error: ${e}`);
@@ -75,12 +63,12 @@ const LoginForm = () => {
7563
required
7664
/>
7765
<br />
78-
<button type="submit" onClick={handleSubmit}>
66+
<button disabled={isLoading} type="submit" onClick={handleSubmit}>
7967
Login
8068
</button>
8169
<br />
8270
</form>
83-
{message && <p>{message}</p>}
71+
{error && <p>{error}</p>}
8472
</>
8573
);
8674
};

client/src/components/Auth/RequireAuth.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { useLocation, Navigate, Outlet } from 'react-router-dom';
2-
import useAuth from '../../hooks/useAuth.js';
2+
// import useAuth from '../../hooks/useAuth.js';
33

44
const RequiredAuth = () => {
5-
const { auth } = useAuth();
5+
// const { auth } = useAuth();
66
const location = useLocation();
77
return auth?.user ? <Outlet /> : <Navigate to="/login" state={{ from: location }} replace />;
88
};

client/src/components/Auth/RegisterForm.jsx renamed to client/src/components/Auth/SignupForm.jsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
import { useState } from 'react';
22
import { Link } from 'react-router-dom';
3-
import { register } from '../../services/auth.js';
3+
import { useSignup } from '../../hooks/useSignup.js';
44

5-
const RegisterForm = () => {
5+
const SignupForm = () => {
66
const [state, setState] = useState({
7-
email: '',
8-
password: '',
9-
confirmPassword: '',
7+
8+
password: 'password',
9+
confirmPassword: 'password',
1010
});
11-
const [message, setMessage] = useState(null);
11+
const { signup, error, isLoading } = useSignup();
12+
const [showError, setShowError] = useState(false);
1213
const [success, setSuccess] = useState(false);
1314

1415
const handleChange = (e) => {
1516
setState({
1617
...state,
1718
[e.target.name]: e.target.value,
1819
});
20+
setShowError(false);
1921
};
2022

2123
const handleSubmit = async (e) => {
2224
e.preventDefault();
23-
const res = await register(state);
25+
setShowError(true);
26+
await signup(state);
27+
2428
// res.success = true/false, which can be used for styling error messages
25-
setMessage(res.message);
26-
setSuccess(res.success);
29+
// setMessage(res.message);
30+
// setSuccess(res.success);
2731
};
2832

2933
return (
@@ -69,13 +73,13 @@ const RegisterForm = () => {
6973
required
7074
/>
7175
<br />
72-
<button type="submit" onClick={handleSubmit}>
73-
Register
76+
<button disabled={isLoading} type="submit" onClick={handleSubmit}>
77+
Sign Up
7478
</button>
7579
<br />
7680
</form>
7781
)}
78-
{message && <p>{message}</p>}
82+
{showError && error && <p>{error}</p>}
7983
{success && (
8084
<div>
8185
<Link to="/login">Login</Link>
@@ -84,4 +88,4 @@ const RegisterForm = () => {
8488
</>
8589
);
8690
};
87-
export default RegisterForm;
91+
export default SignupForm;

client/src/components/Hero/Hero.jsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import classes from './Hero.module.css';
22
import UrlForm from '../UrlForm/UrlForm.jsx';
3-
import useAuth from '../../hooks/useAuth.js';
3+
import { useAuth } from '../../hooks/useAuth.js';
4+
// import useAuth from '../../hooks/useAuth.js';
45

56
function Hero() {
6-
const { auth } = useAuth();
7+
const { user } = useAuth();
78

8-
if (!auth?.user) {
9+
if (!user) {
910
return (
1011
<>
1112
<div className={classes.container}>
@@ -28,13 +29,13 @@ function Hero() {
2829
<UrlForm />
2930
</div>
3031
</>
31-
)
32+
);
3233
} else {
3334
return (
3435
<div className={classes.container}>
3536
<UrlForm />
3637
</div>
37-
)
38+
);
3839
}
3940
}
4041

client/src/components/Layout/Navbar/Navbar.jsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { NavLink } from 'react-router-dom';
2-
import useAuth from '../../../hooks/useAuth.js';
2+
// import useAuth from '../../../hooks/useAuth.js';
33
import classes from './Navbar.module.css';
4+
import { useLogout } from '../../../hooks/useLogout.js';
5+
import { useAuth } from '../../../hooks/useAuth.js';
46

57
function Navbar() {
6-
const { auth, setAuth } = useAuth();
8+
// const { auth, setAuth } = useAuth();
9+
const { user } = useAuth();
10+
const { logout } = useLogout();
711
return (
812
<header className={classes.header}>
913
<nav className={classes.nav}>
@@ -19,10 +23,10 @@ function Navbar() {
1923
<NavLink to="/">Pricing</NavLink>
2024
</li>
2125
</ul> */}
22-
{!auth?.user ? (
23-
<ul className={classes.loginRegister}>
26+
{!user ? (
27+
<ul className={classes.loginSignup}>
2428
<li>
25-
<NavLink to="/register" className={classes.signup}>
29+
<NavLink to="/signup" className={classes.signup}>
2630
Sign Up
2731
</NavLink>
2832
</li>
@@ -38,7 +42,7 @@ function Navbar() {
3842
<NavLink to="/home">MyLinks</NavLink>
3943
*/}
4044
{/*TODO: temporary logout*/}
41-
<NavLink onClick={() => setAuth({})} className={classes.logout}>
45+
<NavLink onClick={() => logout()} className={classes.logout}>
4246
Log Out
4347
</NavLink>
4448
</>

0 commit comments

Comments
 (0)