-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathDrivePage.vue
More file actions
120 lines (118 loc) · 3.43 KB
/
DrivePage.vue
File metadata and controls
120 lines (118 loc) · 3.43 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<template>
<div>
<div>
<div class="text warning" v-show="needEncryption">
{{ i18n.encryption_required }}
</div>
<div v-show="backupToken">
<div style="margin: 10px 0px 0px 20px; overflow-wrap: break-word">
{{ i18n.account }} - {{ email }}
</div>
</div>
<a-button v-show="backupToken" @click="backupLogout()">
{{ i18n.log_out }}
</a-button>
<a-button
v-show="!backupToken && !needEncryption"
@click="getBackupToken()"
>
{{ i18n.sign_in }}
</a-button>
<a-button v-show="backupToken && !needEncryption" @click="backupUpload()">
{{ i18n.manual_dropbox }}
</a-button>
</div>
</div>
</template>
<script lang="ts">
import Vue from "vue";
import { isChrome } from "../../browser";
import { Drive } from "../../models/backup";
import { UserSettings } from "../../models/settings";
const service = "drive";
export default Vue.extend({
data: function () {
return {
email: this.i18n.loading,
};
},
created() {
UserSettings.updateItems();
},
computed: {
defaultEncryption: function () {
return this.$store.state.accounts.defaultEncryption;
},
allEntriesEncrypted: function (): boolean {
return this.$store.getters["accounts/allEntriesEncrypted"];
},
backupToken: function (): string | undefined {
return this.$store.state.backup.driveToken;
},
needEncryption: function (): boolean {
return !this.defaultEncryption || !this.allEntriesEncrypted;
},
},
methods: {
getBackupToken() {
chrome.runtime.sendMessage({ action: service });
},
async backupLogout() {
await new Promise((resolve: (value: boolean) => void) => {
const xhr = new XMLHttpRequest();
xhr.open(
"POST",
"https://accounts.google.com/o/oauth2/revoke?token=" +
UserSettings.items.driveToken
);
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (isChrome) {
chrome.identity.removeCachedAuthToken(
{ token: UserSettings.items.driveToken as string },
() => {
resolve(true);
}
);
} else {
resolve(true);
}
return;
}
};
xhr.send();
});
UserSettings.removeItem("driveToken");
this.$store.commit("backup/setToken", { service, value: false });
this.$store.commit("style/hideInfo");
},
async backupUpload() {
const drive = new Drive();
const response = await drive.upload(
this.$store.state.accounts.encryption
);
if (response === true) {
this.$store.commit("notification/alert", this.i18n.updateSuccess);
} else if (UserSettings.items.driveRevoked === true) {
this.$store.commit(
"notification/alert",
chrome.i18n.getMessage("token_revoked", ["Google Drive"])
);
UserSettings.removeItem("driveRevoked");
this.$store.commit("backup/setToken", { service, value: false });
} else {
this.$store.commit("notification/alert", this.i18n.updateFailure);
}
},
async getUser() {
const drive = new Drive();
return await drive.getUser();
},
},
mounted: async function () {
if (this.backupToken) {
this.email = await this.getUser();
}
},
});
</script>