File tree Expand file tree Collapse file tree 8 files changed +119
-1
lines changed Expand file tree Collapse file tree 8 files changed +119
-1
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import SimpleSnackbar from "./components/snackBar/SimpleSnackbar";
13
13
import { useSelector } from "react-redux" ;
14
14
import UpdateCategory from "./adminpages/categories/UpdateCategory" ;
15
15
import AddBook from "./adminpages/books/AddBook"
16
+ import Login from "./pages/login/Login" ;
16
17
17
18
18
19
function App ( ) {
@@ -31,6 +32,9 @@ function App() {
31
32
32
33
< Route path = '/admin/authors/list' element = { < ListAuthor /> } />
33
34
< Route path = '/admin/authors/add' element = { < AddAuthor /> } />
35
+
36
+ < Route path = '/auth/login' element = { < Login /> } />
37
+
34
38
< Route path = '/' element = { < Home /> } />
35
39
</ Routes >
36
40
Original file line number Diff line number Diff line change
1
+ import React from "react" ;
2
+ import { useFormik } from "formik" ;
3
+ import { useDispatch , useSelector } from "react-redux" ;
4
+ import { login } from "../../store/actions/authAction" ;
5
+ import AuthService from "../../services/AuthService" ;
6
+ export default function Login ( ) {
7
+
8
+ const { user} = useSelector ( state => state . auth )
9
+ const authDispatch = useDispatch ( ) ;
10
+
11
+
12
+
13
+ const { handleSubmit, handleChange, values } = useFormik ( {
14
+ initialValues : {
15
+ userName : "" ,
16
+ password : "" ,
17
+ } ,
18
+ onSubmit : async ( values ) => {
19
+ console . log ( values ) ;
20
+ const authService = new AuthService ( ) ;
21
+ const resp = await authService . login ( values ) ;
22
+ console . log ( resp ) ;
23
+ if ( resp . status === 200 ) {
24
+ authDispatch ( login ( resp ) ) ;
25
+ }
26
+ } ,
27
+ } ) ;
28
+
29
+
30
+ return (
31
+ < div >
32
+ < h1 > Login</ h1 >
33
+ < form onSubmit = { handleSubmit } >
34
+ < input id = 'userName' name = 'userName' type = 'text' onChange = { handleChange } />
35
+ < input id = 'password' name = 'password' type = 'password' onChange = { handleChange } />
36
+ < input type = 'submit' value = 'login' > </ input >
37
+ </ form >
38
+ { JSON . stringify ( values ) }
39
+ </ div >
40
+ ) ;
41
+ }
Original file line number Diff line number Diff line change
1
+ import axios from "axios" ;
2
+
3
+ class AuthService {
4
+ constructor ( ) {
5
+ this . baseUrl = `${ process . env . REACT_APP_BASE_ENDPOINT } /auth` ;
6
+ }
7
+
8
+ async login ( login ) {
9
+ const url = `${ this . baseUrl } /login`
10
+ console . log ( url )
11
+ return await axios . post ( url , login ) . then ( ( resp ) => resp ) ;
12
+ }
13
+ }
14
+
15
+ export default AuthService ;
Original file line number Diff line number Diff line change @@ -7,7 +7,14 @@ class BookService{
7
7
}
8
8
9
9
async getAllBooks ( ) {
10
- return await axios . get ( this . baseUrl ) . then ( resp => resp . data ) ;
10
+ return await axios
11
+ . get ( this . baseUrl , {
12
+ headers :{
13
+ 'Authorization' : 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImF1dGhvcml0aWVzIjpbeyJhdXRob3JpdHkiOiJib29rOmRlbGV0ZSJ9LHsiYXV0aG9yaXR5IjoiYm9vazpwb3N0In0seyJhdXRob3JpdHkiOiJib29rOmdldCJ9LHsiYXV0aG9yaXR5IjoiUk9MRV9BRE1JTiJ9LHsiYXV0aG9yaXR5IjoiYm9vazpwdXQifV0sImlhdCI6MTY1OTQ2NTA4MiwiZXhwIjoxNjU5NDY4NjgyfQ.VED0vLYOAJXu3IEVlA6RFN2aOw3vPUB655yaLptSgIXeyWkX9Sfg23X2CoJVgnQ-xpdMjdOO-9mwGy7yn4NIrg'
14
+ }
15
+ } )
16
+ . then ( resp => resp . data )
17
+ . catch ( err => console . log ( err . status ) ) ;
11
18
}
12
19
13
20
async getOneBook ( id ) {
Original file line number Diff line number Diff line change
1
+ import AuthService from "../../services/AuthService" ;
2
+
3
+ export const LOGIN = "LOGIN" ;
4
+
5
+ const authService = new AuthService ( ) ;
6
+
7
+ export function login ( login ) {
8
+ return function ( dispatch ) {
9
+ authService
10
+ . login ( login )
11
+ . then ( ( resp ) => resp . data )
12
+ . then ( ( resp ) => dispatch ( { type : LOGIN , payload : resp } ) ) ;
13
+ } ;
14
+ }
Original file line number Diff line number Diff line change
1
+ export const user = {
2
+ userId :0 ,
3
+ userName :'' ,
4
+ firstName :'' ,
5
+ lastName :'' ,
6
+ accessToken :'' ,
7
+ refreshToken :''
8
+ }
Original file line number Diff line number Diff line change
1
+ import { LOGIN } from "../actions/authAction" ;
2
+ import { user } from "../initialValues/authItems" ;
3
+ const initialValues = {
4
+ user
5
+ }
6
+
7
+ function authReducer ( state = initialValues , { type, payload} ) {
8
+ switch ( type ) {
9
+ case LOGIN :
10
+ return {
11
+ ...state ,
12
+ user : {
13
+ userId : payload . userId ,
14
+ userName :payload . userName ,
15
+ firstName :payload . firstName ,
16
+ lastName :payload . lastName ,
17
+ accessToken :payload . accessToken ,
18
+ refreshToken :payload . refreshToken
19
+ }
20
+ }
21
+ default :
22
+ return {
23
+ ...state
24
+ } ;
25
+ }
26
+ }
27
+ export default authReducer ;
Original file line number Diff line number Diff line change @@ -2,11 +2,13 @@ import { combineReducers } from "redux";
2
2
import settingReducer from "./reducers/settingReducer" ;
3
3
import categoryReducer from "./reducers/categoryReducer" ;
4
4
import bookReducer from "./reducers/bookReducer" ;
5
+ import authReducer from "./reducers/authReducer" ;
5
6
6
7
const rootReducer = combineReducers ( {
7
8
setting : settingReducer ,
8
9
category : categoryReducer ,
9
10
book : bookReducer ,
11
+ auth : authReducer
10
12
} ) ;
11
13
12
14
export default rootReducer ;
You can’t perform that action at this time.
0 commit comments