-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathauthenticate.js
More file actions
98 lines (78 loc) 路 2.6 KB
/
Copy pathauthenticate.js
File metadata and controls
98 lines (78 loc) 路 2.6 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import path from 'path'
import pogobuf from 'pogobuf-vnext'
import {
remote
} from 'electron'
import {
createAction
} from 'redux-actions'
import * as fs from 'async-file'
import { setClient } from '../client'
import {
getTrainerInfoAndPokemon,
} from './trainer'
const saveAccountCredentialsFailed = createAction('SAVE_ACCOUNT_CREDENTIALS_FAILED')
const saveAccountCredentialsSuccess = createAction('SAVE_ACCOUNT_CREDENTIALS_SUCCESS')
const checkAndDeleteCredentialsFailed = createAction('CHECK_AND_DELETE_CREDENTIALS_FAILED')
const checkAndDeleteCredentialsSuccess = createAction('CHECK_AND_DELETE_CREDENTIALS_SUCCESS')
const userLoginStarted = createAction('USER_LOGIN_STARTED')
const userLoginSuccess = createAction('USER_LOGIN_SUCCESS')
const userLoginFailed = createAction('USER_LOGIN_FAILED')
const accountPath = path.join(remote.app.getPath('appData'), '/pokenurse/account.json')
export default {
login({ method, username, password, hashingKey, apiVersion }) {
return async (dispatch) => {
dispatch(userLoginStarted())
let login
if (method === 'google') {
login = new pogobuf.GoogleLogin()
} else {
login = new pogobuf.PTCLogin()
}
try {
const token = await login.login(username, password)
const options = {
hashingKey,
authType: method,
authToken: token,
useHashingServer: !!hashingKey,
}
// Use default API version
if (hashingKey) options.version = apiVersion || 7100
const client = new pogobuf.Client(options)
await client.init()
setClient(client)
// TODO display a loading spinner
// then fetch all necessary things
await dispatch(getTrainerInfoAndPokemon())
dispatch(userLoginSuccess())
} catch (error) {
console.error(error) // eslint-disable-line
dispatch(userLoginFailed({ error }))
}
}
},
logout: createAction('USER_LOGOUT'),
checkAndDeleteCredentials() {
return async (dispatch) => {
try {
if (await fs.exists(accountPath)) {
await fs.unlink(accountPath)
}
dispatch(checkAndDeleteCredentialsSuccess())
} catch (error) {
dispatch(checkAndDeleteCredentialsFailed())
}
}
},
saveAccountCredentials(credentials) {
return async (dispatch) => {
try {
const response = await fs.writeFile(accountPath, JSON.stringify(credentials))
dispatch(saveAccountCredentialsSuccess({ response, credentials }))
} catch (error) {
dispatch(saveAccountCredentialsFailed(error))
}
}
}
}