我正在学习React应用程序中每个环境的MSAL身份验证。
authConfig.ts
import { Configuration } from "@azure/msal-browser";
import { envConfig } from "./config";
// Config object to be passed to Msal on creation
export const msalConfig: Configuration = {
auth: {
clientId: envConfig.msalConfig.uiClientId,
authority: `https://login.microsoftonline.com/${envConfig.msalConfig.tenantId}`,
redirectUri: "/",
postLogoutRedirectUri: "/"
}
};
// scopes
export const loginRequest = {
scopes: envConfig.msalConfig.scopes
};
// endpoints
export const config = {
endpoint: `${envConfig.msalConfig.serviceBaseUrl}/api/v1/items`
};
IEnvConfig:
export interface IEnvConfig {
env: string;
msalConfig: {
uiClientId: string;
authority: string;
apiClientId: string;
tenantId: string;
serviceBaseUrl: string;
scopes: string[];
};
}
prod.ts
import { IEnvConfig } from "../config";
export const prod: IEnvConfig = {
env: "PROD",
msalConfig: {
uiClientId: "",
authority: "https://login.microsoftonline.com/",
apiClientId: "",
tenantId: "tenantId",
serviceBaseUrl: "https://xxx.azurewebsites.net",
scopes: []
},
};
内部
export const int: IEnvConfig = {
env: "INT",
msalConfig: {
uiClientId: "",
authority: "https://login.microsoftonline.com/",
apiClientId: "",
tenantId: "tenantId",
serviceBaseUrl: "https://xxx.azurewebsites.net",
scopes: []
},
};
用户名:
export const ua: IEnvConfig = {
env: "UA",
msalConfig: {
uiClientId: "",
authority: "https://login.microsoftonline.com/",
apiClientId: "",
tenantId: "tenantId",
serviceBaseUrl: "https://xxx.azurewebsites.net",
scopes: []
},
};
如何根据环境选择配置值?如何检查我所处的环境?
1条答案
按热度按时间lb3vh1jj1#
而不是每个env声明一个对象(
ua, int, prod
等),你应该只使用一个全局对象(即config
),并在构建过程中设置其值。查看Customizing Environment Variables for Arbitrary Build Environments和Adding Custom Environment Variables以获取更多信息。