Parameterize by the endpoint

This commit is contained in:
joshaber 2016-05-31 12:03:13 -04:00
parent 7eb23a4607
commit 1ad5b0af26
3 changed files with 14 additions and 12 deletions

View file

@ -16,9 +16,8 @@ const DefaultHeaders: {[key: string]: string} = {
let authState: string = null
export function requestToken(code: string): Promise<string> {
// TODO: This should take a server URL.
return fetch('https://github.com/login/oauth/access_token', {
export function requestToken(endpoint: string, code: string): Promise<string> {
return fetch(`${endpoint}/login/oauth/access_token`, {
method: 'post',
headers: DefaultHeaders,
body: JSON.stringify({
@ -32,9 +31,8 @@ export function requestToken(code: string): Promise<string> {
.then(response => response.access_token)
}
function getOAuthURL(state: string): string {
// TODO: This should take a server URL.
return 'https://github.com/login/oauth/authorize?client_id=' + ClientID + '&scope=repo&state=' + state
function getOAuthURL(endpoint: string, state: string): string {
return `${endpoint}/login/oauth/authorize?client_id=${ClientID}&scope=repo&state=${state}`
}
function guid(): string {
@ -46,9 +44,13 @@ function guid(): string {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4()
}
export function askUserToAuth() {
export function getDotComEndpoint(): string {
return 'https://github.com'
}
export function askUserToAuth(endpoint: string) {
authState = guid()
shell.openExternal(getOAuthURL(authState))
shell.openExternal(getOAuthURL(endpoint, authState))
}
export function getToken(username: string): string {

View file

@ -4,7 +4,7 @@ import * as ReactDOM from 'react-dom'
import {ipcRenderer} from 'electron'
import App from './app'
import {requestToken} from './auth'
import {requestToken, getDotComEndpoint} from './auth'
import {URLActionType, OAuthAction} from './lib/parse-url'
import UsersStore from './users-store'
import User from './user'
@ -33,7 +33,7 @@ usersStore.loadFromDisk()
ReactDOM.render(<App style={style} usersStore={usersStore}/>, document.getElementById('content'))
async function addUserWithCode(code: string) {
const token = await requestToken(code)
const token = await requestToken(getDotComEndpoint(), code)
const octo = new Octokat({token})
const user = await octo.user.fetch()
usersStore.addUser(new User(user.login, token))

View file

@ -1,13 +1,13 @@
import * as React from 'react'
import {askUserToAuth} from './auth'
import {askUserToAuth, getDotComEndpoint} from './auth'
export default class NotLoggedIn extends React.Component<void, void> {
public render() {
return (
<div>
<div>You don't seem to be logged in.</div>
<button onClick={() => askUserToAuth()}>Log In For Great Glory</button>
<button onClick={() => askUserToAuth(getDotComEndpoint())}>Log In For Great Glory</button>
</div>
)
}