Skip to content

Commit 3f7d0d9

Browse files
authored
Merge pull request #3 from foyzulkarim/feature/secured-request
Add attribute in API and Update readme
2 parents fd8c88e + 71910f9 commit 3f7d0d9

File tree

8 files changed

+123
-11
lines changed

8 files changed

+123
-11
lines changed

README.md

+75-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,75 @@
1-
# react-redux-saga-jwt-auth-aspnet
1+
# Welcome to React Redux JWT Authentication using ASP.NET Core API !
2+
3+
## Technology used
4+
5+
This repository uses a number of frameworks and libraries to work:
6+
7+
* [ReactJS] - A JavaScript library for building user interfaces
8+
* [ASP.NET Core API] - Build secure REST APIs on any platform with C#
9+
* [SQL Server] - SQL Server 2019 Express is a free edition of SQL Server
10+
* [MongoDB] - The database for
11+
modern applications
12+
13+
14+
## Installation and Run
15+
16+
Install the dependencies and devDependencies and start the server.
17+
18+
To run Auth server
19+
20+
```sh
21+
$ cd .\server\AuthWebApplication\AuthWebApplication\
22+
$ dotnet restore
23+
$ dotnet run
24+
```
25+
Verify the deployment by navigating to your server address in your preferred browser.
26+
27+
```sh
28+
http://localhost:5000/
29+
```
30+
31+
To run Resource server
32+
33+
```sh
34+
$ cd .\server\WebApplication2\WebApplication2
35+
$ dotnet restore
36+
$ dotnet watch run
37+
```
38+
Verify the deployment by navigating to your server address in your preferred browser.
39+
40+
```sh
41+
http://localhost:5005/
42+
```
43+
44+
To run client
45+
46+
```sh
47+
$ cd .\client
48+
$ npm install
49+
$ npm start
50+
```
51+
Verify the deployment by navigating to your server address in your preferred browser.
52+
53+
```sh
54+
http://localhost:3000/
55+
```
56+
57+
58+
59+
60+
### Todos
61+
- Write tests
62+
- Add nodejs resource server
63+
64+
License
65+
----
66+
67+
MIT
68+
69+
[node.js]: <http://nodejs.org>
70+
[express]: <http://expressjs.com>
71+
[ReactJS]: <https://reactjs.org/>
72+
[Gulp]: <http://gulpjs.com>
73+
[ASP.NET Core API]:<https://dotnet.microsoft.com/apps/aspnet/apis>
74+
[SQL Server]:<https://www.microsoft.com/en-us/sql-server/sql-server-downloads>
75+
[MongoDB]:<https://www.mongodb.com/>

client/src/reducers/userReducer.js

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export default (state = initialState, action) => {
2626
};
2727
default:
2828
let localStorageData = localStorage.getItem('data');
29-
console.log('localStorageData', localStorageData);
3029
if (localStorageData) {
3130
localStorageData = JSON.parse(localStorageData);
3231
return {

client/src/sagas/api.js

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
import axios from 'axios';
2-
// const BaseUrl = 'http://localhost:61361/api';
3-
const BaseUrl = 'http://feli-api.azurewebsites.net/api';
4-
const AuthUrl = 'http://localhost:5000'
2+
3+
const BaseUrl = 'http://localhost:5005/api';
4+
const AuthUrl = 'http://localhost:5000';
5+
6+
7+
axios.interceptors.request.use(function (config) {
8+
let localStorageData = localStorage.getItem('data');
9+
if (localStorageData) {
10+
localStorageData = JSON.parse(localStorageData);
11+
let token = 'Bearer ' + localStorageData.access_token;
12+
config.headers.Authorization = token;
13+
}
14+
console.log(config);
15+
return config;
16+
});
17+
18+
19+
axios.interceptors.response.use(function (response) {
20+
return response;
21+
}, function (error) {
22+
if (error.response.status === 401) {
23+
localStorage.removeItem('data');
24+
window.location = '/login';
25+
} else {
26+
return Promise.reject(error);
27+
}
28+
});
529

630
export const getPosts = () => {
731
console.log("getPosts api call.");
32+
console.log(axios.defaults);
833
return axios.get(`${BaseUrl}/posts`);
934
}
1035

client/src/sagas/sagas.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function* addPost({ payload }) {
77
yield put({ type: 'ADD_POST_SUCCESS', payload: output });
88
yield put({ type: 'FETCH_POSTS' });
99
} catch (error) {
10-
console.log('fetch posts error', error);
10+
console.log('addPost error', error);
1111
}
1212
}
1313

@@ -21,7 +21,7 @@ export function* editPost({ payload }) {
2121
yield put({ type: 'EDIT_POST_SUCCESS', payload: output });
2222
yield put({ type: 'FETCH_POSTS' });
2323
} catch (error) {
24-
console.log('fetch posts error', error);
24+
console.log('editPost error', error);
2525
}
2626
}
2727

@@ -35,7 +35,7 @@ export function* deletePost({ payload }) {
3535
yield put({ type: 'DELETE_POST_SUCCESS', payload: output });
3636
yield put({ type: 'FETCH_POSTS' });
3737
} catch (error) {
38-
console.log('fetch posts error', error);
38+
console.log('deletePost error', error);
3939
}
4040
}
4141

server/WebApplication2/WebApplication2/Controllers/CommentsController.cs

+2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
using Microsoft.AspNetCore.Mvc;
77
using WebApplication2.Models;
88
using WebApplication2.Services;
9+
using Microsoft.AspNetCore.Authorization;
910

1011
namespace WebApplication2.Controllers
1112
{
13+
[Authorize]
1214
[Route("api/[controller]")]
1315
[ApiController]
1416
public class CommentsController : ControllerBase

server/WebApplication2/WebApplication2/Controllers/PostsController.cs

+2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
using Microsoft.AspNetCore.Mvc;
88
using WebApplication2.Models;
99
using WebApplication2.Services;
10+
using Microsoft.AspNetCore.Authorization;
1011

1112
namespace WebApplication2.Controllers
1213
{
14+
[Authorize]
1315
[EnableCors("all")]
1416
[Route("api/[controller]")]
1517
[ApiController]

server/WebApplication2/WebApplication2/Properties/launchSettings.json

+12-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,20 @@
2121
"commandName": "Project",
2222
"launchBrowser": true,
2323
"launchUrl": "weatherforecast",
24-
"applicationUrl": "http://localhost:5000",
24+
"applicationUrl": "http://localhost:5005",
25+
"environmentVariables": {
26+
"ASPNETCORE_ENVIRONMENT": "Development"
27+
}
28+
},
29+
"Watch": {
30+
"commandName": "Watch",
31+
"executablePath": "C:\\Program Files\\dotnet\\dotnet.exe",
32+
"commandLineArgs": "watch run",
33+
"launchBrowser": true,
34+
"launchUrl": "http://localhost:5005",
2535
"environmentVariables": {
2636
"ASPNETCORE_ENVIRONMENT": "Development"
2737
}
2838
}
2939
}
30-
}
40+
}

server/WebApplication2/WebApplication2/appsettings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"BookstoreDatabaseSettings": {
33
"BooksCollectionName": "Posts",
4-
"ConnectionString": "mongodb+srv://mongoadmin:Pa552123@cluster0-mz4lo.azure.mongodb.net/test",
4+
"ConnectionString": "mongodb://localhost:27017/test",
55
"DatabaseName": "node_boilerplate"
66
},
77
"Logging": {

0 commit comments

Comments
 (0)