0

How can we get the email of a user connected to an Azure App Service with Microsoft Identity?

Our front-end is built on top of React and our back-end is built on top of Node.js.

Thanks, Christophe

1 Answers1

0

Finally, it was enough to call the /.auth/me REST endpoint to retrieve user claims from the response data.

import axios from "axios";

const _ = require("lodash");

export function hasRoleReaders(userDetails) {
    return hasRole(userDetails, "readers");
}

export function hasRoleWriters(userDetails) {
    return hasRole(userDetails, "writers");
}

function hasRole(userDetails, expectation) {
    const found = _.find(userDetails.roles, function (role) {
        return role.val === expectation;
    });

    return found !== undefined;
}

export async function getUserDetails() {
    return axios.get("/.auth/me")
                .then(response => {
                    const datum = response.data[0];

                    const userId = datum.user_id;
                    const userClaims = datum.user_claims;
                    const name = _.find(userClaims, function (userClaim) {
                        return userClaim["typ"] === "name";
                    });
                    const roles = _.filter(userClaims, function (userClaim) {
                        return userClaim["typ"] === "roles";
                    });

                    return {
                        "userId": userId,
                        "name": name,
                        "userClaims": userClaims,
                        "roles": roles
                    };
                })
                .catch(reason => {
                    console.log(reason);

                    return {
                        "userId": "na",
                        "name": "na",
                        "userClaims": [],
                        "roles": []
                    };
                });
}