<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: random_person</title>
    <description>The latest articles on DEV Community by random_person (@holymartian07).</description>
    <link>https://dev.to/holymartian07</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3162040%2F2433b80d-0cec-4544-90d7-59374ce1d361.png</url>
      <title>DEV Community: random_person</title>
      <link>https://dev.to/holymartian07</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/holymartian07"/>
    <language>en</language>
    <item>
      <title>Azure Active Directory Integration in Angular and React</title>
      <dc:creator>random_person</dc:creator>
      <pubDate>Wed, 14 May 2025 13:40:52 +0000</pubDate>
      <link>https://dev.to/holymartian07/azure-active-directory-integration-in-angular-and-react-e18</link>
      <guid>https://dev.to/holymartian07/azure-active-directory-integration-in-angular-and-react-e18</guid>
      <description>&lt;p&gt;Overview&lt;br&gt;
This guide provides a full walkthrough for implementing authentication and authorization using Azure Active Directory (AAD) in Angular and React applications. It includes architecture flow, access request procedures, official library usage, and security best practices. It is complemented by fully working demo apps as reference implementations.&lt;/p&gt;
&lt;h2&gt;
  
  
  Architecture Flow
&lt;/h2&gt;

&lt;p&gt;1.1 Components Overview&lt;br&gt;
Client App (SPA): Angular or React frontend&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Azure AD: Identity Provider (IdP)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Protected API (Optional): Backend with protected endpoints (Node.js, .NET, etc.)&lt;/p&gt;

&lt;p&gt;1.2 Authentication Flow&lt;br&gt;
User accesses the SPA.&lt;/p&gt;

&lt;p&gt;SPA redirects user to Azure AD for login.&lt;/p&gt;

&lt;p&gt;Azure AD authenticates the user.&lt;/p&gt;

&lt;p&gt;Azure AD returns an ID Token and optionally an Access Token.&lt;/p&gt;

&lt;p&gt;SPA stores the token and uses it to access protected resources or APIs.&lt;/p&gt;
&lt;h2&gt;
  
  
  Requesting Access / App Registration in Azure
&lt;/h2&gt;

&lt;p&gt;2.1 Register Your App in Azure AD&lt;br&gt;
Go to Azure Portal&lt;/p&gt;

&lt;p&gt;Navigate to Azure Active Directory &amp;gt; App registrations&lt;/p&gt;

&lt;p&gt;Click New registration&lt;/p&gt;

&lt;p&gt;Set:&lt;/p&gt;

&lt;p&gt;Name: My Angular App or My React App&lt;/p&gt;

&lt;p&gt;Redirect URI:&lt;/p&gt;

&lt;p&gt;Angular: &lt;a href="http://localhost:4200/" rel="noopener noreferrer"&gt;http://localhost:4200/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;React: &lt;a href="http://localhost:3000/" rel="noopener noreferrer"&gt;http://localhost:3000/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click Register&lt;/p&gt;

&lt;p&gt;2.2 Configure Authentication&lt;br&gt;
Under Authentication tab:&lt;/p&gt;

&lt;p&gt;Enable ID tokens&lt;/p&gt;

&lt;p&gt;Add redirect URIs for local dev and production&lt;/p&gt;

&lt;p&gt;2.3 Expose an API (optional)&lt;br&gt;
In Expose an API:&lt;/p&gt;

&lt;p&gt;Add scopes like api.read, api.write for backend access control&lt;/p&gt;
&lt;h2&gt;
  
  
  Angular Integration
&lt;/h2&gt;

&lt;p&gt;3.1 Install Required Packages&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @azure/msal-browser @azure/msal-angular
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.2 MSAL Configuration (in app.module.ts)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { PublicClientApplication, InteractionType } from '@azure/msal-browser';
import {
  MsalModule,
  MsalInterceptorConfiguration,
  MsalGuardConfiguration,
  MSAL_INSTANCE,
  MSAL_GUARD_CONFIG,
} from '@azure/msal-angular';

export function MSALInstanceFactory() {
  return new PublicClientApplication({
    auth: {
      clientId: '&amp;lt;YOUR-CLIENT-ID&amp;gt;',
      authority: 'https://login.microsoftonline.com/&amp;lt;TENANT-ID&amp;gt;',
      redirectUri: 'http://localhost:4200',
    },
    cache: {
      cacheLocation: 'localStorage',
      storeAuthStateInCookie: true,
    },
  });
}

@NgModule({
  imports: [
    MsalModule.forRoot(
      MSALInstanceFactory(),
      {
        interactionType: InteractionType.Redirect,
        authRequest: { scopes: ['user.read'] },
      },
      {
        interactionType: InteractionType.Redirect,
        protectedResourceMap: new Map([
          ['https://graph.microsoft.com/v1.0/me', ['user.read']],
        ]),
      }
    ),
  ],
})
export class AppModule {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.3 Use MsalGuard for Route Protection&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const routes: Routes = [
  {
    path: 'profile',
    component: ProfileComponent,
    canActivate: [MsalGuard],
  },
];
3.4 Login and Logout
ts
Copy
Edit
import { MsalService } from '@azure/msal-angular';

constructor(private authService: MsalService) {}

login() {
  this.authService.loginRedirect();
}

logout() {
  this.authService.logoutRedirect();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  React Integration
&lt;/h2&gt;

&lt;p&gt;4.1 Install Required Packages&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @azure/msal-browser @azure/msal-react
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4.2 MSAL Configuration&lt;br&gt;
// src/authConfig.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const msalConfig = {
  auth: {
    clientId: '&amp;lt;YOUR-CLIENT-ID&amp;gt;',
    authority: 'https://login.microsoftonline.com/&amp;lt;TENANT-ID&amp;gt;',
    redirectUri: 'http://localhost:3000',
  },
};

export const loginRequest = {
  scopes: ['user.read'],
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4.3 Wrap App with MsalProvider&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { PublicClientApplication } from '@azure/msal-browser';
import { MsalProvider } from '@azure/msal-react';
import { msalConfig } from './authConfig';
import App from './App';

const msalInstance = new PublicClientApplication(msalConfig);

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  &amp;lt;MsalProvider instance={msalInstance}&amp;gt;
    &amp;lt;App /&amp;gt;
  &amp;lt;/MsalProvider&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4.4 Use AuthenticatedTemplate and Login Hooks&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {
  useMsal,
  useIsAuthenticated,
  AuthenticatedTemplate,
  UnauthenticatedTemplate,
} from '@azure/msal-react';

const LoginButton = () =&amp;gt; {
  const { instance } = useMsal();
  return &amp;lt;button onClick={() =&amp;gt; instance.loginRedirect()}&amp;gt;Login&amp;lt;/button&amp;gt;;
};

const LogoutButton = () =&amp;gt; {
  const { instance } = useMsal();
  return &amp;lt;button onClick={() =&amp;gt; instance.logoutRedirect()}&amp;gt;Logout&amp;lt;/button&amp;gt;;
};

const MainContent = () =&amp;gt; {
  const isAuthenticated = useIsAuthenticated();
  return (
    &amp;lt;&amp;gt;
      &amp;lt;AuthenticatedTemplate&amp;gt;
        &amp;lt;h2&amp;gt;Welcome!&amp;lt;/h2&amp;gt;
        &amp;lt;LogoutButton /&amp;gt;
      &amp;lt;/AuthenticatedTemplate&amp;gt;
      &amp;lt;UnauthenticatedTemplate&amp;gt;
        &amp;lt;h2&amp;gt;Please sign in.&amp;lt;/h2&amp;gt;
        &amp;lt;LoginButton /&amp;gt;
      &amp;lt;/UnauthenticatedTemplate&amp;gt;
    &amp;lt;/&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Best Practices
5.1 Token Storage
Use localStorage only if necessary; prefer sessionStorage to reduce token lifetime risks.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Avoid storing tokens in cookies unless HTTP-only and secure.&lt;/p&gt;

&lt;p&gt;5.2 Security Recommendations&lt;br&gt;
Always validate scopes.&lt;/p&gt;

&lt;p&gt;Refresh tokens securely (MSAL handles this internally).&lt;/p&gt;

&lt;p&gt;Use HTTPS for redirect URIs.&lt;/p&gt;

&lt;p&gt;Enable Conditional Access policies in Azure AD for sensitive apps.&lt;/p&gt;

&lt;p&gt;5.3 Environment Management&lt;br&gt;
Never hardcode client IDs in shared codebases&lt;/p&gt;

&lt;p&gt;Use environment variables for:&lt;/p&gt;

&lt;p&gt;Client ID&lt;/p&gt;

&lt;p&gt;Tenant ID&lt;/p&gt;

&lt;p&gt;Redirect URIs&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Resources
Microsoft Identity Platform Docs&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;MSAL.js GitHub&lt;/p&gt;

&lt;p&gt;React + MSAL Sample&lt;/p&gt;

&lt;p&gt;Angular + MSAL Sample&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
