decap-cms-backend-github 2.15.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,151 @@
1
+ import React from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import styled from '@emotion/styled';
4
+ import { NetlifyAuthenticator } from 'decap-cms-lib-auth';
5
+ import { AuthenticationPage, Icon } from 'decap-cms-ui-default';
6
+
7
+ const LoginButtonIcon = styled(Icon)`
8
+ margin-right: 18px;
9
+ `;
10
+
11
+ const ForkApprovalContainer = styled.div`
12
+ display: flex;
13
+ flex-flow: column nowrap;
14
+ justify-content: space-around;
15
+ flex-grow: 0.2;
16
+ `;
17
+ const ForkButtonsContainer = styled.div`
18
+ display: flex;
19
+ flex-flow: column nowrap;
20
+ justify-content: space-around;
21
+ align-items: center;
22
+ `;
23
+
24
+ export default class GitHubAuthenticationPage extends React.Component {
25
+ static propTypes = {
26
+ onLogin: PropTypes.func.isRequired,
27
+ inProgress: PropTypes.bool,
28
+ base_url: PropTypes.string,
29
+ siteId: PropTypes.string,
30
+ authEndpoint: PropTypes.string,
31
+ config: PropTypes.object.isRequired,
32
+ clearHash: PropTypes.func,
33
+ t: PropTypes.func.isRequired,
34
+ };
35
+
36
+ state = {};
37
+
38
+ getPermissionToFork = () => {
39
+ return new Promise((resolve, reject) => {
40
+ this.setState({
41
+ requestingFork: true,
42
+ approveFork: () => {
43
+ this.setState({ requestingFork: false });
44
+ resolve();
45
+ },
46
+ refuseFork: () => {
47
+ this.setState({ requestingFork: false });
48
+ reject();
49
+ },
50
+ });
51
+ });
52
+ };
53
+
54
+ loginWithOpenAuthoring(data) {
55
+ const { backend } = this.props;
56
+
57
+ this.setState({ findingFork: true });
58
+ return backend
59
+ .authenticateWithFork({ userData: data, getPermissionToFork: this.getPermissionToFork })
60
+ .catch(err => {
61
+ this.setState({ findingFork: false });
62
+ console.error(err);
63
+ throw err;
64
+ });
65
+ }
66
+
67
+ handleLogin = e => {
68
+ e.preventDefault();
69
+ const cfg = {
70
+ base_url: this.props.base_url,
71
+ site_id:
72
+ document.location.host.split(':')[0] === 'localhost'
73
+ ? 'cms-demo.netlify.com'
74
+ : this.props.siteId,
75
+ auth_endpoint: this.props.authEndpoint,
76
+ };
77
+ const auth = new NetlifyAuthenticator(cfg);
78
+
79
+ const { open_authoring: openAuthoring = false, auth_scope: authScope = '' } =
80
+ this.props.config.backend;
81
+
82
+ const scope = authScope || (openAuthoring ? 'public_repo' : 'repo');
83
+ auth.authenticate({ provider: 'github', scope }, (err, data) => {
84
+ if (err) {
85
+ this.setState({ loginError: err.toString() });
86
+ return;
87
+ }
88
+ if (openAuthoring) {
89
+ return this.loginWithOpenAuthoring(data).then(() => this.props.onLogin(data));
90
+ }
91
+ this.props.onLogin(data);
92
+ });
93
+ };
94
+
95
+ renderLoginButton = () => {
96
+ const { inProgress, t } = this.props;
97
+ return inProgress || this.state.findingFork ? (
98
+ t('auth.loggingIn')
99
+ ) : (
100
+ <React.Fragment>
101
+ <LoginButtonIcon type="github" />
102
+ {t('auth.loginWithGitHub')}
103
+ </React.Fragment>
104
+ );
105
+ };
106
+
107
+ getAuthenticationPageRenderArgs() {
108
+ const { requestingFork } = this.state;
109
+
110
+ if (requestingFork) {
111
+ const { approveFork, refuseFork } = this.state;
112
+ return {
113
+ renderPageContent: ({ LoginButton, TextButton, showAbortButton }) => (
114
+ <ForkApprovalContainer>
115
+ <p>
116
+ Open Authoring is enabled: we need to use a fork on your github account. (If a fork
117
+ already exists, we&#39;ll use that.)
118
+ </p>
119
+ <ForkButtonsContainer>
120
+ <LoginButton onClick={approveFork}>Fork the repo</LoginButton>
121
+ {showAbortButton && (
122
+ <TextButton onClick={refuseFork}>Don&#39;t fork the repo</TextButton>
123
+ )}
124
+ </ForkButtonsContainer>
125
+ </ForkApprovalContainer>
126
+ ),
127
+ };
128
+ }
129
+
130
+ return {
131
+ renderButtonContent: this.renderLoginButton,
132
+ };
133
+ }
134
+
135
+ render() {
136
+ const { inProgress, config, t } = this.props;
137
+ const { loginError, requestingFork, findingFork } = this.state;
138
+
139
+ return (
140
+ <AuthenticationPage
141
+ onLogin={this.handleLogin}
142
+ loginDisabled={inProgress || findingFork || requestingFork}
143
+ loginErrorMessage={loginError}
144
+ logoUrl={config.logo_url}
145
+ siteUrl={config.site_url}
146
+ {...this.getAuthenticationPageRenderArgs()}
147
+ t={t}
148
+ />
149
+ );
150
+ }
151
+ }