Passed
Push — 6.4 ( ab3aa6...778d27 )
by Christian
13:21 queued 12s
created

index.ts ➔ metaInfo   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
import './sw-inactivity-login.scss';
2
import type { MetaInfo } from 'vue-meta';
3
import template from './sw-inactivity-login.html.twig';
4
5
const { Component } = Shopware;
6
7
/**
8
 * @package admin
9
 *
10
 * @private
11
 */
12
Component.register('sw-inactivity-login', {
13
    template,
14
15
    inject: ['loginService'],
16
17
    data(): {
18
        isLoading: boolean,
19
        lastKnownUser: string,
20
        password: string,
21
        passwordError: null|{ detail: string }
22
        } {
23
        return {
24
            isLoading: false,
25
            lastKnownUser: '',
26
            password: '',
27
            passwordError: null,
28
        };
29
    },
30
31
    computed: {
32
        title(): string {
33
            const moduleName = this.$tc('sw-inactivity-login.general.mainMenuItemIndex');
34
            const adminName = this.$tc('global.sw-admin-menu.textShopwareAdmin');
35
36
            return `${moduleName} | ${adminName}`;
37
        },
38
    },
39
40
    metaInfo(): MetaInfo {
41
        return {
42
            title: this.title,
43
        };
44
    },
45
46
    created() {
47
        const lastKnownUser = localStorage.getItem('lastKnownUser');
48
        localStorage.removeItem('lastKnownUser');
49
50
        if (!lastKnownUser) {
51
            void this.$router.push({ name: 'sw.login.index' });
52
53
            return;
54
        }
55
56
        this.lastKnownUser = lastKnownUser;
57
    },
58
59
    mounted() {
60
        const dataUrl = localStorage.getItem('inactivityBackground');
61
        if (!dataUrl) {
62
            return;
63
        }
64
65
        // We know this exists once the component is mounted
66
        (document.querySelector('.sw-inactivity-login') as HTMLElement).style.backgroundImage = `url('${dataUrl}')`;
67
    },
68
69
    beforeDestroy() {
70
        localStorage.removeItem('inactivityBackground');
71
    },
72
73
    methods: {
74
        loginUserWithPassword() {
75
            this.isLoading = true;
76
77
            return this.loginService.loginByUsername(this.lastKnownUser, this.password)
78
                .then(() => {
79
                    this.handleLoginSuccess();
80
                    this.isLoading = false;
81
                })
82
                .catch(() => {
83
                    this.password = '';
84
85
                    this.passwordError = {
86
                        detail: this.$tc('sw-inactivity-login.modal.errors.password'),
87
                    };
88
89
                    this.isLoading = false;
90
                });
91
        },
92
93
        handleLoginSuccess() {
94
            this.password = '';
95
96
            this.forwardLogin();
97
98
            window.location.reload();
99
        },
100
101
        forwardLogin() {
102
            const previousRoute = JSON.parse(sessionStorage.getItem('sw-admin-previous-route') || '') as {
103
                fullPath?: string,
104
                name?: string,
105
            };
106
            sessionStorage.removeItem('sw-admin-previous-route');
107
108
            if (previousRoute?.fullPath) {
109
                void this.$router.push(previousRoute.fullPath);
110
                return;
111
            }
112
113
            void this.$router.push({ name: 'core' });
114
        },
115
116
        onBackToLogin() {
117
            this.isLoading = true;
118
            this.lastKnownUser = '';
119
            this.password = '';
120
121
            void this.$router.push({ name: 'sw.login.index' });
122
        },
123
    },
124
});
125