-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessionManagementAPISample.tsx
123 lines (109 loc) · 4.3 KB
/
sessionManagementAPISample.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import React, { useEffect, useState } from 'react';
import { useAuthContext, HttpRequestConfig } from "@asgardeo/auth-react";
import * as authConfig from "../config.json"
import ReactJson from "react-json-view";
import { useHistory } from "react-router-dom";
export const SessionManagementAPISection = (props: any) => {
const { state, httpRequest } = useAuthContext();
const [sessionID, setSessionId] = useState<string>("");
const [sessionsInfo, setSessionsInfo] = useState<any>({});
const history = useHistory();
useEffect(() => {
if (state?.isAuthenticated) {
getAllSessions();
}
}, [state.isAuthenticated]);
function getAllSessions() {
const requestConfig: HttpRequestConfig = {
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
attachToken: true,
method: "GET",
url: authConfig.serverOrigin+"/api/users/v1/me/sessions"
};
httpRequest(requestConfig).then((response: any) => {
setSessionsInfo(response.data);
}).catch((error) => {
console.log("request error: " + error);
history.push("/networkError");
})
}
function terminateAllSessions() {
const requestConfig: HttpRequestConfig = {
method: "DELETE",
url: authConfig.serverOrigin+"/api/users/v1/me/sessions"
};
httpRequest(requestConfig).then((response: any) => {
if (response.status === 204){
getAllSessions();
setSessionId("");
alert("All authenticated sessions terminated successfully!");
}
}).catch((error) => {
console.log("request error: " + error);
history.push("/networkError");
})
}
function terminateSpecificSession(sessionID: string) {
const requestConfig: HttpRequestConfig = {
method: "DELETE",
url: authConfig.serverOrigin+"/api/users/v1/me/sessions/" + sessionID
};
httpRequest(requestConfig).then((response: any) => {
if (response.status === 204){
setSessionId("");
getAllSessions();
alert("session " + sessionID + " is successfully terminated!");
}
}).catch((error) => {
console.log("request error: " + error);
history.push("/networkError");
})
}
const handleTerminateAll = (event: any) => {
event.preventDefault();
terminateAllSessions();
}
const handleTerminateSession = (event: any) => {
event.preventDefault();
terminateSpecificSession(sessionID);
}
return (
<div id='features' className='text-center'>
<div className='container'>
<div className='col-md-10 col-md-offset-1 section-title'>
<h4>Session Management API</h4>
</div>
<div className='row row-center'>
<form id="delete-specific-session-form" onSubmit={handleTerminateSession}>
<label>
Enter Session ID:
<input
onChange={(e) => setSessionId(e.target.value)}
value={sessionID} type="text" name="sessionID" />
</label>
<input type="submit" value="Terminate Session" />
</form>
<br />
<form onSubmit={handleTerminateAll}>
<input type="submit" value="Terminate All Sessions" />
</form>
<div className='col-xs-12 col-md-12 json'>
{' '}
<ReactJson
src={sessionsInfo}
name={null}
enableClipboard={false}
displayObjectSize={false}
displayDataTypes={false}
iconStyle="square"
theme="monokai"
/>
</div>
</div>
</div>
</div>
)
}