-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCallback.js
More file actions
89 lines (80 loc) · 3.4 KB
/
Callback.js
File metadata and controls
89 lines (80 loc) · 3.4 KB
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
import React, {useEffect, useState} from 'react';
import {
TokenRequest,
BaseTokenRequestHandler,
GRANT_TYPE_AUTHORIZATION_CODE,
AuthorizationServiceConfiguration,
RedirectRequestHandler,
AuthorizationNotifier,
FetchRequestor, LocalStorageBackend, DefaultCrypto
} from '@openid/appauth';
import environment from "./environment";
import {NoHashQueryStringUtils} from './noHashQueryStringUtils';
export const Callback = (props) => {
const [error, setError] = useState(null);
const [code, setCode] = useState(null);
useEffect(function () {
///////////////////////////
const tokenHandler = new BaseTokenRequestHandler(new FetchRequestor());
const authorizationHandler = new RedirectRequestHandler(new LocalStorageBackend(), new NoHashQueryStringUtils(), window.location, new DefaultCrypto());
const notifier = new AuthorizationNotifier();
authorizationHandler.setAuthorizationNotifier(notifier);
notifier.setAuthorizationListener((request, response, error) => {
console.log('Authorization request complete ', request, response, error);
if (response) {
console.log(`Authorization Code ${response.code}`);
let extras = null;
if (request && request.internal) {
extras = {};
extras.code_verifier = request.internal.code_verifier;
}
const tokenRequest = new TokenRequest({
client_id: environment.clientId,
redirect_uri: environment.redirectURL,
grant_type: GRANT_TYPE_AUTHORIZATION_CODE,
code: response.code,
refresh_token: undefined,
extras
});
AuthorizationServiceConfiguration.fetchFromIssuer(environment.OPServer, new FetchRequestor())
.then((oResponse) => {
const configuration = oResponse;
return tokenHandler.performTokenRequest(configuration, tokenRequest);
})
.then((oResponse) => {
localStorage.setItem('access_token', oResponse.accessToken);
props.history.push('/profile');
})
.catch(oError => {
setError(oError);
});
}
});
//////////////////////////
const params = new URLSearchParams(props.location.search);
setCode(params.get('code'));
if (!code) {
setError('Unable to get authorization code');
return;
}
authorizationHandler.completeAuthorizationRequestIfPossible();
}, [code, props]);
return (
<div className="container-fluid" style={{marginTop: '10px'}}>
<div className="card">
{
code ?
<div className="card-body">
<h5 className="card-title">Loading...</h5>
</div>
:
<div className="card-body bg-danger">
<div className="card-body">
<p className="card-text">{error}</p>
</div>
</div>
}
</div>
</div>
);
}