-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathinit.js
87 lines (76 loc) · 2.89 KB
/
init.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
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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import tts from '@console/api/tts'
import { clear as clearAccessToken } from '@ttn-lw/lib/access-token'
import createRequestLogic from '@ttn-lw/lib/store/logics/create-request-logic'
import * as init from '@ttn-lw/lib/store/actions/init'
import { TokenError } from '@ttn-lw/lib/errors/custom-errors'
import { isPermissionDeniedError, isUnauthenticatedError } from '@ttn-lw/lib/errors/utils'
import * as user from '@console/store/actions/logout'
import {
getInboxNotifications,
getUnseenNotificationsPeriodically,
} from '@console/store/actions/notifications'
const consoleAppLogic = createRequestLogic({
type: init.INITIALIZE,
noCancelOnRouteChange: true,
process: async (_, dispatch) => {
dispatch(user.getUserRights())
let info, rights
try {
// There is no way to retrieve the current user directly within the
// console app, so first get the authentication info and only afterwards
// fetch the user.
info = await tts.Auth.getAuthInfo()
rights = info.oauth_access_token.rights
dispatch(user.getUserRightsSuccess(rights))
} catch (error) {
if (
error instanceof TokenError
? !isUnauthenticatedError(error?.cause) && !isPermissionDeniedError(error?.cause)
: !isUnauthenticatedError(error)
) {
throw error
}
// Clear existing access token since it does
// not appear to be valid anymore.
clearAccessToken()
dispatch(user.getUserRightsFailure())
info = undefined
}
if (info) {
try {
dispatch(user.getUserMe())
const userId = info.oauth_access_token.user_ids.user_id
const userResult = await tts.Users.getById(userId, [
'state',
'name',
'primary_email_address_validated_at',
'profile_picture',
'console_preferences',
])
userResult.isAdmin = info.is_admin || false
dispatch(user.getUserMeSuccess(userResult))
dispatch(getInboxNotifications({ page: 1, limit: 3 }))
dispatch(getUnseenNotificationsPeriodically())
} catch (error) {
dispatch(user.getUserMeFailure(error))
}
}
// eslint-disable-next-line no-console
console.log('Console initialization successful!')
return
},
})
export default [consoleAppLogic]