All files / src/app/components/login login.component.ts

25% Statements 9/36
0% Branches 0/5
0% Functions 0/12
25% Lines 8/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 891x 1x 1x 1x 1x   1x 1x                                                         1x                                                                                                        
import { CommonModule } from "@angular/common";
import { HTTP_INTERCEPTORS } from "@angular/common/http";
import { Component, OnInit } from "@angular/core";
import { ActivatedRoute, Router, RouterModule } from "@angular/router";
import { MSAL_INSTANCE, MsalBroadcastService, MsalGuard, MsalInterceptor, MsalService } from "@azure/msal-angular";
import { Account } from "@shared/login/account.interface";
import { LoginService } from "@shared/login/login.service";
import { MSALInstanceFactory } from "@shared/login/msal-login/login-config";
 
 
@Component({
    selector: "app-login",
    standalone: true,
    providers: [
        {
            provide: HTTP_INTERCEPTORS,
            useClass: MsalInterceptor,
            multi: true,
        },
        {
            provide: MSAL_INSTANCE,
            useFactory: () => MSALInstanceFactory(),
        },
 
        MsalService,
        MsalGuard,
        MsalBroadcastService,
    ],
    imports: [
        CommonModule,
        RouterModule,
    ],
  
    templateUrl: "./login.component.html",
})
 
export class LoginComponent implements OnInit {
    constructor (
        private loginService: LoginService,
        private readonly route: ActivatedRoute,
        private readonly router: Router
    ) {
    }
    ngOnInit () {
        this.loginService.init();
 
        this.route.params.subscribe((param) => this.handleActions(param["action"]));
 
        Iif(this.loginService.isLoggedIn())
            this.router.navigate(["/home"]);
    }
 
    handleActions (action: string | undefined) {
        this.loginService.init()
            .subscribe(() => {
                switch (action) {
                case "success":
                    this.loginService.handleSuccess(() => {
                        const redirURL = sessionStorage.getItem("afterlogin-redirect");
                        if(redirURL) {
                            sessionStorage.removeItem("afterlogin-redirect");
                            this.router.navigateByUrl(redirURL).then(() => location.reload());
                        } else {
                            this.router.navigate(["/home"]).then(() => location.reload());
                        }
                    });
                    break;
                case "logout":
                    this.loginService.logout();
                    break;
                }
            });
    }
 
 
    login () {
        this.loginService.login();
    }
 
    logout () {
        this.loginService.logout();
    }
 
    getAccount (): Account {
        const account = this.loginService.getAccount()!;
        console.log(account);
        return account;
    }
}