Skip to content

Commit e79d360

Browse files
committed
Add User Id detection to GetSignalRInfo and update web app to verify user is authenticated before connecting to SignalR Service
1 parent a50414a commit e79d360

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

dotnet/ServerlessMicroservices.FunctionApp.Trips/TripFunctions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public static async Task<IActionResult> AssignTripDriver([HttpTrigger(Authorizat
166166
/*** SignalR Info or Negotiate Function ****/
167167
[FunctionName("GetSignalRInfo")]
168168
public static IActionResult GetSignalRInfo([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "signalrinfo")] HttpRequest req,
169-
[SignalRConnectionInfo(HubName = "trips")] SignalRConnectionInfo info,
169+
[SignalRConnectionInfo(HubName = "trips", UserId = "{headers.x-ms-client-principal-id}")] SignalRConnectionInfo info,
170170
ILogger log)
171171
{
172172
log.LogInformation("GetSignalRInfo triggered....");

web/serverless-microservices-web/src/api/trips.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import { checkResponse, post } from '@/utils/http';
1+
import { checkResponse, get, post } from '@/utils/http';
22
//const baseUrl = 'http://localhost:7071/api';
33
const baseUrl = window.apiTripsBaseUrl;
44
const apiKey = window.apiKey;
55

6+
// GET methods
7+
export function getSignalRInfo() {
8+
return get(`${baseUrl}/signalrinfo`, {}, apiKey).then(checkResponse);
9+
}
10+
611
// POST methods
712
export function createTrip(trip) {
813
return post(`${baseUrl}/trips`, trip, apiKey).then(checkResponse);

web/serverless-microservices-web/src/components/SignalRTrips.vue

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,22 @@ export default {
1616
return {};
1717
},
1818
computed: {
19-
...commonGetters(['notificationSystem']),
19+
...commonGetters(['notificationSystem', 'user']),
2020
...tripGetters(['trip', 'currentStep', 'contentLoading'])
2121
},
22+
watch: {
23+
user(val, old) {
24+
if (old === null && val !== null) {
25+
this.connectToSignalR();
26+
}
27+
}
28+
},
2229
methods: {
23-
...tripActions(['setTrip', 'setCurrentStep', 'createTrip']),
24-
getSignalRInfo: async url => {
25-
console.log(`SignalR Info URL ${url}`);
26-
let rawResponse = await fetch(url, {
27-
method: 'GET', // *GET, POST, PUT, DELETE, etc.
28-
mode: 'cors', // no-cors, cors, *same-origin
29-
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
30-
credentials: 'same-origin', // include, same-origin, *omit
31-
headers: {
32-
'Content-Type': 'application/json; charset=utf-8'
33-
},
34-
redirect: 'follow', // manual, *follow, error
35-
referrer: 'no-referrer' // no-referrer, *client
36-
});
30+
...tripActions(['setTrip', 'setCurrentStep', 'createTrip', 'getSignalRInfo']),
31+
async getSignalRInformation() {
32+
let rawResponse = await this.getSignalRInfo(); // {url: '', status: 201};//await this.getSignalRInfo();
3733
if (rawResponse.status === 200) {
38-
let signalRInfo = await rawResponse.json();
34+
let signalRInfo = rawResponse.data;
3935
console.log(`Connection Endpoint: ${signalRInfo.url}`);
4036
return signalRInfo;
4137
} else {
@@ -47,7 +43,8 @@ export default {
4743
}
4844
},
4945
connectToSignalR() {
50-
this.getSignalRInfo(window.signalrInfoUrl)
46+
if (this.user !== null) {
47+
this.getSignalRInformation()
5148
.then(signalrInfo => {
5249
if (signalrInfo !== null && signalrInfo !== undefined) {
5350
let options = {
@@ -144,6 +141,10 @@ export default {
144141
this.notificationSystem.options.error
145142
);
146143
});
144+
}
145+
else {
146+
console.log('Not connecting to SignalR because the user is not authenticated.')
147+
}
147148
}
148149
},
149150
mounted() {

web/serverless-microservices-web/src/store/trips.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createTrip } from '@/api/trips';
1+
import { createTrip, getSignalRInfo } from '@/api/trips';
22

33
export default {
44
namespaced: true,
@@ -49,6 +49,15 @@ export default {
4949
} finally {
5050
commit('contentLoading', false);
5151
}
52+
},
53+
54+
async getSignalRInfo({ commit }) {
55+
try {
56+
let signalRInfo = await getSignalRInfo();
57+
return signalRInfo;
58+
} catch (e) {
59+
throw e;
60+
}
5261
}
5362
}
5463
};

0 commit comments

Comments
 (0)