vercel / next.js

The React Framework
https://nextjs.org
MIT License
127.55k stars 27.05k forks source link

Private enviroments variables not working with react Msal #73042

Closed cadinhoruf closed 1 week ago

cadinhoruf commented 1 week ago

Link to the code that reproduces this issue

None

To Reproduce

Reproduction Steps

  1. Environment Setup:

    • Ensure you are using the latest version of the MSAL library. (Example: @azure/msal-browser@latest)
    • Use Node.js version 14 or higher.
    • Create a new Next.js project or use an existing one.
    • Make sure you are using a version similar to or equal to 15.0.3 of Nextjs
  2. Install the Library:

    • Run the following command to install the library:
      npm install @azure/msal-browser
  3. Code Configuration:

    • Create a config.ts file with the following configuration:

      import { Configuration, PublicClientApplication } from '@azure/msal-browser';
      
      const msalConfig: Configuration = {
      auth: {
       clientId: process.env.NEXT_PUBLIC_AZURE_AD_CLIENT || '',
       authority: `https://login.microsoftonline.com/${process.env.NEXT_PUBLIC_AZURE_AD_TENANT}/v2.0`,
       redirectUri: process.env.NEXT_PUBLIC_REDIRECT_URI || 'http://localhost:3000'
      },
      cache: {
       cacheLocation: 'localStorage'
      }
      };
      
      const msalInstance = new PublicClientApplication(msalConfig);
      export { msalInstance };
  4. Login Call:

    • In your component, add the following code to call the login method:
      <button onClick={() => instance.loginRedirect()}> Login</button>
  5. Run the Application:

    • Start the development server:
      npm run dev
  6. Access the Application:

    • Open your browser and go to http://localhost:3000.
    • Click the login button that calls the loginRedirect function.
  7. Result:

    • Instead, you receive the error AADSTS900023: Specified tenant identifier 'undefined' is neither a valid DNS name, nor a valid external domain..

Current vs. Expected behavior

This error occurs because the AZURE_AD_TENANT environment variable is not being recognized, resulting in the tenant identifier being undefined. As a result, the login process fails, and I am unable to authenticate the user.

Expected Behavior: I expect the login process to initiate successfully without any errors when the loginRedirect() function is called. Specifically, I expect the following:

  1. The AZURE_AD_TENANT environment variable should be correctly loaded and accessible, allowing the authority URL to be constructed properly.
  2. Upon successful authentication, I should receive a valid authentication response containing user information and access tokens.
  3. The console should display the message "Login successful:" followed by the authentication response object.

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Pro
  Available memory (MB): 16100
  Available CPU cores: 8
Binaries:
  Node: 20.13.1
  npm: 10.2.4
  Yarn: 1.22.19
  pnpm: 9.7.1
Relevant Packages:
  next: 15.0.3 // Latest available version is detected (15.0.3).
  eslint-config-next: 15.0.3
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.6.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

create-next-app

Which stage(s) are affected? (Select all that apply)

next dev (local), next build (local), next start (local), Other (Deployed)

Additional context

When I attempt to log in using the MSAL library with the provided configuration, I expect the following behavior:

  1. Successful Authentication:

    • The login process should initiate without errors when the loginRedirect() function is called.
    • Upon successful authentication, I should receive a valid authentication response containing user information and access tokens.
  2. User Redirection:

    • After successful login, the user should be redirected to the specified redirectUri (e.g., http://localhost:3000).
  3. Access to Protected Resources:

    • Once authenticated, the user should be able to access protected resources or APIs that require authentication, using the access token received during the login process.
  4. No Errors:

    • I expect that no errors will occur during the login process, particularly the error AADSTS900023: Specified tenant identifier 'undefined' is neither a valid DNS name, nor a valid external domain. should not be displayed.

Comments

If I use NEXT_PUBLIC in the environment variables, it works normally, but I can't because it is an internal application.

github-actions[bot] commented 1 week ago

We could not detect a valid reproduction link. Make sure to follow the bug report template carefully.

Why was this issue closed?

To be able to investigate, we need access to a reproduction to identify what triggered the issue. We need a link to a public GitHub repository (template for App Router, template for Pages Router), but you can also use these templates: CodeSandbox: App Router or CodeSandbox: Pages Router.

The bug template that you filled out has a section called "Link to the code that reproduces this issue", which is where you should provide the link to the reproduction.

What should I do?

Depending on the reason the issue was closed, you can do the following:

In general, assume that we should not go through a lengthy onboarding process at your company code only to be able to verify an issue.

My repository is private and cannot make it public

In most cases, a private repo will not be a sufficient minimal reproduction, as this codebase might contain a lot of unrelated parts that would make our investigation take longer. Please do not make it public. Instead, create a new repository using the templates above, adding the relevant code to reproduce the issue. Common things to look out for:

I did not open this issue, but it is relevant to me, what can I do to help?

Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps by opening a new issue.

I think my reproduction is good enough, why aren't you looking into it quickly?

We look into every Next.js issue and constantly monitor open issues for new comments.

However, sometimes we might miss one or two due to the popularity/high traffic of the repository. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.

Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.

Useful Resources