2022-12-27 09:38:35 +01:00
|
|
|
<template>
|
|
|
|
<n-card>
|
|
|
|
<n-form-item label="JWT to decode" :feedback="validation.message" :validation-status="validation.status">
|
|
|
|
<n-input v-model:value="rawJwt" type="textarea" placeholder="Put your token here..." rows="5" />
|
|
|
|
</n-form-item>
|
|
|
|
|
2023-01-13 13:59:27 +01:00
|
|
|
<n-table v-if="validation.isValid">
|
2022-12-27 09:38:35 +01:00
|
|
|
<tbody>
|
2023-01-13 13:59:27 +01:00
|
|
|
<template v-for="section of sections" :key="section.key">
|
|
|
|
<th colspan="2" class="table-header">{{ section.title }}</th>
|
|
|
|
<tr v-for="{ claim, claimDescription, friendlyValue, value } in decodedJWT[section.key]" :key="claim + value">
|
|
|
|
<td class="claims">
|
|
|
|
<n-space>
|
|
|
|
<n-text strong>{{ claim }}</n-text>
|
|
|
|
<template v-if="claimDescription">
|
|
|
|
<n-text depth="3">({{ claimDescription }})</n-text>
|
|
|
|
</template>
|
|
|
|
</n-space>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<n-space>
|
|
|
|
<n-text>{{ value }}</n-text>
|
|
|
|
<template v-if="friendlyValue">
|
|
|
|
<n-text depth="3">({{ friendlyValue }})</n-text>
|
|
|
|
</template>
|
|
|
|
</n-space>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</template>
|
2022-12-27 09:38:35 +01:00
|
|
|
</tbody>
|
|
|
|
</n-table>
|
|
|
|
</n-card>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
|
|
|
import { useValidation } from '@/composable/validation';
|
|
|
|
import { isNotThrowing } from '@/utils/boolean';
|
2023-01-13 13:59:27 +01:00
|
|
|
import { withDefaultOnError } from '@/utils/defaults';
|
|
|
|
import { computed, ref } from 'vue';
|
|
|
|
import { decodeJwt } from './jwt-parser.service';
|
2022-12-27 09:38:35 +01:00
|
|
|
|
|
|
|
const rawJwt = ref(
|
|
|
|
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
|
|
|
|
);
|
|
|
|
|
2023-01-13 13:59:27 +01:00
|
|
|
const decodedJWT = computed(() =>
|
|
|
|
withDefaultOnError(() => decodeJwt({ jwt: rawJwt.value }), { header: [], payload: [] }),
|
|
|
|
);
|
|
|
|
|
|
|
|
const sections = [
|
|
|
|
{ key: 'header', title: 'Header' },
|
|
|
|
{ key: 'payload', title: 'Payload' },
|
|
|
|
] as const;
|
|
|
|
|
2022-12-27 09:38:35 +01:00
|
|
|
const validation = useValidation({
|
|
|
|
source: rawJwt,
|
|
|
|
rules: [
|
|
|
|
{
|
2023-01-13 13:59:27 +01:00
|
|
|
validator: (value) => value.length > 0 && isNotThrowing(() => decodeJwt({ jwt: rawJwt.value })),
|
2022-12-27 09:38:35 +01:00
|
|
|
message: 'Invalid JWT',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="less" scoped>
|
|
|
|
.table-header {
|
|
|
|
text-align: center;
|
|
|
|
}
|
|
|
|
</style>
|