mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 06:39:48 +00:00
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:
parent
5d08381be5
commit
09adb58c40
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user