forked from NebraskaJS/speaker-signup
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: begin working on authentication * chore: finish authentication (for now) * chore: Switch to querystring because it can be uglified
- Loading branch information
Showing
13 changed files
with
263 additions
and
81 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,2 @@ | ||
CLIENT_ID='fb701c554c046357a5d8' | ||
CLIENT_SECRET='254558185a4ae5a5083f2d76f069fd1572ed11fe' |
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,2 @@ | ||
CLIENT_ID='6cd76d335dc3bf8ae896' | ||
CLIENT_SECRET='a345acffc61ef90ce98b00fc911764e051d9135e' |
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 |
---|---|---|
|
@@ -5,6 +5,5 @@ node_modules | |
yarn-error.log | ||
.env | ||
|
||
|
||
# Build directory | ||
/public |
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
107 changes: 107 additions & 0 deletions
107
src/components/AuthenticationProvider/AuthenticationProvider.js
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,107 @@ | ||
import React, { Component } from 'react'; | ||
import qs from 'querystring'; | ||
import fetch from 'isomorphic-fetch'; | ||
|
||
import { getItem, setItem } from '../../util/local-storage'; | ||
|
||
const AuthenticationContext = React.createContext('authentication'); | ||
|
||
export class AuthenticationProvider extends Component { | ||
static AUTHENTICATION_KEY = '__SPEAKER_SIGNUP_AUTHENTICATION__'; | ||
|
||
state = { | ||
authenticated: false, | ||
token: '', | ||
}; | ||
|
||
componentDidMount() { | ||
const { code } = qs.parse(location.search); | ||
if (code) { | ||
const { | ||
redirectURI, | ||
clientId, | ||
clientSecret, | ||
scope, | ||
} = this.getAuthenticationParams(); | ||
const params = qs.stringify({ | ||
client_id: clientId, | ||
client_secret: clientSecret, | ||
code, | ||
redirect_uri: redirectURI, | ||
}); | ||
/* | ||
* TODO: Fix this is dog shit | ||
*/ | ||
fetch( | ||
`https://cors-anywhere.herokuapp.com/https://github.com/login/oauth/access_token?${params}`, | ||
{ | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json', | ||
}, | ||
} | ||
) | ||
.then(response => response.json()) | ||
.then(json => { | ||
const { access_token } = json; | ||
if (access_token) { | ||
history.replaceState('', '', location.href.replace(/\?.*/, '')); | ||
this.setState( | ||
{ | ||
authenticated: true, | ||
token: access_token, | ||
}, | ||
() => { | ||
setItem(AuthenticationProvider.AUTHENTICATION_KEY, this.state); | ||
} | ||
); | ||
} | ||
}); | ||
} else { | ||
const stored = getItem(AuthenticationProvider.AUTHENTICATION_KEY); | ||
if (stored.token) { | ||
this.setState({ | ||
...stored, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
getAuthenticationParams = () => { | ||
const redirectURI = location.href; | ||
const isLocalHost = redirectURI.includes('localhost'); | ||
const clientId = process.env.CLIENT_ID; | ||
const clientSecret = process.env.CLIENT_SECRET; | ||
const scope = ['repo'].join(' '); | ||
|
||
return { | ||
redirectURI, | ||
clientId, | ||
clientSecret, | ||
scope, | ||
}; | ||
}; | ||
|
||
authenticate = () => { | ||
const { redirectURI, clientId, scope } = this.getAuthenticationParams(); | ||
location.replace( | ||
`https://github.com/login/oauth/authorize?client_id=${clientId}&redirect-uri=${redirectURI}&scope=${scope}` | ||
); | ||
}; | ||
|
||
render() { | ||
const { children } = this.props; | ||
return ( | ||
<AuthenticationContext.Provider | ||
value={{ | ||
...this.state, | ||
authenticate: this.authenticate, | ||
}} | ||
> | ||
{children} | ||
</AuthenticationContext.Provider> | ||
); | ||
} | ||
} | ||
|
||
export const Authentication = AuthenticationContext.Consumer; |
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 @@ | ||
export * from './AuthenticationProvider'; |
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
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
Oops, something went wrong.