I’m implementing MSAL.js to make an Azure Active Directory login.
The login itself works great, im getting a successful handshake.
But i’m unable to protect routes, and for some reason, getcurrentUser isnot getting called at all.
In addition, when trying to go to a “protected route”, im getting http://localhost:8910/?redirectTo=/admin
in the URL, and a blank white page.
im currently not trying to write or read anything from the db.
index.js
const msalConfig = {
auth: {
clientId: 'xxxx',
authority:
'xxxx',
redirectUri: 'http://localhost:8910/admin',
},
cache: {
cacheLocation: 'sessionStorage', // This configures where your cache will be stored
storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge
},
}
const authClient = new Msal.UserAgentApplication(msalConfig)
ReactDOM.render(
<FatalErrorBoundary page={FatalErrorPage}>
<AuthProvider client={authClient} type="custom">
<RedwoodProvider>
<Routes />
</RedwoodProvider>
</AuthProvider>
</FatalErrorBoundary>,
document.getElementById('redwood-app')
api/src/lib/auth.js
export const getCurrentUser = async (decoded, { type, token }) => {
console.log('get user') // < not called
}
node_modules/@redwoodjs/auth/dist/authClients/custom.js
“use strict”;
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js/object/define-property");
_Object$defineProperty(exports, "__esModule", {
value: true
});
exports.custom = void 0;
var custom = function custom(authClient) {
return {
type: 'custom',
client: authClient,
// restoreAuthState: async () => {
// window.history.replaceState(
// {},
// document.title,
// '/admin'
// )
// },
login: function login() {
var loginRequest = {
scopes: ['openid', 'profile', 'User.Read'],
}
console.log('login')
authClient
.loginPopup(loginRequest)
.then((loginResponse) => {
console.log('id_token acquired at: ' + new Date().toString())
console.log('loginResponse', loginResponse)
if (authClient.getAccount()) {
console.log(authClient.getAccount()) // << working
}
})
.catch((error) => {
console.log(error)
})
},
logout: function logout() {
console.log('log out')
authClient.logout() // << working
},
getToken: function () {
console.log('get token')
return 'string'
}(),
getUserMetadata: function () {
console.log('getUserMetadata')
return authClient.getAccount() || null
}()
};
};
exports.custom = custom;