chore: investigate login error [INS-3851] (#7438)

* chore: investigate login error [INS-3851]

* another pass at error we show

* Update packages/insomnia/src/ui/auth-session-provider.ts
This commit is contained in:
Filipe Freire 2024-05-28 13:45:13 +01:00 committed by GitHub
parent 5d08381be5
commit 09adb58c40
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 5 deletions

View File

@ -1,4 +1,3 @@
import { decodeBase64, encodeBase64 } from '@getinsomnia/api-client/base64';
import { keyPair, open } from '@getinsomnia/api-client/sealedbox';
import * as session from '../account/session';
@ -29,6 +28,43 @@ encodeBase64(sessionKeyPair.secretKey).then(res => {
* Keypair used for the login handshake.
* This keypair can be re-used for the entire session.
*/
export async function decodeBase64(base64: string): Promise<Uint8Array> {
try {
let uri = 'data:application/octet-binary;base64,';
uri += base64;
const res = await fetch(uri);
const buffer = await res.arrayBuffer();
return new Uint8Array(buffer);
} catch (error) {
console.error(error);
throw new Error('Failed to decode base64');
}
}
export async function encodeBase64(data: Uint8Array): Promise<string> {
const dataUri = await new Promise<string>((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
if (typeof reader.result === 'string') {
resolve(reader.result);
} else {
reject();
}
};
reader.onerror = reject;
reader.readAsDataURL(new Blob([data]));
});
const dataAt = dataUri.indexOf(',');
if (dataAt === -1) {
throw new Error(`unexpected data uri output: ${dataUri}`);
}
return dataUri.slice(dataAt + 1);
}
export async function submitAuthCode(code: string) {
try {
const rawBox = await decodeBase64(code.trim());
@ -41,7 +77,8 @@ export async function submitAuthCode(code: string) {
const box: AuthBox = JSON.parse(decoder.decode(boxData));
await session.absorbKey(box.token, box.key);
} catch (error) {
return error.message;
console.error(error);
return error;
}
}

View File

@ -13,11 +13,12 @@ export const action: ActionFunction = async ({
const data = await request.json();
invariant(typeof data?.code === 'string', 'Expected code to be a string');
const fetchError = await submitAuthCode(data.code);
if (fetchError) {
const error = await submitAuthCode(data.code);
if (error) {
const humanReadableError = error === 'TypeError: Failed to fetch' ? 'Network failed, try again.' : error;
return {
errors: {
message: 'Invalid code: ' + fetchError,
message: humanReadableError,
},
};
}