Strip URL Auth

2.0.0 · active · verified Wed Apr 22

strip-url-auth is a focused JavaScript utility that removes the username and password (authentication) portion from a given URL string. It is currently stable at version 2.0.0. Developed by Sindresorhus, it adheres to a philosophy of single-purpose, highly reliable modules. Key differentiators include its minimalistic API, robust parsing for common URL formats, and its strict adherence to modern JavaScript standards. Releases typically occur when necessary for compatibility with newer Node.js versions or to address specific edge cases, rather than on a fixed cadence. It provides a simple, direct solution for sanitizing URLs for display or storage where authentication credentials should not be exposed.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to import and use `stripUrlAuth` to remove authentication credentials from various URL formats, including HTTPS and FTP, and shows its behavior with URLs that do not contain auth or have malformed auth segments.

import stripUrlAuth from 'strip-url-auth';

const urlWithAuth = 'https://admin:securepass@example.com/path?query=value#hash';
const urlWithoutAuth = stripUrlAuth(urlWithAuth);

console.log(`Original URL: ${urlWithAuth}`);
console.log(`URL without auth: ${urlWithoutAuth}`);

// Example with no auth
const simpleUrl = 'https://www.google.com';
const simpleUrlStripped = stripUrlAuth(simpleUrl);
console.log(`\nOriginal simple URL: ${simpleUrl}`);
console.log(`Stripped simple URL: ${simpleUrlStripped}`);

// Demonstrating with a different protocol
const ftpUrl = 'ftp://user:secret@ftp.example.org/files';
const ftpUrlStripped = stripUrlAuth(ftpUrl);
console.log(`\nOriginal FTP URL: ${ftpUrl}`);
console.log(`Stripped FTP URL: ${ftpUrlStripped}`);

// Handles edge cases like empty auth or missing protocol gracefully
const malformedAuthUrl = 'user:@host.com/path';
const malformedAuthUrlStripped = stripUrlAuth(malformedAuthUrl);
console.log(`\nMalformed auth URL: ${malformedAuthUrl}`);
console.log(`Stripped malformed auth URL: ${malformedAuthUrlStripped}`);

view raw JSON →