Skip to content

Commit 1e10316

Browse files
committed
Update auth for header
1 parent 3b91570 commit 1e10316

File tree

12 files changed

+154
-128
lines changed

12 files changed

+154
-128
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ To use this template
2222
```
2323
- Start codinggs
2424
Demo deploy: [reactts-boilerplate.netlify.app](https://reactts-boilerplate.netlify.app/)
25+
2526
Login
27+
```
2628
username: admin
2729
password: 123456
28-
30+
```
2931

3032
## Project structure
3133

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"@types/redux": "^3.6.31",
1818
"@types/redux-thunk": "^2.1.32",
1919
"@types/styled-components": "^5.1.4",
20-
"antd": "^4.8.6",
20+
"antd": "^4.9.2",
2121
"concurrently": "^5.3.0",
2222
"immer": "^8.0.0",
2323
"json-server": "^0.16.3",

src/App/App.reducer.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import * as types from './App.constants';
2-
import { LOGIN_SUCCESS } from 'src/components/Auth/Auth.constants';
2+
import {
3+
LOGIN_SUCCESS,
4+
REGISTER_SUCCESS,
5+
} from 'src/components/Auth/Auth.constants';
36
import produce from 'immer';
47

58
const initialState = {
69
isAuthenticated: false,
7-
closeSideNav: false,
810
};
911

1012
export const AppReducer = (state = initialState, action: { type: any }) =>
@@ -15,11 +17,9 @@ export const AppReducer = (state = initialState, action: { type: any }) =>
1517
draft.isAuthenticated = false;
1618
break;
1719
case LOGIN_SUCCESS:
20+
case REGISTER_SUCCESS:
1821
draft.isAuthenticated = true;
1922
break;
20-
case types.CLOSE_SIDE_NAV:
21-
draft.closeSideNav = !state.closeSideNav;
22-
break;
2323
default:
2424
return state;
2525
}

src/App/App.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
2+
import { loadUser, logout } from 'src/components/Auth/Auth.thunks';
23
import { Routes } from 'src/routes';
34

45
function App() {

src/components/Auth/Auth.reducers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const initialState = {
1515
user: userType,
1616
};
1717

18-
export const loginReducer = (
18+
export const authReducer = (
1919
state = initialState,
2020
action: { type: any; payload: WritableDraft<IUser> },
2121
) =>

src/components/Auth/Login.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { useHistory, Link } from 'react-router-dom';
66
import { login } from './Auth.thunks';
77
import { PATH } from 'src/constants/paths';
88

9-
const mapStateToProps = state => ({
10-
loading: state.loading,
11-
isAuthenticated: state.isAuthenticated,
9+
const mapStateToProps = (state: AppState) => ({
10+
loading: state.auth.loading,
11+
isAuthenticated: state.app.isAuthenticated,
1212
});
1313
const mapDispatchToProps = {
1414
login,
@@ -36,6 +36,7 @@ const _Login = (props: Props) => {
3636
}
3737
}
3838
};
39+
3940
return (
4041
<div className="container">
4142
<div className="login-form-wrap">
@@ -86,13 +87,12 @@ const _Login = (props: Props) => {
8687
type="primary"
8788
htmlType="submit"
8889
className="login-form-button"
89-
loading={loading}
9090
>
9191
Log in
9292
</Button>
9393
<div className="login-form-register-link-wrapper">
9494
Or{' '}
95-
<Link to="/signup" className="login-form-register-link">
95+
<Link to={PATH.REGISTER} className="login-form-register-link">
9696
Register now!
9797
</Link>
9898
</div>

src/components/Auth/Register.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { useHistory, Link } from 'react-router-dom';
66
import { register } from './Auth.thunks';
77
import { PATH } from 'src/constants/paths';
88

9-
const mapStateToProps = state => ({
10-
loading: state.loading,
11-
isAuthenticated: state.isAuthenticated,
9+
const mapStateToProps = (state: AppState) => ({
10+
loading: state.auth.loading,
11+
isAuthenticated: state.app.isAuthenticated,
1212
});
1313
const mapDispatchToProps = {
1414
register,
@@ -33,6 +33,9 @@ const _Register = (props: Props) => {
3333
}
3434
}
3535
};
36+
const onFinishFailed = errorInfo => {
37+
console.log('Failed:', errorInfo);
38+
};
3639
return (
3740
<div className="container">
3841
<div className="login-form-wrap">
@@ -48,6 +51,7 @@ const _Register = (props: Props) => {
4851
className="login-form"
4952
initialValues={{ remember: true }}
5053
onFinish={onFinish}
54+
onFinishFailed={onFinishFailed}
5155
>
5256
<Form.Item
5357
name="username"
@@ -89,7 +93,7 @@ const _Register = (props: Props) => {
8993
</Button>
9094
<div className="login-form-register-link-wrapper">
9195
Or{' '}
92-
<Link to="/login" className="login-form-register-link">
96+
<Link to={PATH.LOGIN} className="login-form-register-link">
9397
Log in now!
9498
</Link>
9599
</div>
Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,58 @@
11
import React from 'react';
22
import { Menu, Grid } from 'antd';
33
import { NavLink } from 'react-router-dom';
4+
import { LoginOutlined } from '@ant-design/icons';
5+
import { logout } from 'src/components/Auth/Auth.thunks';
6+
import { connect, ConnectedProps } from 'react-redux';
7+
import { PATH } from 'src/constants/paths';
48

59
const { useBreakpoint } = Grid;
610

7-
export const RightMenu = () => {
11+
const mapStateToProps = (state: AppState) => ({
12+
loading: state.auth.loading,
13+
isAuthenticated: state.app.isAuthenticated,
14+
});
15+
const mapDispatchToProps = {
16+
logout,
17+
};
18+
19+
const connector = connect(mapStateToProps, mapDispatchToProps);
20+
interface Props extends ConnectedProps<typeof connector> {}
21+
22+
const _RightMenu = (props: Props) => {
23+
const { loading, isAuthenticated, logout } = props;
824
const { md } = useBreakpoint();
9-
return (
25+
const guestLinks = (
1026
<Menu mode={md ? 'horizontal' : 'inline'}>
1127
<Menu.Item key="menukey-login">
12-
<NavLink className="navbar-item primary" to="/login">
28+
<NavLink className="navbar-item primary" to={PATH.LOGIN}>
1329
Sign In
1430
</NavLink>
1531
</Menu.Item>
1632
<Menu.Item key="menukey-signup">
17-
<NavLink className="navbar-item" to="/signup">
33+
<NavLink className="navbar-item" to={PATH.REGISTER}>
1834
Register
1935
</NavLink>
2036
</Menu.Item>
2137
</Menu>
2238
);
39+
const authLinks = (
40+
<Menu mode={md ? 'horizontal' : 'inline'}>
41+
<Menu.Item key="menukey-login">
42+
<NavLink
43+
className="navbar-item primary"
44+
to={PATH.HOME}
45+
onClick={logout}
46+
>
47+
<span>
48+
<LoginOutlined /> Log Out
49+
</span>
50+
</NavLink>
51+
</Menu.Item>
52+
</Menu>
53+
);
54+
return <div>{!loading && isAuthenticated ? authLinks : guestLinks}</div>;
2355
};
56+
57+
const RightMenu = connector(_RightMenu);
58+
export { RightMenu };

src/components/Home/GuestLinks.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Button } from 'antd';
2+
import React from 'react';
3+
import { Link } from 'react-router-dom';
4+
5+
export const GuestLinks = () => {
6+
return (
7+
<div className="homepage">
8+
<div className="home-overlay">
9+
<div className="container homepage-inner">
10+
<div className="home-content">
11+
<h1>React Typescript Template</h1>
12+
<p>Please login with account & password below.</p>
13+
<p className="home-text-light">
14+
Account:
15+
<span> admin</span>
16+
</p>
17+
<p className="home-text-light">
18+
Password: <span> 123456</span>
19+
</p>
20+
<div className="home-button-wrap">
21+
<Button type="primary" size="large">
22+
<Link to="/login" className="button-login-link">
23+
Go To Login Page
24+
</Link>
25+
</Button>
26+
</div>
27+
</div>
28+
</div>
29+
</div>
30+
</div>
31+
);
32+
};

src/components/Home/index.tsx

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
11
import React from 'react';
2-
import { Link } from 'react-router-dom';
2+
import { GuestLinks } from './GuestLinks';
3+
import { connect, ConnectedProps } from 'react-redux';
34
import { Button } from 'antd';
5+
import { Link } from 'react-router-dom';
6+
7+
const mapStateToProps = (state: AppState) => ({
8+
loading: state.auth.loading,
9+
isAuthenticated: state.app.isAuthenticated,
10+
});
11+
const mapDispatchToProps = {};
412

5-
export const Home = () => {
13+
const connector = connect(mapStateToProps, mapDispatchToProps);
14+
interface Props extends ConnectedProps<typeof connector> {}
15+
16+
const _Home = (props: Props) => {
17+
const { loading, isAuthenticated } = props;
618
return (
7-
<div className="homepage">
8-
<div className="home-overlay">
9-
<div className="container homepage-inner">
10-
<div className="home-content">
11-
<h1>React Typescript Template</h1>
12-
<p>Please login with account & password below.</p>
13-
<p className="home-text-light">
14-
Account:
15-
<span> admin</span>
16-
</p>
17-
<p className="home-text-light">
18-
Password: <span> 123456</span>
19-
</p>
20-
<div className="home-button-wrap">
21-
<Button type="primary" size="large">
22-
<Link to="/login" className="button-login-link">
23-
Go To Login Page
24-
</Link>
25-
</Button>
26-
</div>
27-
</div>
28-
</div>
29-
</div>
30-
</div>
19+
<>{!loading && isAuthenticated ? <div>Hi there!</div> : <GuestLinks />}</>
3120
);
3221
};
22+
23+
const Home = connector(_Home);
24+
export { Home };

0 commit comments

Comments
 (0)