Skip to content

Commit 4a281a0

Browse files
committed
login page has been tried.
1 parent aeb724b commit 4a281a0

File tree

8 files changed

+119
-1
lines changed

8 files changed

+119
-1
lines changed

day-10/bs-store/src/App.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import SimpleSnackbar from "./components/snackBar/SimpleSnackbar";
1313
import {useSelector} from "react-redux";
1414
import UpdateCategory from "./adminpages/categories/UpdateCategory";
1515
import AddBook from "./adminpages/books/AddBook"
16+
import Login from "./pages/login/Login";
1617

1718

1819
function App() {
@@ -31,6 +32,9 @@ function App() {
3132

3233
<Route path='/admin/authors/list' element={<ListAuthor />} />
3334
<Route path='/admin/authors/add' element={<AddAuthor />} />
35+
36+
<Route path='/auth/login' element={<Login />} />
37+
3438
<Route path='/' element={<Home />} />
3539
</Routes>
3640

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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;

day-10/bs-store/src/services/BookService.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ class BookService{
77
}
88

99
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));
1118
}
1219

1320
async getOneBook(id){
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const user = {
2+
userId:0,
3+
userName:'',
4+
firstName:'',
5+
lastName:'',
6+
accessToken:'',
7+
refreshToken:''
8+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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;

day-10/bs-store/src/store/rootReducer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { combineReducers } from "redux";
22
import settingReducer from "./reducers/settingReducer";
33
import categoryReducer from "./reducers/categoryReducer";
44
import bookReducer from "./reducers/bookReducer";
5+
import authReducer from "./reducers/authReducer";
56

67
const rootReducer = combineReducers({
78
setting: settingReducer,
89
category: categoryReducer,
910
book: bookReducer,
11+
auth: authReducer
1012
});
1113

1214
export default rootReducer;

0 commit comments

Comments
 (0)