diff --git a/src/index.tsx b/src/index.tsx index 83b51fff3e..8e71d92d54 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -36,5 +36,5 @@ async function addUserWithCode(code: string) { const token = await requestToken(code) const octo = new Octokat({token}) const user = await octo.user.fetch() - usersStore.addUser(new User(user, token)) + usersStore.addUser(new User(user.login, token)) } diff --git a/src/main-process/main.ts b/src/main-process/main.ts index f402cfaf0c..d32d879e7a 100644 --- a/src/main-process/main.ts +++ b/src/main-process/main.ts @@ -1,10 +1,9 @@ -import {app, ipcMain, Menu} from 'electron' +import {app, Menu} from 'electron' import AppWindow from './app-window' import Stats from './stats' -import {authenticate} from '../auth' import {buildDefaultMenu} from './menu' -import parseURL from './parse-url' +import parseURL from '../lib/parse-url' const stats = new Stats() @@ -22,7 +21,6 @@ app.on('ready', () => { stats.readyTime = Date.now() app.setAsDefaultProtocolClient('x-github-client') - ipcMain.on('request-auth', authenticate) createWindow() diff --git a/src/user.ts b/src/user.ts index eb54e0ccf0..491a46cba3 100644 --- a/src/user.ts +++ b/src/user.ts @@ -1,9 +1,10 @@ export default class User { public token: string public login: string + public server: string - public constructor(user: any, token: string) { - this.login = user.login + public constructor(login: string, token: string) { + this.login = login this.token = token } } diff --git a/src/users-store.ts b/src/users-store.ts index a761c29d3c..f85c26eef7 100644 --- a/src/users-store.ts +++ b/src/users-store.ts @@ -1,15 +1,18 @@ import {Emitter, Disposable} from 'event-kit' -import {setToken} from './auth' +import {setToken, getToken} from './auth' import User from './user' export default class UsersStore { private emitter: Emitter private users: User[] + private persisted: boolean + public constructor() { this.emitter = new Emitter() this.users = [] + this.persisted = false } public onUsersChanged(fn: (users: User[]) => void): Disposable { @@ -29,9 +32,29 @@ export default class UsersStore { this.users.push(user) this.usersDidChange() + + if (this.persisted) { + this.saveToDisk() + } } public loadFromDisk() { - // TODO: actually do it + this.persisted = true + + const raw = localStorage.getItem('users') + if (!raw || !raw.length) { + return + } + + const rawUsers: User[] = JSON.parse(raw) + const usersWithTokens = rawUsers.map(user => new User(user.login, getToken(user.login))) + this.users = usersWithTokens + + this.usersDidChange() + } + + private saveToDisk() { + const usersWithoutTokens = this.users.map(user => new User(user.login, '')) + localStorage.setItem('users', JSON.stringify(usersWithoutTokens)) } }