urlObject.auth() Method in Node.js

Node.jsJavascriptWeb DevelopmentFront End Technology

The auth() property defines the username and password portion of any URL, also called as userInfo. The string and username are separated by a colon ( : ).

Syntax

urlOject.auth()

Parameters

Since it retuns only the username and password from a URL, it does not required any input parameters.

Example

Create a file with name – auth.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below −

node auth.js

auth.js

 Live Demo

// 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);

Output

C:\home\node>> node auth.js
username=hello:password=tutorialspoint

Example

Let's take a look at one more example.

 Live Demo

// 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);

Output

C:\home\node>> node auth.js
username=admin:password=tutorialspoint123
raja
Published on 20-May-2021 13:27:14
Advertisements