Socket
Socket
Sign inDemoInstall

@azure/msal-angular

Package Overview
Dependencies
Maintainers
3
Versions
105
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@azure/msal-angular

Microsoft Authentication Library for Angular


Version published
Weekly downloads
184K
increased by4.62%
Maintainers
3
Weekly downloads
 
Created

What is @azure/msal-angular?

@azure/msal-angular is a library that allows Angular applications to authenticate users with Microsoft Identity Platform and obtain tokens to call Microsoft APIs such as Microsoft Graph or any API registered with the Microsoft identity platform.

What are @azure/msal-angular's main functionalities?

User Authentication

This code demonstrates how to set up the MSAL instance in an Angular application to enable user authentication with Microsoft Identity Platform.

import { MsalModule, MsalService, MSAL_INSTANCE } from '@azure/msal-angular';
import { PublicClientApplication } from '@azure/msal-browser';

export function MSALInstanceFactory() {
  return new PublicClientApplication({
    auth: {
      clientId: 'your-client-id',
      authority: 'https://login.microsoftonline.com/your-tenant-id',
      redirectUri: 'http://localhost:4200'
    }
  });
}

@NgModule({
  imports: [
    MsalModule
  ],
  providers: [{
    provide: MSAL_INSTANCE,
    useFactory: MSALInstanceFactory
  }, MsalService]
})
export class AppModule { }

Token Acquisition

This code demonstrates how to log in a user and acquire an access token silently for calling Microsoft Graph API or other APIs.

import { MsalService } from '@azure/msal-angular';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'
})
export class AppComponent {
  constructor(private authService: MsalService) {}

  login() {
    this.authService.loginPopup().subscribe(response => {
      console.log('Login successful', response);
    }, error => {
      console.error('Login failed', error);
    });
  }

  getToken() {
    this.authService.acquireTokenSilent({
      scopes: ['user.read']
    }).subscribe(response => {
      console.log('Token acquired', response.accessToken);
    }, error => {
      console.error('Token acquisition failed', error);
    });
  }
}

Guarding Routes

This code demonstrates how to protect routes in an Angular application using MsalGuard to ensure that only authenticated users can access certain routes.

import { MsalGuard } from '@azure/msal-angular';

const routes: Routes = [
  { path: 'profile', component: ProfileComponent, canActivate: [MsalGuard] }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

Other packages similar to @azure/msal-angular

FAQs

Package last updated on 17 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc