Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions build/bring.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/bring.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions src/bring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,31 @@ class Bring {
* Try to log into given account
*/
async login(): Promise<void> {
let data: AuthResponse;
let resp: Response;
let bodyText: string;

try {
const resp = await fetch(`${this.url}bringauth`, {
resp = await fetch(`${this.url}bringauth`, {
method: 'POST',
body: new URLSearchParams({ email: this.mail, password: this.password })
});

data = await resp.json();
// Read as text first so non-JSON bodies (e.g. text/plain "Invalid Email.",
// HTML maintenance pages, Cloudflare challenges) are preserved verbatim
// instead of being swallowed by resp.json() as "Failed to parse JSON".
bodyText = await resp.text();
} catch (e: any) {
throw new Error(`Cannot Login: ${e.message}`);
}

let data: AuthResponse;
try {
data = JSON.parse(bodyText);
} catch {
const snippet = bodyText.trim().slice(0, 500) || '(empty body)';
throw new Error(`Cannot Login: ${resp.status} ${snippet}`);
}

if ('error' in data) {
throw new Error(`Cannot Login: ${data.message}`);
}
Expand Down