-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathGithubAuthButton.jsx
76 lines (61 loc) · 1.87 KB
/
GithubAuthButton.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
73
74
75
76
import React from 'react';
import PropTypes from 'prop-types';
import api from '@util/federalistApi';
import globals from '../globals';
import { IconGitHub } from './icons';
const apiUrl = globals.APP_HOSTNAME;
const path = '/auth/github2';
const calcWindow = () => ({
width: 800,
height: 800,
left: window.screen.width / 2 - 800 / 2,
top: window.screen.height / 2 - 800 / 2,
});
function authorize(revokeFirst) {
const authPromise = () =>
new Promise((resolve, reject) => {
const url = `${apiUrl}${path}`;
const { width, height, top, left } = calcWindow();
const opts = `
resizable=yes,
scrollbars=yes,
width=${width},
height=${height},
top=${top},
left=${left}
`;
const authWindow = window.open(url, 'authWindow', opts);
const handleMessage = (e) => {
if (e.origin === apiUrl && e.data === 'success') {
authWindow.close();
return resolve(true);
}
return reject(new Error('Authentication failed'));
};
window.addEventListener('message', handleMessage, { once: true });
authWindow.focus();
});
if (revokeFirst) {
return api.revokeApplicationGrant().then(authPromise);
}
return authPromise();
}
const GithubAuthButton = ({ onFailure, onSuccess, text, revokeFirst = false }) => (
<div className="bg-primary-lightest padding-2">
<p className="usa-label margin-top-0">{text}</p>
<button
type="button"
className="usa-button github-auth-button"
onClick={() => authorize(revokeFirst).then(onSuccess).catch(onFailure)}
>
<IconGitHub /> Connect with GitHub
</button>
</div>
);
GithubAuthButton.propTypes = {
onFailure: PropTypes.func.isRequired,
onSuccess: PropTypes.func.isRequired,
revokeFirst: PropTypes.bool,
text: PropTypes.string.isRequired,
};
export default GithubAuthButton;