Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@azure/msal-angular

Package Overview
Dependencies
Maintainers
0
Versions
107
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

  • 3.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
190K
decreased by-2.09%
Maintainers
0
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 05 Nov 2024

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