跳至內容
正在從 NextAuth.js v4 遷移?請閱讀 我們的遷移指南.

NetSuite

⚠️
將在 next-auth@5.0.0-beta.18 中發布

資源

設定

回呼 URL

https://example.com/api/auth/callback/netsuite

NetSuite 不支援 http:// 回呼 URL。在本機測試時,您可以使用像 ngrok 這類的服務來取得本機 https URL。

環境變數

AUTH_NETSUITE_ID
AUTH_NETSUITE_SECRET
AUTH_NETSUITE_ACCOUNT_ID

組態

在設定提供者之前,您需要確保已完成以下設定。

  • 建立整合記錄
    • 取消勾選 TBA 驗證流程核取方塊。
    • 勾選 OAuth 2.0 驗證流程核取方塊。
    • 將下方的 回呼 URL 複製並貼到 重新導向 URI 欄位。
    • 然後選取您想要使用的範圍。
      • REST Web Services (rest_webservices) - 存取 REST Web Services。
      • RESTlets(restlets) - 存取 RESTLets。
      • SuiteAnalytics Connect (suiteanalytics_connect) - 存取 SuiteAnalytics Connect。
    • 新增您想要使用的任何原則。
      • 應用程式標誌 (選用) (在要求使用者授權存取您的應用程式時向使用者顯示)。- 同意畫面
      • 應用程式使用條款 (選用) - 包含您應用程式使用條款的 PDF 檔案。- 同意畫面
      • 應用程式隱私權政策 (選用) - 包含您應用程式隱私權政策的 PDF 檔案。- 同意畫面
    • OAuth 2.0 同意原則偏好設定 - 此設定決定使用者是每次登入時都要求授權存取您的應用程式,還是僅在第一次登入時要求,或永不要求。
    • 儲存整合記錄。
    • 整合記錄將用於產生提供者的 clientIdclientSecret請儲存產生的值以供稍後使用

Userinfo RESTLet 處理常式

若要使用此提供者,您需要在您的 NetSuite 執行個體中建立 userinfo RESTlet。我們的 userinfo URL 需要是一個 suitelet 或 RESTLet URL,可為我們提供關於使用者的資訊。最好的辦法是先使用 N/runtime 模組取得基本資訊。- 以下是一個 RESTlet 的範例。請務必部署並啟用「所有角色」的存取權。

請務必部署並使用 URI 的任何用法的外部 RESTLet URL。

/**
 * @NApiVersion 2.1
 * @NScriptType Restlet
 */
define(["N/runtime"],
 (runtime) => {
/**
 * Defines the function that is executed when a GET request is sent to a RESTlet.
 * @param {Object} requestParams - Parameters from HTTP request URL; parameters passed as an Object (for all supported
 *     content types)
 * @returns {string | Object} HTTP response body; returns a string when request Content-Type is 'text/plain'; returns an
 *     Object when request Content-Type is 'application/json' or 'application/xml'
 * @since 2015.2
 */
  const get = (requestParams) => {
    let userObject = runtime.getCurrentUser();
 
    try {
      log.debug({ title: "Payload received:", details: requestParams });
 
      const { id, name, role, location, email, contact } = userObject;
 
      log.audit({ title: "Current User Ran", details: name });
 
      let user = {
        id,
        name,
        role,
        location,
        email,
        contact,
      };
 
      log.debug({ title: "Returning user", details: user });
 
      return JSON.stringify(user);
    } catch (e) {
      log.error({ title: "Error grabbing current user:", details: e });
    }
  };
 
  return {
    get,
  };
);

以上是傳回基本執行階段資訊的範例。請務必建立新的腳本記錄和部署記錄。當您儲存部署記錄時,您將取得 RESTlet 的 URL,我們將其用作 userinfo URL。

範例用法

@auth.ts
import NextAuth from "next-auth"
import NetSuite from "next-auth/providers/netsuite"
 
export const { handlers, auth, signIn, signOut } = NextAuth({
  providers: [
    NetSuite({
      clientId: process.env.AUTH_NETSUITE_ID,
      clientSecret: process.env.AUTH_NETSUITE_SECRET,
      issuer: process.env.AUTH_NETSUITE_ACCOUNT_ID, // EX: TSTDRV1234567 or 81555 for prod, and 1234567-SB1 for Sandbox accounts not "_" use "-".
      // Returns the current user using the N/runtime module. This url can be a suitelet or RESTlet (Recommended)
      // Using getCurrentUser(); So we match this schema returned from this RESTlet in the profile callback. (Required)
      userinfo:
        "https://1234567.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=123&deploy=1",
      // Optional
      prompt: "login", // Required if you want to force the user to login every time.
      scope: "restlets", // Optional defaults to "restlets rest_webservices". Enter the scope(s) you want to use followed by spaces.
    }),
  ],
})