mirror of
https://github.com/desktop/desktop
synced 2024-10-31 05:19:03 +00:00
Persist the logged in users
This commit is contained in:
parent
383fe2acf0
commit
64368d7744
4 changed files with 31 additions and 9 deletions
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue