-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
auth-api.js
50 lines (42 loc) · 1.14 KB
/
auth-api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import appStore from '../lib/app-store'
import authStore from './auth-store'
import ipc from '../lib/ipc'
class AuthApi {
loadUser () {
ipc.getCurrentUser()
.then((user) => {
authStore.setUser(user)
// mobx can trigger a synchronous re-render, which executes
// componentDidMount, etc in other components, making bluebird
// think another promise was created but not returned
// return null to prevent bluebird warning about it
// same goes for other `return null`s below
return null
})
.catch(ipc.isUnauthed, ipc.handleUnauthed)
.finally(() => {
authStore.setLoading(false)
})
}
login (utm) {
ipc.onAuthMessage((__, message) => {
authStore.setMessage(message)
})
return ipc.beginAuth(utm)
.then((user) => {
authStore.setUser(user)
authStore.setMessage(null)
return null
})
.catch({ alreadyOpen: true }, () => {})
}
logOut () {
authStore.setUser(null)
ipc.logOut()
.catch((err) => {
err.name = 'An unexpected error occurred while logging out'
appStore.setError(err)
})
}
}
export default new AuthApi()