forked from JeffBeltran/sanctum-tokens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TokenRow.vue
165 lines (157 loc) · 4.88 KB
/
TokenRow.vue
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<template>
<tr>
<td
class="py-2 border-t border-gray-100 dark:border-gray-700 px-2 td-fit pl-5 pr-5 dark:bg-gray-800"
></td>
<td
class="px-2 py-2 border-t border-gray-100 dark:border-gray-700 whitespace-nowrap dark:bg-gray-800"
>
<div class="text-left text-left" via-resource="users" via-resource-id="1">
<span class="whitespace-no-wrap">{{ token.name }}</span>
</div>
</td>
<td
v-if="showAbilities"
class="px-2 py-2 border-t border-gray-100 dark:border-gray-700 whitespace-nowrap dark:bg-gray-800"
>
<div class="text-left text-left" via-resource="users" via-resource-id="1">
<span class="whitespace-no-wrap">{{ token.abilities }}</span>
</div>
</td>
<td
class="px-2 py-2 border-t border-gray-100 dark:border-gray-700 whitespace-nowrap dark:bg-gray-800"
>
<div
class="text-left text-left"
via-resource="users"
via-resource-id="1"
:title="lastUsedTooltip"
>
<span class="whitespace-no-wrap">{{ lastUsed }}</span>
</div>
</td>
<td
class="px-2 py-2 border-t border-gray-100 dark:border-gray-700 whitespace-nowrap dark:bg-gray-800"
>
<div
class="text-left text-left"
via-resource="users"
via-resource-id="1"
>
<span class="whitespace-no-wrap" :class="expirationDate.cssClass ? expirationDate.cssClass : ''">{{ expirationDate.date }}</span>
</div>
</td>
<td
class="py-2 border-t border-gray-100 dark:border-gray-700 px-2 td-fit text-right align-middle dark:bg-gray-800"
>
<div class="flex items-center justify-end space-x-0 text-gray-400">
<button
aria-label="Delete"
class="toolbar-button hover:text-primary-500 px-2 v-popper--has-tooltip"
@click="showDeleteModal = true"
>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
width="24"
height="24"
class="inline-block"
role="presentation"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"
></path>
</svg>
</button>
</div>
</td>
<DeleteTokenModal
:show="showDeleteModal"
:token-name="token.name"
@cancel="showDeleteModal = false"
@confirmed="revokeToken"
/>
</tr>
</template>
<script>
import { DateTime } from "luxon";
export default {
props: {
token: {
required: true,
type: Object,
},
showAbilities: {
required: true,
type: Boolean,
},
defaultExpirationDuration: {
required: true,
type: Number,
}
},
emits: ["revoke-token"],
data() {
return {
showDeleteModal: false,
};
},
computed: {
lastUsed() {
if (this.token.last_used_at) {
return DateTime.fromISO(this.token.last_used_at).toRelativeCalendar();
} else {
return "—";
}
},
lastUsedTooltip() {
if (this.token.last_used_at) {
return DateTime.fromISO(this.token.last_used_at).toFormat("f ZZZZ");
} else {
return "—";
}
},
expirationDate() {
let tokenExpirationDate = this.token.expires_at ? new Date(this.token.expires_at) : null;
const defaultExpirationDate = this.defaultExpirationDuration
if (defaultExpirationDate) {
const tokenCreationDate = new Date(this.token.created_at);
// Add default expiration time to created token date
const defaultTokenExpirationDate = new Date(tokenCreationDate.getTime() + this.defaultExpirationDuration * 60000);
const tokenCustomExpirationDate = this.token.expires_at ? new Date(this.token.expires_at) : null;
tokenExpirationDate = tokenCustomExpirationDate ? (tokenCustomExpirationDate < defaultTokenExpirationDate ? tokenCustomExpirationDate : defaultTokenExpirationDate) : defaultTokenExpirationDate;
}
const today = new Date();
const diffInDays = tokenExpirationDate ? ((today - tokenExpirationDate) / (1000 * 3600 * 24)) : null;
return tokenExpirationDate
? {
date: tokenExpirationDate.toLocaleDateString(),
cssClass: diffInDays ? (diffInDays > -10 && diffInDays < 0 ? 'warning-color' : (diffInDays > 0 ? 'expired-color' : '')) : null,
}
: {
date: '—',
cssClass: null,
};
},
},
methods: {
revokeToken() {
this.showDeleteModal = false;
this.$emit("revoke-token", this.token.id);
},
},
};
</script>
<style lang="css" scoped>
.warning-color {
color: rgb(249 115 22);
}
.expired-color {
color: rgb(239 68 68);
}
</style>