-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
➖ remove cryptojs dependency in favor of browser native crypto
- Loading branch information
Showing
3 changed files
with
31 additions
and
35 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,38 @@ | ||
<script setup lang="ts"> | ||
import { onMounted, ref } from "vue"; | ||
import { User } from "../types/mete"; | ||
const emit = defineEmits<{ | ||
click: [user: User]; | ||
}>(); | ||
const props = defineProps<{ user: User }>(); | ||
const gravatar = ref<string>(); | ||
// copy paste: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#converting_a_digest_to_a_hex_string | ||
async function sha256(message: string) { | ||
const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array | ||
const hashBuffer = await crypto.subtle.digest("SHA-256", msgUint8); // hash the message | ||
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array | ||
const hashHex = hashArray | ||
.map((b) => b.toString(16).padStart(2, "0")) | ||
.join(""); // convert bytes to hex string | ||
return hashHex; | ||
} | ||
onMounted(async () => { | ||
gravatar.value = `https://www.gravatar.com/avatar/${await sha256( | ||
props.user.email | ||
)}`; | ||
}); | ||
</script> | ||
<template> | ||
<el-card class="card" @click="$emit('click', user)"> | ||
<el-card class="card" @click="emit('click', user)"> | ||
<el-row> | ||
<el-col><img :src="gravatar(user.email)" class="image" /></el-col> | ||
<el-col><img v-if="gravatar" :src="gravatar" class="image" /></el-col> | ||
</el-row> | ||
<el-row> | ||
<el-col>{{ user.name }}</el-col> | ||
</el-row> | ||
</el-card> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { PropType, defineComponent } from "vue"; | ||
import { User } from "../types/mete"; | ||
import MD5 from "crypto-js/md5"; | ||
export default defineComponent({ | ||
name: "UserCard", | ||
props: { user: { type: Object as PropType<User>, required: true } }, | ||
emits: ["click"], | ||
methods: { | ||
gravatar(email: string) { | ||
return `https://www.gravatar.com/avatar/${MD5(email)}`; | ||
}, | ||
}, | ||
}); | ||
</script> |