Giter VIP home page Giter VIP logo

angular-msal's Introduction

Angular MSAL

About this package

When I started using msal with Angular I used msal-angular and did some changes in the library. Later Microsoft created their own library and msal-angular wasn't maintained anymore. I did try to use @azure/msal-angular but it lacks support for Angular 6+ and didn't compile to es5. That's why I deceided to clone @azure/msal and fix some things. When [email protected] came out I upgraded the package and removed all code I didn't understand and thought I wouldn't need. I'm sure I missed things so if thing does not work contact me or better: create a pull request. if @azure/msal-angular will be usefull I'll stop maintaining this library.

Changes

Installation

npm install angular-msal --save

Usage

This is how I use the package: I have a userService that has a function: TryToGetUser, which tries to get the current user from my backend (using the tokens ObjectId which I make sure is equal to my User.Id)

Add the MsalModule and HttpIntercepter in app.module.ts

export function baseUri() {
  return window.location.protocol + '//' + window.location.host + '/';
}

@NgModule({
  imports: [
    MsalModule.forRoot({
      clientID: environment.clientId,
      authority: environment.authority + environment.userFlowTeacher,
      validateAuthority: true,
      cacheLocation: 'localStorage',
      postLogoutRedirectUri: baseUri,
      redirectUri: baseUri,
      navigateToLoginRequestUrl: true,
      popUp: false,
      consentScopes: environment.scopes,
      logger: loggerCallback,
      correlationId: 'correlationId1234',
      level: LogLevel.Info,
      piiLoggingEnabled: true
    }),
  ],
providers: [UserService,
    { provide: HTTP_INTERCEPTORS, useClass: MsalInterceptor, multi: true }
  ]
@Injectable()
export class UserService {
    cachedUser: User;
    
    constructor(private authService: MsalService, private http: HttpClient) {
        // register redirect call back (only for needed for loginRedirect)
        this.authService.handleRedirectCallback(() => {  
          router.navigate(['myProfile']);
        });
    }
    public tryToGetUser() {
        if (this.authService.getAccount()) {
            return this.getUser();
        }
        return of(null);
    }

    public getUser() {
        return this.http.get<User>(`User/loggedinuser`).pipe(tap(user => {
            this.cachedUser = user;
        }));
    }
}

login.component.ts

export class LoginPageComponent {
  constructor(private authService: MsalService, public userService: UserService, private router: Router) {

  loginRedirect = () => this.authService.loginRedirect();

  loginPopup = () => {
      this.authService.loginPopup().then(_ => {
        this.userService.tryToGetUser().subscribe(_ => {
           this.router.navigate(['myProfile']);
        });
      });
    }

To logout use

this.msalService.logout();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.