-
Notifications
You must be signed in to change notification settings - Fork 12
/
OIDLoginCallback.jsx
72 lines (65 loc) · 2.25 KB
/
OIDLoginCallback.jsx
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
/*eslint no-process-env: 0*/
import React from 'react';
import alertifyjs from 'alertifyjs';
import { get } from 'lodash';
import {
refreshCurrentUserCache
} from '../../common/utils';
import APIService from '../../services/APIService'
class OIDLoginCallback extends React.Component {
constructor(props) {
super(props)
this.state = {
next: null
}
}
componentDidMount() {
this.exchangeCodeForToken()
}
exchangeCodeForToken = () => {
const queryParams = new URLSearchParams(this.props.location.search)
const code = queryParams.get('code')
const idToken = queryParams.get('id_token')
const next = queryParams.get('next')
if(code) {
/*eslint no-undef: 0*/
this.setState({next: next && next !== '/' ? next : null }, () => {
const redirectURL = this.state.next ? window.location.origin + this.state.next : (window.LOGIN_REDIRECT_URL || process.env.LOGIN_REDIRECT_URL)
const clientSecret = window.OIDC_RP_CLIENT_SECRET || process.env.OIDC_RP_CLIENT_SECRET
const clientId = window.OIDC_RP_CLIENT_ID || process.env.OIDC_RP_CLIENT_ID
APIService.users().appendToUrl('oidc/code-exchange/').post({code: code, redirect_uri: redirectURL, client_id: clientId, client_secret: clientSecret}).then(res => {
if(res.data?.access_token) {
localStorage.removeItem('server_configs')
localStorage.setItem('token', res.data.access_token)
localStorage.setItem('id_token', idToken)
this.cacheUserData()
} else {
alertifyjs.error(res.data)
}
})
})
}
}
cacheUserData() {
refreshCurrentUserCache(response => {
alertifyjs.success(`Successfully signed in`)
if(this.state.next)
window.location.hash = '#' + this.state.next
else {
let returnToURL = response.data.url
if(get(this.props, 'location.search')) {
const queryParams = new URLSearchParams(this.props.location.search)
if(queryParams && queryParams.get('returnTo'))
returnToURL = queryParams.get('returnTo')
}
window.location.hash = '#' + returnToURL
}
})
}
render() {
return (
<div>Signing in...</div>
)
}
}
export default OIDLoginCallback;