urlObject.auth() Method in Node.js

The auth property of a parsed URL object contains the username and password portion of a URL, also known as userInfo. The username and password are separated by a colon (:).

Syntax

urlObject.auth

Parameters

The auth property is read-only and does not require any input parameters. It returns the authentication credentials from the parsed URL.

Example 1: Basic Authentication

Create a file named auth.js and run it using node auth.js:

// Importing the URL module
const url = require('url');

var adr = 'https://username=hello:password=tutorialspoint@www.tutorialspoint.com/';

// Parsing the above URL address
var q = url.parse(adr, true);

// Printing the auth details
console.log(q.auth);
username=hello:password=tutorialspoint

Example 2: Different Credentials

Here's another example with different authentication credentials:

// Importing the URL module
const url = require('url');

var adr = 'http://username=admin:password=tutorialspoint123@www.tutorialspoint.com/';

// Parsing the above URL address
var q = url.parse(adr, true);

// Printing the auth details
console.log(q.auth);
username=admin:password=tutorialspoint123

Example 3: URL Without Auth

When a URL doesn't contain authentication information, the auth property returns null:

const url = require('url');

var adr = 'https://www.tutorialspoint.com/nodejs/index.htm';
var q = url.parse(adr, true);

console.log(q.auth); // null for URLs without auth
null

Conclusion

The auth property extracts username and password from URLs containing authentication credentials. It returns null for URLs without authentication information.

Updated on: 2026-03-15T23:19:00+05:30

167 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements