Let's get authenticated

This commit is contained in:
joshaber 2016-05-26 17:28:16 -04:00
parent 1ef567ab8c
commit f3e97ee9f3
5 changed files with 50 additions and 4 deletions

View file

@ -1,7 +1,10 @@
import * as React from 'react'
import {ipcRenderer} from 'electron'
import ThingList from './thing-list'
import Info from './info'
import {getToken} from './main-process/auth'
const Octokat = require('octokat')
type AppState = {
@ -34,6 +37,16 @@ export default class App extends React.Component<AppProps, AppState> {
public async componentDidMount() {
const zen = await this.octo.zen.read()
console.log('zen', zen)
ipcRenderer.on('did-auth', () => this.didAuthenticate())
}
public componentWillUnmount() {
ipcRenderer.removeListener('did-auth', () => this.didAuthenticate())
}
private didAuthenticate() {
console.log(`authenticated! ${getToken()}`)
}
public render() {

View file

@ -1,4 +1,5 @@
import * as React from 'react'
import {ipcRenderer} from 'electron'
const LOLZ = [
'http://www.reactiongifs.com/r/drkrm.gif',
@ -29,10 +30,18 @@ const ImageStyle = {
export default class Info extends React.Component<InfoProps, void> {
private renderNoSelection() {
return (
<div>No row selected!</div>
<div>
<div>No row selected!</div>
<div>Maybe you'd like to authenticate for fun and Great Victory?</div>
<button onClick={() => this.authenticate()}>Yes pls</button>
</div>
)
}
private authenticate() {
ipcRenderer.send('request-auth')
}
public render() {
const row = this.props.selectedRow
if (row < 0) {

View file

@ -39,7 +39,15 @@ export default class AppWindow {
this.window.on('closed', fn)
}
public didAuthenticate() {
this.send('did-auth', {})
}
private rendererLog(msg: string) {
this.window.webContents.send('log', msg)
this.send('log', msg)
}
private send(channel: string, args: any) {
this.window.webContents.send(channel, args)
}
}

View file

@ -1,5 +1,10 @@
import {shell} from 'electron'
const keytar = require('keytar')
const ServiceName = 'GitHubClient'
const ServiceUserName = 'user'
const ClientID = 'de0e3c7e9973e1c4dd77'
const ClientSecret = '4b35aab1581a32e23af0d930f2a294ae3bb84960'
@ -46,3 +51,11 @@ export function authenticate() {
authState = guid()
shell.openExternal(getOAuthURL(authState))
}
export function getToken(): string {
return keytar.getPassword(ServiceName, ServiceUserName)
}
export function setToken(token: string) {
keytar.addPassword(ServiceName, ServiceUserName, token)
}

View file

@ -3,7 +3,7 @@ import {app, ipcMain} from 'electron'
import AppWindow from './app-window'
import Stats from './stats'
import {requestToken, authenticate} from './auth'
import {requestToken, authenticate, setToken} from './auth'
const stats = new Stats()
@ -22,7 +22,10 @@ app.on('will-finish-launching', () => {
const parsedURL = URL.parse(url, true)
const action = parseURLAction(parsedURL)
if (action === 'oauth') {
requestToken(parsedURL.query.code)
requestToken(parsedURL.query.code).then(token => {
setToken(token)
mainWindow.didAuthenticate()
})
} else {
console.error(`I dunno how to handle this URL: ${url}`)
}