Computer >> Computer tutorials >  >> Programming >> Javascript

urlObject.auth() Method in Node.js


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

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

var adr = 'https://username=hello:[email protected]/';

// 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.

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

var adr = 'https://username=admin:[email protected]/';

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