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 | 7x 7x 7x 7x 7x | import { Injectable } from "@angular/core";
import { MsalService } from "@azure/msal-angular";
import { map, of } from "rxjs";
import { LoginProvider } from "../login-service.interface";
import { MSALInstanceFactory, MSALScopes, loginConfig } from "./login-config";
@Injectable({
providedIn: "root",
})
export class MSALLoginService implements LoginProvider {
constructor (private msalService: MsalService) {}
init () {
const msalInstance = MSALInstanceFactory();
Iif(!msalInstance)
return of();
this.msalService.instance = msalInstance;
return this.msalService.initialize();
}
getToken () {
return this.msalService.acquireTokenSilent({scopes: MSALScopes}).pipe(
map(res =>
res.accessToken
));
}
handleSuccess (callback?: (() => void) | undefined) {
this.msalService.instance
.handleRedirectPromise()
.then(resp => {
const account = resp?.account;
Iif(account)
this.msalService.instance.setActiveAccount(account);
callback?.();
});
}
async login () {
sessionStorage.removeItem("msal.interaction.status");
const request = { scopes: ["User.Read"] };
try {
if (loginConfig.method === "redirect") {
this.msalService.instance.loginRedirect(request);
} else {
const auth = await this.msalService.instance.loginPopup(request);
this.msalService.instance.setActiveAccount(auth.account);
}
} catch (error) {
console.error("Login failed", error);
}
}
logout () {
this.msalService.logout();
sessionStorage.removeItem("msal.interaction.status");
window.location.href = "/";
}
getAccount () {
const account = this.msalService.instance.getActiveAccount();
if (account) {
console.log(account);
return {
username: account.username,
name: account.name,
key: account.localAccountId,
providedBy: "msal",
};
} else {
console.warn("No active account found");
return undefined;
}
}
getProvider = () => "msal";
} |