Persist the logged in users

This commit is contained in:
joshaber 2016-05-27 15:16:51 -04:00
parent 383fe2acf0
commit 64368d7744
4 changed files with 31 additions and 9 deletions

View file

@ -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))
}

View file

@ -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()

View file

@ -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
}
}

View file

@ -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))
}
}