Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new expired date column #84

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/js/tool.js

Large diffs are not rendered by default.

74 changes: 40 additions & 34 deletions resources/js/components/SanctumTokens.vue
Expand Up @@ -16,43 +16,49 @@
>
<table class="table w-full" cellspacing="0" cellpadding="0">
<thead class="bg-gray-50 dark:bg-gray-800">
<tr>
<th
class="td-fit uppercase text-xxs text-gray-500 tracking-wide pl-5 pr-2 py-2"
>
<span class="sr-only">spacer</span>
</th>
<th
class="text-left px-2 whitespace-nowrap uppercase text-gray-500 text-xxs tracking-wide py-2"
>
<span>{{ __("Name") }}</span>
</th>
<th
v-if="panel.fields[0].showAbilities"
class="text-left px-2 whitespace-nowrap uppercase text-gray-500 text-xxs tracking-wide py-2"
>
<span>{{ __("Abilities") }}</span>
</th>
<th
class="text-left px-2 whitespace-nowrap uppercase text-gray-500 text-xxs tracking-wide py-2"
>
<span>{{ __("Last Used") }}</span>
</th>
<tr>
<th
class="td-fit uppercase text-xxs text-gray-500 tracking-wide pl-5 pr-2 py-2"
>
<span class="sr-only">spacer</span>
</th>
<th
class="text-left px-2 whitespace-nowrap uppercase text-gray-500 text-xxs tracking-wide py-2"
>
<span>{{ __("Name") }}</span>
</th>
<th
v-if="panel.fields[0].showAbilities"
class="text-left px-2 whitespace-nowrap uppercase text-gray-500 text-xxs tracking-wide py-2"
>
<span>{{ __("Abilities") }}</span>
</th>
<th
class="text-left px-2 whitespace-nowrap uppercase text-gray-500 text-xxs tracking-wide py-2"
>
<span>{{ __("Last Used") }}</span>
</th>
<th
class="text-left px-2 whitespace-nowrap uppercase text-gray-500 text-xxs tracking-wide py-2"
>
<span>{{ __("Expiration Date") }}</span>
</th>

<!-- View, Edit, and Delete -->
<th class="uppercase text-xxs tracking-wide px-2 py-2">
<span class="sr-only">{{ __("Controls") }}</span>
</th>
</tr>
<!-- View, Edit, and Delete -->
<th class="uppercase text-xxs tracking-wide px-2 py-2">
<span class="sr-only">{{ __("Controls") }}</span>
</th>
</tr>
</thead>
<tbody>
<TokenRow
v-for="token in tokens"
:key="token.id"
:token="token"
:show-abilities="panel.fields[0].showAbilities"
@revoke-token="revokeToken"
/>
<TokenRow
v-for="token in tokens"
:key="token.id"
:token="token"
:default-expiration-duration="panel.fields[0].defaultExpirationDuration"
:show-abilities="panel.fields[0].showAbilities"
@revoke-token="revokeToken"
/>
</tbody>
</table>
</div>
Expand Down
51 changes: 50 additions & 1 deletion resources/js/components/TokenRow.vue
Expand Up @@ -30,6 +30,17 @@
<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"
>
Expand Down Expand Up @@ -81,6 +92,10 @@ export default {
required: true,
type: Boolean,
},
defaultExpirationDuration: {
required: true,
type: Number,
}
},
emits: ["revoke-token"],
data() {
Expand All @@ -103,6 +118,32 @@ export default {
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() {
Expand All @@ -113,4 +154,12 @@ export default {
};
</script>

<style lang="css" scoped></style>
<style lang="css" scoped>
.warning-color {
color: rgb(249 115 22);
}

.expired-color {
color: rgb(239 68 68);
}
</style>
2 changes: 2 additions & 0 deletions src/SanctumTokens.php
Expand Up @@ -9,12 +9,14 @@ class SanctumTokens extends ResourceTool
private $defaultOptions = [
"showAbilities" => true,
"defaultAbilities" => "*",
"defaultExpirationDuration" => null,
];

public function __construct()
{
parent::__construct();

$this->defaultOptions['defaultExpirationDuration'] = config('sanctum.expiration');
return $this->withMeta($this->defaultOptions);
}

Expand Down