Parse a Basic Auth Header
Medium · Auth
With HTTP Basic auth the client sends Authorization: Basic <base64 of "user:password">. Write parseBasicAuth(header) that returns { user, password }, or null when the header is not valid Basic auth.
- The scheme Basic is case-insensitive and is followed by one or more spaces, then the base64 credentials.
- Decode with atob, then turn the bytes into text with TextDecoder so non-English characters work.
- Split at the FIRST :. The password may contain more colons and may be empty; the user may be empty too.
- Return null for: a missing/empty header, another scheme, invalid base64, or decoded text without :.