Search code examples
node.jsazure-active-directoryazure-sql-databasetedious

How do I use Azure AD Authentication with node-mssql?


So I got a connection working using tedious but the options available in node-mssql for handling JSON are something really useful that I would like to have access to.

The documentation for node-mssql says you can pass an object with authentication settings that tedious would use and it will override the user/password properties but it's definitely not doing that. I can confirm because the error message comes back with the value for the user property.

Is there something wrong with the config object?

const sqlConfig = {
    server: process.env.SQL_SERVER,    
    database: process.env.DATABASE_NAME,
    user: "root.user",                    
    password: "password",       
    options:
    {
        encrypt: true,             
        authentication: {
            type: "azure-active-directory-password",                         
            options: {
                userName: "root.options.authentication.options.userName",                    
                password: "password"                    
                }
        },
    }
};

Solution

  • Here's the example using node-mssql and azure-active-directory-password(supports Azure AD from [email protected]):

    const config = {
        server: 'yoursqlserver.database.windows.net',
        database: 'yourdb',
        authentication: {
            type: "azure-active-directory-password",
            options: {
                userName: "[email protected]",
                password: "password",
                }
            }
        }
    

    Ref here:

    1. https://stackoverflow.com/a/58386825/10549281
    2. https://stackoverflow.com/a/56145320/10549281.
    3. https://github.com/tediousjs/tedious/issues/416#issuecomment-441364272