Passed
Push — trunk ( 17b9ed...85c15e )
by Christian
12:23 queued 13s
created

user-information.init.ts ➔ initializeUserContext   A

Complexity

Conditions 3

Size

Total Lines 28
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 23
dl 0
loc 28
rs 9.328
c 0
b 0
f 0
1
/**
2
 * @package admin
3
 */
4
5
import { initializeUserNotifications } from 'src/app/state/notification.store';
6
7
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
8
export default function initializeUserContext() {
9
    return new Promise<void>((resolve) => {
10
        const loginService = Shopware.Service('loginService');
11
        const userService = Shopware.Service('userService');
12
13
        // The user isn't logged in
14
        if (!loginService.isLoggedIn()) {
15
            // Remove existing login info from the locale storage
16
            loginService.logout();
17
            resolve();
18
            return;
19
        }
20
21
        userService.getUser().then((response) => {
22
            // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
23
            const data = response?.data;
24
            // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
25
            delete data.password;
26
27
            Shopware.State.commit('setCurrentUser', data);
28
            initializeUserNotifications();
29
            resolve();
30
        }).catch(() => {
31
            // An error occurred which means the user isn't logged in so get rid of the information in local storage
32
            loginService.logout();
33
            resolve();
34
        });
35
    });
36
}
37