-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #313 from nukeop/feature/github-login
Github login
- Loading branch information
Showing
10 changed files
with
406 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { | ||
GITHUB_OAUTH_ACCESS_TOKEN_URL, | ||
GITHUB_API_USER_ENDPOINT | ||
} from '../rest/Github'; | ||
|
||
import { | ||
githubClientId, | ||
githubSecret | ||
} from '../globals'; | ||
|
||
export const GITHUB_OAUTH_CODE_SUCCESS = 'GITHUB_OAUTH_CODE_SUCCESS'; | ||
export const GITHUB_OAUTH_ACCESS_TOKEN_SUCCESS = 'GITHUB_OAUTH_ACCESS_TOKEN_SUCCESS'; | ||
export const GITHUB_LOG_OUT = 'GITHUB_LOG_OUT'; | ||
|
||
export const GITHUB_GET_USER_START = 'GITHUB_GET_USER_START'; | ||
export const GITHUB_GET_USER_SUCCESS = 'GITHUB_GET_USER_SUCCESS'; | ||
export const GITHUB_GET_USER_ERROR = 'GITHUB_GET_USER_ERROR'; | ||
|
||
function githubOauthCodeSuccess(code) { | ||
return { | ||
type: GITHUB_OAUTH_CODE_SUCCESS, | ||
payload: { code } | ||
}; | ||
} | ||
|
||
function githubOauthAccessTokenSuccess(accessToken) { | ||
return { | ||
type: GITHUB_OAUTH_ACCESS_TOKEN_SUCCESS, | ||
payload: { accessToken } | ||
}; | ||
} | ||
|
||
export function githubOauth(code) { | ||
return dispatch => { | ||
dispatch(githubOauthCodeSuccess(code)); | ||
fetch( | ||
'https://cors-anywhere.herokuapp.com/' + | ||
GITHUB_OAUTH_ACCESS_TOKEN_URL + | ||
'?client_id=' + | ||
githubClientId + | ||
'&client_secret=' + | ||
githubSecret + | ||
'&code=' + | ||
code, | ||
{ | ||
method: 'POST', | ||
headers: { 'Accept': 'application/json' } | ||
} | ||
) | ||
.then(response => response.json()) | ||
.then(data => { | ||
dispatch(githubOauthAccessTokenSuccess(data.access_token)); | ||
dispatch(githubGetUser(data.access_token)); | ||
}); | ||
}; | ||
} | ||
|
||
export function githubLogOut() { | ||
return { | ||
type: GITHUB_LOG_OUT | ||
}; | ||
} | ||
|
||
function githubGetUserStart() { | ||
return { | ||
type: GITHUB_GET_USER_START | ||
}; | ||
} | ||
|
||
function githubGetUserSuccess(data) { | ||
return { | ||
type: GITHUB_GET_USER_SUCCESS, | ||
payload: { data } | ||
}; | ||
} | ||
|
||
function githubGetUserError(error) { | ||
return { | ||
type: GITHUB_GET_USER_ERROR, | ||
payload: { error } | ||
}; | ||
} | ||
|
||
export function githubGetUser(token) { | ||
return dispatch => { | ||
dispatch(githubGetUserStart()); | ||
fetch(GITHUB_API_USER_ENDPOINT, { | ||
headers: { 'Authorization': `token ${token}` } | ||
}) | ||
.then(response => response.json()) | ||
.then(data => { | ||
dispatch(githubGetUserSuccess(data)); | ||
}) | ||
.catch(error => { | ||
dispatch(githubGetUserError(error)); | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
class OauthPopup extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
} | ||
|
||
createPopup() { | ||
const { | ||
url, | ||
width, | ||
height, | ||
onCode, | ||
onClose, | ||
onError | ||
} = this.props; | ||
|
||
const left = window.screenX + (window.outerWidth - width) / 2; | ||
const top = window.screenY + (window.outerHeight - height) / 2.5; | ||
this.externalWindow = window.open( | ||
url, | ||
'', | ||
`width=${width},height=${height},left=${left},top=${top}` | ||
); | ||
|
||
this.codeCheck = setInterval(() => { | ||
try { | ||
const params = new URL(this.externalWindow.location).searchParams; | ||
const code = params.get('code'); | ||
if (!code) { | ||
return; | ||
} | ||
clearInterval(this.codeCheck); | ||
onCode(code, params); | ||
this.externalWindow.close(); | ||
} catch (e) { | ||
onError(e); | ||
} | ||
}, 20); | ||
|
||
this.externalWindow.onbeforeunload = () => { | ||
onClose(); | ||
clearInterval(this.codeCheck); | ||
}; | ||
} | ||
|
||
render() { | ||
const { | ||
render | ||
} = this.props; | ||
|
||
return render({ onClick: this.createPopup.bind(this) }); | ||
} | ||
} | ||
|
||
OauthPopup.propTypes = { | ||
url: PropTypes.string, | ||
width: PropTypes.number, | ||
height: PropTypes.number, | ||
render: PropTypes.func, | ||
onCode: PropTypes.func, | ||
onClose: PropTypes.func, | ||
onError: PropTypes.func | ||
}; | ||
|
||
OauthPopup.defaultProps = { | ||
url: '', | ||
width: 500, | ||
height: 500, | ||
render: () => null, | ||
onCode: () => {}, | ||
onClose: () => {}, | ||
onError: () => {} | ||
}; | ||
|
||
export default OauthPopup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import _ from 'lodash'; | ||
import { | ||
Button, | ||
Icon | ||
} from 'semantic-ui-react'; | ||
|
||
import Spacer from '../../Spacer'; | ||
import OauthPopup from '../../OauthPopup'; | ||
import { getGithubOauthUrl } from '../../../rest/Github'; | ||
|
||
import settingsStyles from '../styles.scss'; | ||
|
||
const GithubSettings = props => { | ||
const { | ||
username, | ||
loading, | ||
logIn, | ||
logOut | ||
} = props; | ||
|
||
return ( | ||
<div className={ settingsStyles.settings_item } > | ||
<span>User: <strong>{ _.defaultTo(username, 'Not logged in') }</strong></span> | ||
<Spacer /> | ||
|
||
{ | ||
_.isNil(username) && | ||
<OauthPopup | ||
url={ getGithubOauthUrl() } | ||
onCode={ code => logIn(code) } | ||
render={ oauthProps => | ||
<Button | ||
color='black' | ||
loading={ loading } | ||
onClick={ oauthProps.onClick } | ||
> | ||
<Icon name='github'/> | ||
Log in with Github | ||
</Button> | ||
} /> | ||
} | ||
{ | ||
!_.isNil(username) && | ||
<Button | ||
inverted | ||
onClick={ logOut } | ||
> | ||
Log out | ||
</Button> | ||
} | ||
</div> | ||
); | ||
}; | ||
|
||
GithubSettings.propTypes = { | ||
username: PropTypes.string, | ||
loading: PropTypes.bool, | ||
logIn: PropTypes.func, | ||
logOut: PropTypes.func | ||
}; | ||
|
||
GithubSettings.defaultProps = { | ||
username: null, | ||
loading: false, | ||
logIn: () => {}, | ||
logOut: () => {} | ||
}; | ||
|
||
export default GithubSettings; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,7 +74,7 @@ | |
color: $red; | ||
} | ||
|
||
.lastfm_icon_bg { | ||
.social_icon_bg { | ||
color: $white; | ||
font-size: 24px; | ||
} | ||
|
Oops, something went wrong.