Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/ayonli_jsext/workerd/http.ts>verifyBasicAuth

A JavaScript extension package for building strong and modern applications.
Latest
function verifyBasicAuth
import { verifyBasicAuth } from "https://deno.land/x/ayonli_jsext@v0.9.72/workerd/http.ts";

Performs basic authentication verification for the request. When passed, this function returns nothing (undefined), otherwise it returns a Response with status 401 Unauthorized, which should be responded to the client.

Examples

Example 1

import { verifyBasicAuth, type BasicAuthorization } from "@ayonli/jsext/http";

const users = new Map([
   ["root", "pa$$w0rd"]
]);

async function verify(auth: BasicAuthorization) {
    const password = users.get(auth.username);
    return !!password && password === auth.password;
}

export default {
    async fetch(req) {
        const res = await verifyBasicAuth(req, verify);

        if (res) {
            return res;
        }

        // continue with the request
    },
};

Parameters

req: Request
verify: (auth: BasicAuthorization) => boolean | Promise<boolean>

Returns

Promise<void | Response>