0

我们如何获取使用 Microsoft Identity 连接到 Azure 应用服务的用户的电子邮件?

我们的前端构建在 React 之上,我们的后端构建在 Node.js 之上。

谢谢,克里斯托夫

4

1 回答 1

0

/.auth/me最后,调用REST 端点从响应数据中检索用户声明就足够了。

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": []
                    };
                });
}
于 2021-07-13T06:49:48.117 回答