Fixed bug

Welp, so much for a working state app huh
This commit is contained in:
Nixyi 2024-12-16 23:22:40 -03:00
parent fc8110bcad
commit ca8e649932
3 changed files with 43 additions and 30 deletions

View file

@ -86,23 +86,30 @@ export default function ChatPage() {
}, },
async (payload) => { async (payload) => {
if (payload.eventType === "INSERT") { if (payload.eventType === "INSERT") {
const messageData = payload.new as SiPher.RealtimeMessageData; try {
const isSender = messageData.sender_uuid === currentUser.uuid; const messageData = payload.new as SiPher.RealtimeMessageData;
const isSender = messageData.sender_uuid === currentUser.uuid;
const decryptedMsg = await CryptoManager.decryptMessage(messageData.sender_content)
console.log(`Hello there`) const decryptedMsg = await CryptoManager.decryptMessage(
setMessages((prevState) => { // I forgot to add this, without this, it's pretty much unusable.
return [ isSender ? messageData.sender_content : messageData.recipient_content
...prevState, )
{
id: messageData.id, setMessages((prevState) => {
content: decryptedMsg, return [
sender_uuid: messageData.sender_uuid, ...prevState,
created_at: messageData.created_at, {
isSender id: messageData.id,
} content: decryptedMsg,
] sender_uuid: messageData.sender_uuid,
}) created_at: messageData.created_at,
isSender
}
]
})
} catch (e: any) {
console.error(`Something went wrong on the message update: ${e}`)
}
} }
} }
) )
@ -286,8 +293,8 @@ export default function ChatPage() {
> >
<div className={`max-w-[70%] rounded-lg p-3 ${ <div className={`max-w-[70%] rounded-lg p-3 ${
message.isSender message.isSender
? 'bg-primary text-primary-foreground' ? message.error ? 'bg-red-500' : 'bg-primary text-primary-foreground'
: 'bg-secondary' : message.error ? 'bg-red-500' : 'bg-secondary'
}`}> }`}>
<p>{message.content}</p> <p>{message.content}</p>
<div className="flex items-center justify-end space-x-1 mt-1"> <div className="flex items-center justify-end space-x-1 mt-1">

View file

@ -204,7 +204,7 @@ export class CryptoManager {
recipientPublicKey, recipientPublicKey,
{ {
name: "RSA-OAEP", name: "RSA-OAEP",
hash: "SHA-256", hash: "SHA-256", // This is important!
}, },
true, true,
["encrypt"] ["encrypt"]
@ -213,7 +213,7 @@ export class CryptoManager {
const encoder = new TextEncoder(); const encoder = new TextEncoder();
const encrypted = await crypto.subtle.encrypt( const encrypted = await crypto.subtle.encrypt(
{ {
name: "RSA-OAEP", name: "RSA-OAEP"
}, },
publicKey, publicKey,
encoder.encode(message) encoder.encode(message)
@ -236,15 +236,20 @@ export class CryptoManager {
atob(encryptedMessage).split('').map((char) => char.charCodeAt(0)) atob(encryptedMessage).split('').map((char) => char.charCodeAt(0))
); );
const decrypted = await crypto.subtle.decrypt( try {
{ const decrypted = await crypto.subtle.decrypt(
name: "RSA-OAEP", {
}, name: "RSA-OAEP" // hash is only needed during key import
privateKey, },
encrypted privateKey,
); encrypted
);
return new TextDecoder().decode(decrypted);
return new TextDecoder().decode(decrypted);
} catch (e) {
console.error(`Got an error while trying to decrypt the message: ${e}`);
throw e;
}
} }
/** /**

1
src/types/user.d.ts vendored
View file

@ -5,6 +5,7 @@ declare global {
participants: string[]; participants: string[];
participant_suuids: string[]; participant_suuids: string[];
messages: { messages: {
error?: boolean;
isSender: boolean; isSender: boolean;
id: string; // UUID id: string; // UUID
content: string; // The encrypted content (either sender_content or recipient_content) content: string; // The encrypted content (either sender_content or recipient_content)