Skip to content

Commit

Permalink
Fix MSAL silent errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepyfran committed Aug 27, 2024
1 parent f2db7ec commit 7a30195
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export const MsalAuthenticationLive = Layer.effect(
clientId: appConfig.graph.clientId,
redirectUri: appConfig.graph.redirectUri,
},
cache: {
cacheLocation: "localStorage",
temporaryCacheLocation: "sessionStorage",
storeAuthStateInCookie: true,
},
}),
);

Expand Down Expand Up @@ -67,7 +72,7 @@ export const MsalAuthenticationLive = Layer.effect(
});

const authResult = yield* Effect.tryPromise({
try: () => app.loginPopup(authRequest),
try: () => app.acquireTokenPopup(authRequest),
catch: () => AuthenticationError.Unknown,
});

Expand All @@ -79,6 +84,9 @@ export const MsalAuthenticationLive = Layer.effect(
const app = yield* msalAppRef.get;

if (cachedCredentials.providerSpecific._tag !== "MSAL") {
yield* Effect.logError(
"Cached credentials are not MSAL-specific, cannot connect silently",
);
return yield* Effect.fail(AuthenticationError.WrongCredentials);
}

Expand All @@ -100,6 +108,11 @@ export const MsalAuthenticationLive = Layer.effect(
: AuthenticationError.Unknown;
},
}).pipe(
Effect.tap((authInfo) =>
Effect.log(
`Successfully connected silently using MSAL, new token expiration ${authInfo.expiresOn}`,
),
),
Effect.tapError((e) =>
Effect.logError(`Error while connecting silently: ${e}`),
),
Expand Down
4 changes: 4 additions & 0 deletions packages/services/app-init/src/app-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const make = Effect.gen(function* () {
init: Effect.gen(function* () {
const allProviderStates = yield* retrieveAllProviderArgs(localStorage);

yield* Effect.log(
`Re-initializing ${allProviderStates.length} providers on startup`,
);

return yield* Effect.all(
allProviderStates.map((providerStartArgs) =>
Effect.gen(function* () {
Expand Down

0 comments on commit 7a30195

Please sign in to comment.