<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Claire Wei on Medium]]></title>
        <description><![CDATA[Stories by Claire Wei on Medium]]></description>
        <link>https://medium.com/@wei30172?source=rss-a643e8fc28e2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*nIO14gV3G93BHvwWztOGqg.jpeg</url>
            <title>Stories by Claire Wei on Medium</title>
            <link>https://medium.com/@wei30172?source=rss-a643e8fc28e2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 20:05:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@wei30172/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[使用 Next.js 14 Middleware 整合 NextAuth.js v5 以及多國語系 (i18n)]]></title>
            <link>https://medium.com/wei30172/%E4%BD%BF%E7%94%A8-next-js-14-middleware-%E6%95%B4%E5%90%88-nextauth-js-v5-%E4%BB%A5%E5%8F%8A%E5%A4%9A%E5%9C%8B%E8%AA%9E%E7%B3%BB-i18n-d6fac6155fda?source=rss-a643e8fc28e2------2</link>
            <guid isPermaLink="false">https://medium.com/p/d6fac6155fda</guid>
            <category><![CDATA[nextauth]]></category>
            <category><![CDATA[i18n]]></category>
            <category><![CDATA[nextjs]]></category>
            <dc:creator><![CDATA[Claire Wei]]></dc:creator>
            <pubDate>Thu, 24 Apr 2025 19:45:14 GMT</pubDate>
            <atom:updated>2025-04-28T20:08:03.856Z</atom:updated>
            <content:encoded><![CDATA[<p>我們將在 Next.js 14 的 App Router 架構中，實現可擴充的 Middleware 管理系統，透過 Middleware 同時整合 NextAuth.js v5 的身分驗證機制 與 next-intl 的 i18n 多語系處理，並展示組件的多國語系顯示與表單驗證錯誤訊息翻譯邏輯。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*P2MNhcXAh8F0WbN4" /></figure><p>完整專案範例可參考<br><a href="https://github.com/wei30172/nextauth-v5-mongodb-typescript-example">https://github.com/wei30172/nextauth-v5-mongodb-typescript-example</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*zRI1mFBDJEN-czsr" /></figure><p><strong>一、專案目錄結構</strong></p><pre>.<br>├── app<br>│   ├── [locale]/(root)<br>│   │   └── _components<br>│   │   │   └── signin-form.tsx<br>│   │   │   ......<br>│   │   └── signin<br>│   │   │   └── page.tsx<br>│   │   │   ......<br>│   │   └── layout.tsx<br>├── components<br>│   ├── locale-select.tsx<br>│   ├── locale-switcher.tsx<br>├── i18n<br>│   └── request.ts<br>│   └── routing.ts<br>├── lib<br>│   └── actions<br>│   │   └── signin-with-credentials.ts<br>│   │   │   ......<br>│   └── validations<br>│   │   └── auth.ts<br>├── messages<br>│   └── en.json<br>│   └── zh.json<br>├── middleware<br>│   └── chain.ts<br>│   └── with-auth-middleware.ts<br>│   └── with-intl-middleware.ts<br>└── middleware.ts<br>└── next.config.mjs</pre><p><strong>二、實作步驟</strong></p><p><strong>1.使用安裝指令</strong></p><pre>npm install next-intl</pre><p><strong>2. 調整專案結構</strong>，將頁面移至[locale]資料夾中</p><pre>├── app<br>│   ├── <strong>[locale]</strong>/(root)<br>│   │   └── _components<br>│   │   │   └── signin-form.tsx<br>│   │   │   ......<br>│   │   └── signin<br>│   │   │   └── page.tsx<br>│   │   │   ......<br>│   │   └── layout.tsx</pre><p><strong>3. 建立翻譯文件</strong> (messages/en.json &amp; zh.json)</p><p>用於定義各語系對應文字，建議使用巢狀方式管理，對應每個元件的區塊，所有語系的 key 結構必須一致，否則在切換語言時可能出現缺漏或顯示錯誤以 SignInForm 為例：</p><p>(1) messages/en.json 表示英文顯示內容。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/546a89a0ad4a46d53ae863b54e3f5508/href">https://medium.com/media/546a89a0ad4a46d53ae863b54e3f5508/href</a></iframe><p>(2) messages/zh.json 表示中文顯示內容。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7cb52996a4d8ae480d5ad81c8659ba58/href">https://medium.com/media/7cb52996a4d8ae480d5ad81c8659ba58/href</a></iframe><p><strong>4. 配置 i18n 設定</strong> (next.config.mjs、i18n/routing.ts &amp; request.ts)</p><p>(1) next.config.mjs 透過官方插件 createNextIntlPlugin，將多國語系的處理整合進 Next.js 應用的設定中。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d72dd24b61b60c96f88f496eea2494bb/href">https://medium.com/media/d72dd24b61b60c96f88f496eea2494bb/href</a></iframe><p>(2) i18n/routing.ts，defineRouting 處理 locale 前綴導向，其中 locales 可依需求加入更多語系（例如 “ja”, “fr”）。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c0d61f01b0bc3cad80acced6c1b0852/href">https://medium.com/media/2c0d61f01b0bc3cad80acced6c1b0852/href</a></iframe><p>(3) i18n/request.ts，其中 getRequestConfig 提供語系動態匯入，依語系回傳對應 JSON 檔。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ada10fbdd7d8a4fb6555812365e1d39/href">https://medium.com/media/6ada10fbdd7d8a4fb6555812365e1d39/href</a></iframe><p><strong>5. Middleware 設計與整合 </strong>(middleware/chain.ts &amp; with-auth-middleware.ts &amp; with-intl-middleware.ts、middleware.ts)</p><p>(1) middleware/chain.ts 採用函式鏈接模式，允許多個函式以序列方式依序執行，方便模組化各種 middleware。其中 X-Middleware-Next 是 Next.js 用來標示 request 是否應繼續傳遞的標頭，如果呼叫了 NextResponse.redirect() 或 NextResponse.rewrite() 等行為，中間件鏈會中斷，後續 middleware 不會繼續執行。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f44857b8bef22dacdf620cdcafa77b29/href">https://medium.com/media/f44857b8bef22dacdf620cdcafa77b29/href</a></iframe><p>(2) middleware/with-auth-middleware.ts 若使用者未登入且進入保護路由，則自動導回 /signin，已登入者若訪問 /signup 等驗證頁面也會導向指定頁面。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70f2c7c19f0d7b99727cc72714b693dd/href">https://medium.com/media/70f2c7c19f0d7b99727cc72714b693dd/href</a></iframe><p>(3) middleware/with-intl-middleware.ts 套用 next-intl 的 middleware 用以自動處理語系切換與路由前綴。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c101634a45ec86be4ab816f72be6afda/href">https://medium.com/media/c101634a45ec86be4ab816f72be6afda/href</a></iframe><p>(4) middleware.ts 中將權限驗證與 i18n 處理統一以鏈式方式呼叫。 其中的auth 是 NextAuth.js v5 提供的 API，用來自動處理 session 與解析 JWT，注入 req.auth。</p><p>auth: withAuthWrapper 為什麼不寫在 with-auth-middleware.ts？因為必須先處理 NextAuth 的 session/init，再由我們自定義的 middleware 接手驗證與邏輯處理。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b1976571cabb7d7ba91d3602b425a94/href">https://medium.com/media/0b1976571cabb7d7ba91d3602b425a94/href</a></iframe><p><strong>6. 傳遞 locale 參數</strong>(app/[locale]/(root)/layout.tsx)</p><p>locale 是從 URL 的動態參數取得（例如 /en/、/zh/）。透過這個參數，我們可以根據使用者選擇的語言載入對應的翻譯內容。若收到的 locale 不在我們支援的語言清單中（定義於 routing.locales），則會使用 notFound() 終止渲染。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a74fa5dbd56b929d0f31827de03ed39/href">https://medium.com/media/8a74fa5dbd56b929d0f31827de03ed39/href</a></iframe><p>⚠️ 注意：[locale] 是 App Router 的動態路由名稱。若你將資料夾命名為其他名稱（如 [lang]、[language]），則在 layout.tsx 中對應接收的參數名稱也必須一致，例如：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9dfa3b054fcb4ff21e5a625befaa30a9/href">https://medium.com/media/9dfa3b054fcb4ff21e5a625befaa30a9/href</a></iframe><p><strong>7. 使用設定完成的多國語系</strong></p><p>(1) 在元件中可以透過 useTranslations Hook 取得翻譯函式並使用 key 顯示對應文字 ，以 SignInForm 組件為例，會從 messages/en.json 或 messages/zh.json 中載入 SignInForm.ui.email 對應文字。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b525b59fe047c4cb49e55bb53196678/href">https://medium.com/media/3b525b59fe047c4cb49e55bb53196678/href</a></iframe><p>(2) 對於非同步的情況，可以使用 await getTranslations 函數，以 signInWithCredentials Server Action 為例，可以根據使用者目前的語系，回傳對應語言的成功/錯誤訊息。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ecb9f283cf9c07e89a5cd104ff67cca6/href">https://medium.com/media/ecb9f283cf9c07e89a5cd104ff67cca6/href</a></iframe><p><strong>8. zod 表單驗證多國語系</strong> (lib/validations/auth.ts)</p><p>在表單驗證中，我們可結合 zod + next-intl 傳入翻譯函式 t，使驗證訊息也支援多國語系。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2baad5d424c2ca1644b368813059bfdf/href">https://medium.com/media/2baad5d424c2ca1644b368813059bfdf/href</a></iframe><p>(1) 在 SignInForm 組件中使用驗證 schema，搭配 react-hook-form 與 zodResolver，可以實現動態翻譯錯誤訊息顯示。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc474c6f5b061b3813a2c6dfa794c5a4/href">https://medium.com/media/bc474c6f5b061b3813a2c6dfa794c5a4/href</a></iframe><p>(2) 在 signInWithCredentials Server Action 中執行 Schema 驗證，也可以使用相同的驗證邏輯做驗證。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d6b551d82d57ad49ae70a7c3dffa006/href">https://medium.com/media/6d6b551d82d57ad49ae70a7c3dffa006/href</a></iframe><p><strong>9. 語系切換組件</strong> (components/locale-switcher.tsx &amp; locale-select.tsx)</p><p>(1) components/locale-switcher.tsx 使用 next-intl 提供的 useLocale Hook 取得目前語系（例如 en 或 zh）。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6ae17e3d4083d067fd720289ee6eda4/href">https://medium.com/media/b6ae17e3d4083d067fd720289ee6eda4/href</a></iframe><p>(2) components/locale-select.tsx 使用 usePathname、useSearchParams 來取得目前頁面資訊與查詢參數，當使用者選擇新的語系時會透過 router.replace 將使用者導向相同的頁面但語系不同的路由（例如：/en/signin → /zh/signin）。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a167413d22afbd7796b7776cadf404a7/href">https://medium.com/media/a167413d22afbd7796b7776cadf404a7/href</a></iframe><p><strong>10. 讓 &lt;Link&gt; 帶上 locale</strong></p><p>在使用 Next.js 時，預設的 &lt;Link&gt; 元件（next/link）在跳轉時只帶路徑，不會自動帶上目前的 locale，這可能導致頁面跳轉後語言切回預設語系，造成使用者體驗不一致。例如，原本在中文頁面 /zh/signin，點擊跳轉到 /reset，因為沒有帶上 locale，會直接變成 /reset（預設英文版），而不是 /zh/reset。</p><p>為了讓跳轉時保留使用中的語言，我們可以改用 @/i18n/routing 中 next-intl 內建的 &lt;Link&gt;，這個 &lt;Link&gt; 會自動把當前的 locale 加到跳轉路徑上。修改方式很簡單：</p><p>原本</p><pre>import Link from &quot;next/link&quot;</pre><p>修改後</p><pre>import { Link } from &quot;@/i18n/routing&quot;</pre><p>使用範例（結合 &lt;Button&gt; 和 &lt;Link&gt;），這樣點擊 &lt;Button&gt; 時，就會根據目前使用中的語言，跳轉到正確的語系路徑（例如 /zh/reset 或 /en/reset）。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b2ef1d6df17b2af62851bea8c82648b4/href">https://medium.com/media/b2ef1d6df17b2af62851bea8c82648b4/href</a></iframe><p><strong>三、重點整理</strong></p><ul><li><strong>巢狀 messages 結構</strong>：<br>翻譯檔案（如 messages/en.json）建議採巢狀結構（例如 SignInForm.ui.email），可對應元件模組結構，提升語系資料的可讀性與維護效率，也利於日後抽取與翻譯管理。</li><li><strong>middleware.ts 的設計彈性</strong>：<br>採用 chain() 封裝多個 middleware，使身份驗證和 i18n 可以依照順序整合，並保留彈性以擴充其他功能。</li><li><strong>layout.tsx 中 locale 邏輯</strong>：<br>透過 URL 參數取得的 locale 必須為有效語系之一，另外若 [locale] 資料夾命名更改（如改為 [lang]），則參數名稱也必須對應更改。</li><li><strong>useTranslations 與 getTranslations 的使用場景差異</strong>：<br>useTranslations() 適用於客戶端元件（Client Components），可搭配 React hooks 在 CSR 中即時更新 UI 文字；而 getTranslations() 則適用於伺服器端元件（Server Components）或 Server Actions，能在 SSR 階段根據當前語系載入對應的翻譯內容，常用於 layout 或 server 處理流程。</li><li><strong>Zod 驗證整合多語系錯誤訊息</strong>：<br>可結合 next-intl 將翻譯函式傳入 schema 中，搭配 react-hook-form 使用 zodResolver()，即可在 UI 層顯示動態的多語系錯誤訊息。</li></ul><p><strong>四、參考資料</strong></p><ul><li><strong>next-intl 官方文件</strong>－<br><a href="https://next-intl.dev/docs/getting-started/app-router">https://next-intl.dev/docs/getting-started/app-router</a></li><li><strong>NextAuth.js 官方文件</strong>－<br><a href="https://next-auth.js.org/getting-started/example">https://next-auth.js.org/getting-started/example</a></li><li><strong>Next.js Middleware 官方說明</strong>－<br><a href="https://nextjs.org/docs/app/building-your-application/routing/middleware">https://nextjs.org/docs/app/building-your-application/routing/middleware</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d6fac6155fda" width="1" height="1" alt=""><hr><p><a href="https://medium.com/wei30172/%E4%BD%BF%E7%94%A8-next-js-14-middleware-%E6%95%B4%E5%90%88-nextauth-js-v5-%E4%BB%A5%E5%8F%8A%E5%A4%9A%E5%9C%8B%E8%AA%9E%E7%B3%BB-i18n-d6fac6155fda">使用 Next.js 14 Middleware 整合 NextAuth.js v5 以及多國語系 (i18n)</a> was originally published in <a href="https://medium.com/wei30172">ClaireWei</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[全棧身份認證實作：使用 Next.js、NextAuth.js 和 MongoDB－1專案介紹]]></title>
            <link>https://medium.com/wei30172/%E5%85%A8%E6%A3%A7%E8%BA%AB%E4%BB%BD%E8%AA%8D%E8%AD%89%E5%AF%A6%E4%BD%9C-%E4%BD%BF%E7%94%A8-next-js-nextauth-js-%E5%92%8C-mongodb-1%E5%B0%88%E6%A1%88%E4%BB%8B%E7%B4%B9-3d2a3ec1d3b6?source=rss-a643e8fc28e2------2</link>
            <guid isPermaLink="false">https://medium.com/p/3d2a3ec1d3b6</guid>
            <category><![CDATA[nextauth]]></category>
            <category><![CDATA[nextjs]]></category>
            <dc:creator><![CDATA[Claire Wei]]></dc:creator>
            <pubDate>Thu, 14 Dec 2023 03:31:45 GMT</pubDate>
            <atom:updated>2025-04-28T23:38:49.573Z</atom:updated>
            <content:encoded><![CDATA[<p>專案概述和各單元內容概要。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*18xvabSCVZE7Xe_K" /></figure><p><strong>1–1. 專案概述</strong></p><p>我們將使用 Next.js 13、NextAuth.js、MongoDB、TypeScript，以及 Server Actions 來構建一個全棧身份認證專案，了解如何安全地處理用戶密碼，以及如何根據用戶身份實施路由保護。此外我們將透過 MongoDB 進行數據存儲，並利用 Mongoose 來設計和操作數據模型。專案中還包含了對 shadcn/ui 組件的實際應用，基於 Tailwind CSS 的組件不僅具有高度的可客製性，還能根據需求靈活調整。專案的功能特點包含：</p><ul><li>OAuth：可透過 Google 進行登錄。</li><li>註冊功能：可用名稱、電子郵件、密碼註冊帳戶。</li><li>憑證登入：使用電子郵件和密碼進行登入。</li><li>個人資料編輯：變更用戶資料。</li><li>修改密碼：安全地更新密碼。</li><li>安全路由：僅供已登入的用戶或管理員瀏覽特定頁面。</li></ul><p><strong>1–2. 各單元內容概要</strong></p><p>本系列文章包含以下主題：</p><p><strong>第2單元：</strong><a href="https://medium.com/p/c33c20dbd812"><strong>專案建立</strong></a>－初始化和設置 Next.js 專案，包括基本配置和套件安裝</p><p><strong>第3單元：</strong><a href="https://medium.com/p/8104355e7b8a"><strong>介面配置</strong></a>－配置和設計用戶界面，包括頁面佈局和組件創建</p><p><strong>第4單元：</strong><a href="https://medium.com/p/e003492ac318"><strong>NextAuth 設定</strong></a>－使用 NextAuth.js 進行身份認證，包括配置和使用者數據處理</p><p><strong>第5單元：</strong><a href="https://medium.com/p/cae0f2b2952f"><strong>註冊與登入</strong></a>－用戶註冊和登入的實現方式，包括表單處理和驗證</p><p><strong>第6單元：</strong><a href="https://medium.com/p/d7d22aa18250"><strong>Google OAuth</strong></a>－整合 Google OAuth 作為登入方法，並處理第三方認證</p><p><strong>第7單元：</strong><a href="https://medium.com/p/8b20d5b9736b"><strong>用戶登出</strong></a>－用戶登出功能的實現</p><p><strong>第8單元：</strong><a href="https://medium.com/p/8bf0beea0769"><strong>MongoDB 配置</strong></a>－MongoDB 數據庫的配置和整合，以及數據模型設計</p><p><strong>第9單元：</strong><a href="https://medium.com/p/a865141be68b"><strong>Google OAuth 與 mongoose</strong></a>－結合 Google OAuth 和 mongoose 管理用戶數據</p><p><strong>第10單元：</strong><a href="https://medium.com/p/5b852c96f7bb"><strong>用戶資料更新</strong></a>－實現用戶資料的更新，包括創建表單和處理更新請求</p><p><strong>第11單元：</strong><a href="https://medium.com/p/153758938315"><strong>帳號註冊</strong></a>－使用電子郵件和密碼進行帳號註冊的流程</p><p><strong>第12單元：</strong><a href="https://medium.com/p/bde177254036"><strong>帳號登入</strong></a>－使用電子郵件和密碼進行帳號登入的方法</p><p><strong>第13單元：</strong><a href="https://medium.com/p/0ea6ef5c57c6"><strong>密碼變更</strong></a>－實現用戶的密碼更改功能，包括創建界面和處理密碼更新請求</p><p><strong>第14單元：</strong><a href="https://medium.com/p/dfaa009415ed"><strong>路由權限驗證</strong></a>－使用 Middleware 設置路由保護機制，根據用戶身份限制訪問</p><p><strong>第15單元：</strong><a href="https://medium.com/p/cb02ad1b4d40"><strong>深色模式切換</strong></a>－配置深色模式切換功能</p><p><strong>第16單元：</strong><a href="https://medium.com/p/583dc5e7015b"><strong>專案佈署</strong></a>－將專案佈署到線上平台(Vercel)</p><p><strong>延伸功能：</strong><a href="https://medium.com/p/d6fac6155fda"><strong>NextAuth.js v5 &amp; i18n</strong></a>－使用 Next.js 14 Middleware 整合 NextAuth.js v5 以及多國語 (i18n)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d2a3ec1d3b6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/wei30172/%E5%85%A8%E6%A3%A7%E8%BA%AB%E4%BB%BD%E8%AA%8D%E8%AD%89%E5%AF%A6%E4%BD%9C-%E4%BD%BF%E7%94%A8-next-js-nextauth-js-%E5%92%8C-mongodb-1%E5%B0%88%E6%A1%88%E4%BB%8B%E7%B4%B9-3d2a3ec1d3b6">全棧身份認證實作：使用 Next.js、NextAuth.js 和 MongoDB－1專案介紹</a> was originally published in <a href="https://medium.com/wei30172">ClaireWei</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[全棧身份認證實作：使用 Next.js、NextAuth.js 和 MongoDB－16. 專案佈署]]></title>
            <link>https://medium.com/wei30172/%E5%85%A8%E6%A3%A7%E8%BA%AB%E4%BB%BD%E8%AA%8D%E8%AD%89%E5%AF%A6%E4%BD%9C-%E4%BD%BF%E7%94%A8-next-js-nextauth-js-%E5%92%8C-mongodb-16-%E5%B0%88%E6%A1%88%E4%BD%88%E7%BD%B2-583dc5e7015b?source=rss-a643e8fc28e2------2</link>
            <guid isPermaLink="false">https://medium.com/p/583dc5e7015b</guid>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[nextauth]]></category>
            <dc:creator><![CDATA[Claire Wei]]></dc:creator>
            <pubDate>Wed, 13 Dec 2023 07:55:47 GMT</pubDate>
            <atom:updated>2025-04-29T00:47:52.579Z</atom:updated>
            <content:encoded><![CDATA[<p>將專案佈署到 Vercel，以及更新相關 URL 設置。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*gfbivSBph5UVALDS" /></figure><p><strong>16–1. 推送專案到 GitHub</strong></p><ul><li>在 GitHub 上創建新的 repository。</li><li>執行以下 Git 指令推送本地專案：</li></ul><p>初始化 Git repository</p><pre>git init</pre><p>添加所有文件</p><pre>git add .</pre><p>提交變更</p><pre>git commit -m &quot;Initial commit&quot;</pre><p>將分支重新命名為 main</p><pre>git branch -M main</pre><p>設定遠程 Repository</p><pre>git remote add origin https://github.com/username/repository-name.git</pre><p>推送到遠程 main 分支</p><pre>git push -u origin main</pre><p><strong>16–2. 佈署專案到 Vercel</strong></p><ul><li>在 <a href="https://vercel.com/">Vercel</a> 平台上連接 GitHub repository。</li><li>添加專案的環境變數（Environment Variables）。</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c3tO5k5G9RxTr0QXwEPGSw.png" /><figcaption>添加專案的環境變數（Environment Variables）</figcaption></figure><ul><li>點選 Deploy 進行專案佈署，取得佈署後的 URL（如：https://nextauth-fullstack-app.vercel.app）。</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a9hLrXR8j6wgvOzRzuGRTg.png" /><figcaption>佈署成功畫面，點擊 Continue to Dashboard</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1003/1*c4xe7xGhMIywBTOCF6UrYQ.png" /><figcaption>取得佈署後的 URL</figcaption></figure><p><strong>16–3. 更新 URL 設定</strong></p><ul><li>在 <a href="https://console.cloud.google.com/">Google Cloud Console</a> 中更新 API 憑證的 URL；在 Vercel 專案的環境變數（Environment Variables）設置中更新 NEXTAUTH_URL，並執行專案的重新佈署（Redeploy），更新 URL 設定步驟如下：</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*HPmp3s0HJmtcdMt8" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gCzGbZnftIIPWAuzJAfypg.png" /><figcaption>重新佈署（Redeploy）</figcaption></figure><p><strong>16–4. 檢查佈署後的專案功能</strong></p><ul><li>確認功能如深淺模式切換、Google 登入、用戶資料修改、登出、路由權限驗證等均正常工作。</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gjg1-kFha3lzoSKsJ0bGdg.png" /><figcaption>佈署完成，檢查專案功能</figcaption></figure><p><strong>單元重點</strong></p><ul><li>將專案部署到 <a href="https://vercel.com/">Vercel</a>，取得部署後的網址後更新 NEXTAUTH_URL，執行 Redeploy。</li><li>到 <a href="https://console.cloud.google.com/">Google Cloud Console</a> 更新 Authorized JavaScript origins、Authorized redirect URIs。</li></ul><p><strong>回到目錄</strong><br><a href="https://medium.com/p/3d2a3ec1d3b6">https://medium.com/p/3d2a3ec1d3b6</a></p><p><strong>專案結語</strong></p><p>在這個專案中，我們使用了 Next.js、NextAuth.js、MongoDB 和 TypeScript，搭配 Server Actions 構建了一個全棧專案的基礎架構，從開發環境的搭建到用戶認證和數據管理，再到專案佈署，共同實踐了這些技術的實際應用。如果對本專案的<a href="https://www.udemy.com/course/full-stack-authentication-with-nextjs-13/?referralCode=CE0BB92C4A5D886A421D">影音版本</a>說明感興趣，歡迎訪問 Udemy 平台觀看。本專案的基礎架構可作為進一步開發其他功能和頁面的起點。如果有任何想法或建議，歡迎留言告知。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=583dc5e7015b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/wei30172/%E5%85%A8%E6%A3%A7%E8%BA%AB%E4%BB%BD%E8%AA%8D%E8%AD%89%E5%AF%A6%E4%BD%9C-%E4%BD%BF%E7%94%A8-next-js-nextauth-js-%E5%92%8C-mongodb-16-%E5%B0%88%E6%A1%88%E4%BD%88%E7%BD%B2-583dc5e7015b">全棧身份認證實作：使用 Next.js、NextAuth.js 和 MongoDB－16. 專案佈署</a> was originally published in <a href="https://medium.com/wei30172">ClaireWei</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[全棧身份認證實作：使用 Next.js、NextAuth.js 和 MongoDB－15. 深色模式切換]]></title>
            <link>https://medium.com/wei30172/%E5%85%A8%E6%A3%A7%E8%BA%AB%E4%BB%BD%E8%AA%8D%E8%AD%89%E5%AF%A6%E4%BD%9C-%E4%BD%BF%E7%94%A8-next-js-nextauth-js-%E5%92%8C-mongodb-15-%E6%B7%B1%E8%89%B2%E6%A8%A1%E5%BC%8F%E5%88%87%E6%8F%9B-cb02ad1b4d40?source=rss-a643e8fc28e2------2</link>
            <guid isPermaLink="false">https://medium.com/p/cb02ad1b4d40</guid>
            <category><![CDATA[nextauth]]></category>
            <category><![CDATA[nextjs]]></category>
            <dc:creator><![CDATA[Claire Wei]]></dc:creator>
            <pubDate>Wed, 13 Dec 2023 07:22:26 GMT</pubDate>
            <atom:updated>2023-12-21T18:50:16.683Z</atom:updated>
            <content:encoded><![CDATA[<p>配置深色模式切換功能。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*AFb3lTu6bWupfGWS" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*hld4u6dJZ41G4lG4" /><figcaption>參考: shadcn/ui (<a href="https://ui.shadcn.com/docs/dark-mode/next">Dark mode</a>)</figcaption></figure><p><strong>15–1. 配置深色模式</strong></p><ul><li>安裝所需套件：</li></ul><pre>npm install next-themes</pre><ul><li>在 providers 資料夾，新增 theme-provider.tsx，將 ThemeProvider 添加到 layout.ts (RootLayout) 中。</li></ul><p><strong>15–2. 添加切換模式按鈕</strong></p><ul><li>在 components/shared 資料夾中新增 mode-toggle.tsx，使用 theme 狀態和 setTheme function 來控制主題切換。</li><li>在 Navbar 組件（navbar.tsx）中添加 ModeToggle 按鈕。</li></ul><p><strong>程式碼</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70ee48fe315f732125effffd771df404/href">https://medium.com/media/70ee48fe315f732125effffd771df404/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PJd7fNYDcfk4netzVgsJRQ.png" /><figcaption>切換深淺模式按鈕 及 畫面效果</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cVUn8YjudhtmRolvsoerKQ.png" /><figcaption>切換深淺模式按鈕 及 畫面效果</figcaption></figure><p><strong>單元重點</strong></p><ul><li>安裝 next-themes，在 layout (RootLayout) 加上 theme-provider。</li><li>建立切換深淺模式 ModeToggle 按鈕，於 Navbar 組件添加此按鈕。</li></ul><p><strong>參考資料</strong></p><p>shadcn/ui (Dark mode)－<a href="https://ui.shadcn.com/docs/dark-mode/next">https://ui.shadcn.com/docs/dark-mode/next</a></p><p><strong>回到目錄</strong><br><a href="https://medium.com/p/3d2a3ec1d3b6">https://medium.com/p/3d2a3ec1d3b6</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cb02ad1b4d40" width="1" height="1" alt=""><hr><p><a href="https://medium.com/wei30172/%E5%85%A8%E6%A3%A7%E8%BA%AB%E4%BB%BD%E8%AA%8D%E8%AD%89%E5%AF%A6%E4%BD%9C-%E4%BD%BF%E7%94%A8-next-js-nextauth-js-%E5%92%8C-mongodb-15-%E6%B7%B1%E8%89%B2%E6%A8%A1%E5%BC%8F%E5%88%87%E6%8F%9B-cb02ad1b4d40">全棧身份認證實作：使用 Next.js、NextAuth.js 和 MongoDB－15. 深色模式切換</a> was originally published in <a href="https://medium.com/wei30172">ClaireWei</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[全棧身份認證實作：使用 Next.js、NextAuth.js 和 MongoDB－14. 路由權限驗證]]></title>
            <link>https://medium.com/wei30172/%E5%85%A8%E6%A3%A7%E8%BA%AB%E4%BB%BD%E8%AA%8D%E8%AD%89%E5%AF%A6%E4%BD%9C-%E4%BD%BF%E7%94%A8-next-js-nextauth-js-%E5%92%8C-mongodb-14-%E8%B7%AF%E7%94%B1%E6%AC%8A%E9%99%90%E9%A9%97%E8%AD%89-dfaa009415ed?source=rss-a643e8fc28e2------2</link>
            <guid isPermaLink="false">https://medium.com/p/dfaa009415ed</guid>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[nextauth]]></category>
            <dc:creator><![CDATA[Claire Wei]]></dc:creator>
            <pubDate>Wed, 13 Dec 2023 07:15:08 GMT</pubDate>
            <atom:updated>2025-04-28T23:31:31.807Z</atom:updated>
            <content:encoded><![CDATA[<p>使用 Middleware 設置路由保護機制，根據用戶身份限制訪問。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*Ulkz7iEgECYjpUGh" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*MZSgPYNSJ1CN1NE-" /><figcaption>參考: Next.js (<a href="https://next-auth.js.org/configuration/nextjs#middleware">Middleware</a>)</figcaption></figure><p><strong>14–1. 設置路由權限</strong></p><ul><li>修改 middleware.ts，使用 withAuth 來進行權限驗證。通過 callbacks.authorized 檢查 token 的有效性，以控制對特定路由的訪問權限並決定是否執行 middleware function。</li><li>根據用戶的角色（如 admin 或 user）為需要保護的路徑（如 /dashboard）添加額外的路由權限檢查。</li></ul><p><strong>14–2. 處理未授權訪問</strong></p><ul><li>如果用戶嘗試訪問未授權的路由，則將其重定向到指定頁面，如 /unauthorized。</li><li>app 資料夾中新增 unauthorized 資料夾，用於存放未授權訪問的提醒頁面（page.tsx）；app 資料夾中新增 dashboard 資料夾及頁面（page.tsx），用以測試當未授權身份訪問時的處理流程。</li></ul><p><strong>程式碼</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44084e4f9d20e89c03c9efc6c61c6748/href">https://medium.com/media/44084e4f9d20e89c03c9efc6c61c6748/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a75d70e387602a94b902020ca5aaff6/href">https://medium.com/media/4a75d70e387602a94b902020ca5aaff6/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XqK8yja1CK27-ysOmqVE0A.png" /><figcaption>非 admin 用戶嘗試訪問 Dashboard 頁面時，將被重定向到未授權訪問提醒頁</figcaption></figure><p><strong>單元重點</strong></p><ul><li>設定 middleware 針對特定路由進行使用者認證，並進行身份檢查。</li><li>建立 unauthorized 及 dashboard 路徑內容。</li></ul><p><strong>參考資料</strong></p><p>Next.js (Middleware)－<a href="https://next-auth.js.org/configuration/nextjs#middleware">https://next-auth.js.org/configuration/nextjs#middleware</a></p><p><strong>回到目錄</strong><br><a href="https://medium.com/p/3d2a3ec1d3b6">https://medium.com/p/3d2a3ec1d3b6</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dfaa009415ed" width="1" height="1" alt=""><hr><p><a href="https://medium.com/wei30172/%E5%85%A8%E6%A3%A7%E8%BA%AB%E4%BB%BD%E8%AA%8D%E8%AD%89%E5%AF%A6%E4%BD%9C-%E4%BD%BF%E7%94%A8-next-js-nextauth-js-%E5%92%8C-mongodb-14-%E8%B7%AF%E7%94%B1%E6%AC%8A%E9%99%90%E9%A9%97%E8%AD%89-dfaa009415ed">全棧身份認證實作：使用 Next.js、NextAuth.js 和 MongoDB－14. 路由權限驗證</a> was originally published in <a href="https://medium.com/wei30172">ClaireWei</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[全棧身份認證實作：使用 Next.js、NextAuth.js 和 MongoDB－13. 密碼變更]]></title>
            <link>https://medium.com/wei30172/%E5%85%A8%E6%A3%A7%E8%BA%AB%E4%BB%BD%E8%AA%8D%E8%AD%89%E5%AF%A6%E4%BD%9C-%E4%BD%BF%E7%94%A8-next-js-nextauth-js-%E5%92%8C-mongodb-13-%E5%AF%86%E7%A2%BC%E8%AE%8A%E6%9B%B4-0ea6ef5c57c6?source=rss-a643e8fc28e2------2</link>
            <guid isPermaLink="false">https://medium.com/p/0ea6ef5c57c6</guid>
            <category><![CDATA[nextauth]]></category>
            <category><![CDATA[nextjs]]></category>
            <dc:creator><![CDATA[Claire Wei]]></dc:creator>
            <pubDate>Wed, 13 Dec 2023 07:05:47 GMT</pubDate>
            <atom:updated>2025-04-29T00:39:21.616Z</atom:updated>
            <content:encoded><![CDATA[<p>實現用戶的密碼更改功能，包括創建界面和處理密碼更新請求。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*QcvqjmzHM5jQ3432" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*YpOGdDeaKPEl5KW4" /></figure><p><strong>13–1. 建立變更密碼頁面</strong></p><ul><li>在 lib/validations/auth.ts 中新增 changePasswordValidation 來確保密碼的有效性。</li><li>於 components/form 資料夾建立 ChangePasswordForm 組件（change-password-form.tsx）。</li><li>app/(auth) 資料夾中建立 change-password 資料夾，存放對應頁面 （page.tsx），import ChangePasswordForm。</li><li>在 update-form.tsx 中新增前往 change-password 頁面的連結，且僅在用戶以 credentials 登入方式時顯示。</li></ul><p><strong>13–2. 設定用戶變更密碼功能</strong></p><ul><li>在 auth.actions.ts 中新增 changeUserPassword 以處理密碼變更請求，使用 bcrypt 驗證舊密碼並加密新密碼。</li><li>change-password-form.tsx 修改 onSubmit function，使用 changeUserPassword 處理密碼變更，在變更成功後顯示通知，並執行登出及導向登入頁面。</li></ul><p><strong>程式碼</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78ac5c89e6ccb9e88d4aeb039d517cfd/href">https://medium.com/media/78ac5c89e6ccb9e88d4aeb039d517cfd/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZC_z9RwzFOMwHbLXpOLBxg.png" /><figcaption>變更密碼頁面 及 輸入內容驗證</figcaption></figure><p><strong>單元重點：</strong></p><ul><li>(auth) 資料夾中，建立 change-password 路徑內容，並且將當中會使用到 “use client” 的表單獨立成 ChangePasswordForm 組件。</li><li>新增 server actions：changeUserPassword。</li></ul><p><strong>回到目錄</strong><br><a href="https://medium.com/p/3d2a3ec1d3b6">https://medium.com/p/3d2a3ec1d3b6</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0ea6ef5c57c6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/wei30172/%E5%85%A8%E6%A3%A7%E8%BA%AB%E4%BB%BD%E8%AA%8D%E8%AD%89%E5%AF%A6%E4%BD%9C-%E4%BD%BF%E7%94%A8-next-js-nextauth-js-%E5%92%8C-mongodb-13-%E5%AF%86%E7%A2%BC%E8%AE%8A%E6%9B%B4-0ea6ef5c57c6">全棧身份認證實作：使用 Next.js、NextAuth.js 和 MongoDB－13. 密碼變更</a> was originally published in <a href="https://medium.com/wei30172">ClaireWei</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[全棧身份認證實作：使用 Next.js、NextAuth.js 和 MongoDB－12. 帳號登入]]></title>
            <link>https://medium.com/wei30172/%E5%85%A8%E6%A3%A7%E8%BA%AB%E4%BB%BD%E8%AA%8D%E8%AD%89%E5%AF%A6%E4%BD%9C-%E4%BD%BF%E7%94%A8-next-js-nextauth-js-%E5%92%8C-mongodb-12-%E5%B8%B3%E8%99%9F%E7%99%BB%E5%85%A5-bde177254036?source=rss-a643e8fc28e2------2</link>
            <guid isPermaLink="false">https://medium.com/p/bde177254036</guid>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[nextauth]]></category>
            <dc:creator><![CDATA[Claire Wei]]></dc:creator>
            <pubDate>Wed, 13 Dec 2023 06:45:00 GMT</pubDate>
            <atom:updated>2023-12-21T18:49:28.615Z</atom:updated>
            <content:encoded><![CDATA[<p>使用電子郵件和密碼進行帳號登入的方法。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*J1qPyXxGWXG1O7BV" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*qVUNG7pnhWZU2iIq" /><figcaption>參考: NextAuth.js (<a href="https://next-auth.js.org/providers/credentials">Credentials</a>)</figcaption></figure><p><strong>12–1. 設定登入功能</strong></p><ul><li>在 nextauth-options.ts 中加入 CredentialsProvider，設定 authorize function 來驗證用戶提供的登入憑證。</li><li>在 auth.actions.ts 中新增 signInWithCredentials 以處理登入請求，使用 bcrypt 來驗證用戶輸入的密碼，如驗證成功，返回用戶信息；如失敗，返回錯誤信息。</li><li>signin-form.tsx 修改 onSubmit function，使用 credentials signIn 功能並傳遞 email、password 和 callbackUrl 參數進行登入。</li></ul><p><strong>程式碼</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1d1e2ae22e6b65e3faf05244cb0ce12/href">https://medium.com/media/d1d1e2ae22e6b65e3faf05244cb0ce12/href</a></iframe><p><strong>單元重點</strong></p><ul><li>於 nextauth-options.ts 添加 CredentialsProvider；新增 server actions：signInWithCredentials。</li><li>於登入表單使用 credentials signIn 功能。</li></ul><p><strong>參考資料</strong></p><p>NextAuth.js (Credentials)－<a href="https://next-auth.js.org/providers/credentials">https://next-auth.js.org/providers/credentials</a></p><p><strong>回到目錄</strong><br><a href="https://medium.com/p/3d2a3ec1d3b6">https://medium.com/p/3d2a3ec1d3b6</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bde177254036" width="1" height="1" alt=""><hr><p><a href="https://medium.com/wei30172/%E5%85%A8%E6%A3%A7%E8%BA%AB%E4%BB%BD%E8%AA%8D%E8%AD%89%E5%AF%A6%E4%BD%9C-%E4%BD%BF%E7%94%A8-next-js-nextauth-js-%E5%92%8C-mongodb-12-%E5%B8%B3%E8%99%9F%E7%99%BB%E5%85%A5-bde177254036">全棧身份認證實作：使用 Next.js、NextAuth.js 和 MongoDB－12. 帳號登入</a> was originally published in <a href="https://medium.com/wei30172">ClaireWei</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[全棧身份認證實作：使用 Next.js、NextAuth.js 和 MongoDB－11. 帳號註冊]]></title>
            <link>https://medium.com/wei30172/%E5%85%A8%E6%A3%A7%E8%BA%AB%E4%BB%BD%E8%AA%8D%E8%AD%89%E5%AF%A6%E4%BD%9C-%E4%BD%BF%E7%94%A8-next-js-nextauth-js-%E5%92%8C-mongodb-11-%E5%B8%B3%E8%99%9F%E8%A8%BB%E5%86%8A-153758938315?source=rss-a643e8fc28e2------2</link>
            <guid isPermaLink="false">https://medium.com/p/153758938315</guid>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[nextauth]]></category>
            <dc:creator><![CDATA[Claire Wei]]></dc:creator>
            <pubDate>Wed, 13 Dec 2023 06:21:43 GMT</pubDate>
            <atom:updated>2025-04-29T00:36:14.144Z</atom:updated>
            <content:encoded><![CDATA[<p>使用電子郵件和密碼進行帳號註冊的流程。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*C-EstHa5CM3TBBIB" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*SQA_ZsgZ3WNGAW1f" /></figure><p><strong>11–1. 設定註冊功能</strong></p><ul><li>在 auth.actions.ts 中新增 signUpWithCredentials 以處理註冊請求，檢查電子郵件是否已被註冊，並對密碼進行加密處理，創建新用戶並保存到資料庫中。在註冊成功時顯示成功通知，並導向登入頁面；若發生錯誤則導向錯誤頁面，並顯示相應錯誤信息。</li><li>在 “use client” 組件使用 server action 的方式：在 use server 頁面（SignUpPage）中，將 server action（signUpWithCredentials）作為 props 傳給 use client 組件（SignUpForm），以處理用戶註冊。</li></ul><p><strong>程式碼</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69cc8e08448d2e7edba766253bb25ed9/href">https://medium.com/media/69cc8e08448d2e7edba766253bb25ed9/href</a></iframe><p><strong>單元重點：</strong></p><ul><li>新增 server actions：signUpWithCredentials</li></ul><p><strong>回到目錄</strong><br><a href="https://medium.com/p/3d2a3ec1d3b6">https://medium.com/p/3d2a3ec1d3b6</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=153758938315" width="1" height="1" alt=""><hr><p><a href="https://medium.com/wei30172/%E5%85%A8%E6%A3%A7%E8%BA%AB%E4%BB%BD%E8%AA%8D%E8%AD%89%E5%AF%A6%E4%BD%9C-%E4%BD%BF%E7%94%A8-next-js-nextauth-js-%E5%92%8C-mongodb-11-%E5%B8%B3%E8%99%9F%E8%A8%BB%E5%86%8A-153758938315">全棧身份認證實作：使用 Next.js、NextAuth.js 和 MongoDB－11. 帳號註冊</a> was originally published in <a href="https://medium.com/wei30172">ClaireWei</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[全棧身份認證實作：使用 Next.js、NextAuth.js 和 MongoDB－10. 用戶資料更新]]></title>
            <link>https://medium.com/wei30172/%E5%85%A8%E6%A3%A7%E8%BA%AB%E4%BB%BD%E8%AA%8D%E8%AD%89%E5%AF%A6%E4%BD%9C-%E4%BD%BF%E7%94%A8-next-js-nextauth-js-%E5%92%8C-mongodb-10-%E7%94%A8%E6%88%B6%E8%B3%87%E6%96%99%E6%9B%B4%E6%96%B0-5b852c96f7bb?source=rss-a643e8fc28e2------2</link>
            <guid isPermaLink="false">https://medium.com/p/5b852c96f7bb</guid>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[nextauth]]></category>
            <dc:creator><![CDATA[Claire Wei]]></dc:creator>
            <pubDate>Wed, 13 Dec 2023 06:08:14 GMT</pubDate>
            <atom:updated>2025-04-29T00:32:04.336Z</atom:updated>
            <content:encoded><![CDATA[<p>實現用戶資料的更新，包括創建表單和處理更新請求。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*zCxO5EQ2bp1vG5h9" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*PhgOseLwya7WYkIq" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*QKxjjT6fy5Fsn5e-" /></figure><p><strong>10–1. 建立用戶資料更新頁面</strong></p><ul><li>在 lib/validations/auth.ts 中新增 userUpdateValidation 來驗證用戶輸入。</li><li>於 components/form 資料夾建立 UpdateForm 組件（update-form.tsx）。</li><li>app/(auth) 資料夾中建立 profile 資料夾，存放對應頁面（page.tsx），import UpdateForm。</li></ul><p><strong>10–2. 設定用戶資料更新功能</strong></p><ul><li>在 auth.actions.ts 中新增 updateUserProfile 以處理用戶更新請求，檢查用戶登入狀態，並在資料庫中更新用戶信息，在更新成功時顯示成功通知，若發生錯誤則導向錯誤頁面，並顯示相應錯誤信息。</li><li>在 “use client” 組件使用 server action 的方式：在 “use server” 頁面（ProfilePage）中，將 server action（updateUserProfile）作為 props 傳給 use client 組件（UpdateForm），同時，在 auth.actions.ts 中加上 “use server”。</li><li>為解決在提取 session 中的 user 資料時遇到的類型問題，新增 types 資料夾，並創建 next-auth.d.ts 來定義相關的 TypeScript 類型。</li><li>在 nextauth-options.ts 中修改 jwt() callback，加上 update trigger，以實現對 session 中 user name 的即時更新。</li></ul><p><strong>程式碼</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/decaa129e3ec2da26fce0cedeeba348b/href">https://medium.com/media/decaa129e3ec2da26fce0cedeeba348b/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PVVjobX35s1USkxznP94Lg.png" /><figcaption>變更資料頁面 及 輸入內容驗證</figcaption></figure><p><strong>10–3. Toast 通知及錯誤頁面</strong></p><ul><li>在 layout.ts (RootLayout) 添加 Toaster 組件以顯示通知。</li><li>app 資料夾中新增 error 資料夾，用於存放自定義的錯誤頁面（page.tsx），使用 useSearchParams 從 URL 中獲取錯誤信息並顯示，於 nextauth-options.ts 加上指定的錯誤頁面（error: “/error”）。</li></ul><p><strong>程式碼</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/92f50ce4f4d300b7465bf97e7c85c954/href">https://medium.com/media/92f50ce4f4d300b7465bf97e7c85c954/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rDFQi9J1GTujK3z8" /><figcaption>Toast 通知</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ykIwblVSUoHwTJ8QtNRANA.png" /><figcaption><strong>錯誤頁面</strong></figcaption></figure><p><strong>單元重點：</strong></p><ul><li>(auth) 資料夾中，建立 profile 路徑內容，並且將當中會使用到”use client” 的表單獨立成 UpdateForm 組件。</li><li>新增 server actions：updateUserProfile。</li><li>新增 types/next-auth.d.ts 定義相關的 TypeScript 類型。</li><li>nextauth-options.ts 中修改 jwt() callback，加上 update trigger。</li><li>在 layout.ts (RootLayout) 添加 Toaster，使用 toast 通知。</li><li>建立 error 路徑內容，於 nextauth-options.ts 加上指定的錯誤頁面。</li></ul><p><strong>回到目錄</strong><br><a href="https://medium.com/p/3d2a3ec1d3b6">https://medium.com/p/3d2a3ec1d3b6</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5b852c96f7bb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/wei30172/%E5%85%A8%E6%A3%A7%E8%BA%AB%E4%BB%BD%E8%AA%8D%E8%AD%89%E5%AF%A6%E4%BD%9C-%E4%BD%BF%E7%94%A8-next-js-nextauth-js-%E5%92%8C-mongodb-10-%E7%94%A8%E6%88%B6%E8%B3%87%E6%96%99%E6%9B%B4%E6%96%B0-5b852c96f7bb">全棧身份認證實作：使用 Next.js、NextAuth.js 和 MongoDB－10. 用戶資料更新</a> was originally published in <a href="https://medium.com/wei30172">ClaireWei</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[全棧身份認證實作：使用 Next.js、NextAuth.js 和 MongoDB－9. Google OAuth 與 mongoose]]></title>
            <link>https://medium.com/wei30172/%E5%85%A8%E6%A3%A7%E8%BA%AB%E4%BB%BD%E8%AA%8D%E8%AD%89%E5%AF%A6%E4%BD%9C-%E4%BD%BF%E7%94%A8-next-js-nextauth-js-%E5%92%8C-mongodb-9-google-oauth-%E8%88%87-mongoose-a865141be68b?source=rss-a643e8fc28e2------2</link>
            <guid isPermaLink="false">https://medium.com/p/a865141be68b</guid>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[nextauth]]></category>
            <dc:creator><![CDATA[Claire Wei]]></dc:creator>
            <pubDate>Wed, 13 Dec 2023 05:02:01 GMT</pubDate>
            <atom:updated>2025-04-29T00:26:34.475Z</atom:updated>
            <content:encoded><![CDATA[<p>結合 Google OAuth 和 mongoose 管理用戶數據。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*1JAlh2PGcX3IrUmc" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*QOeJUEQ9-VflZj2Y" /><figcaption>參考: NextAuth.js (<a href="https://next-auth.js.org/configuration/callbacks">Callbacks</a>)</figcaption></figure><p><strong>9–1. 處理 Google OAuth 登入函數</strong></p><ul><li>在 nextauth-options.ts 中添加回調函數（callbacks function），使用 signIn(), jwt(), session() 回調來管理用戶信息。</li><li>在 signIn() 回調中處理用戶登入後的資訊，並於 auth.actions.ts 中新增 signInWithOauth，管理 OAuth 登入流程，確保用戶不存在時才建立新用戶，避免資料重複。</li></ul><p><strong>9–2. 更新 Token 和 Session</strong></p><ul><li>使用 jwt() 回調來保存用戶信息於 token 中，在 auth.actions.ts 中新增 getUserByEmail 從資料庫中獲取用戶信息。</li><li>在 session() 回調中將 token 中的用戶信息添加到 session。</li></ul><p><strong>程式碼</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa787496d0f8388245e795bf77be6b2b/href">https://medium.com/media/fa787496d0f8388245e795bf77be6b2b/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UTNbjfmZ_R87V7KZVglf8g.png" /><figcaption>如果有使用 React Developer Tools 擴充，可以點選 Components 查看 SessionProvider</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/523/1*z56X1G7M7ykKL_0ebHDRzA.png" /><figcaption>用戶登入後，session 內容包含 user 信息</figcaption></figure><p><strong>單元重點</strong></p><p>在 nextauth-options.ts 中設定 callbacks：signIn(), jwt(), session()。</p><ul><li>jwt()：將用戶資料保存在 token 中。</li><li>session()：將 token 中的用戶資料傳遞到 session。</li></ul><p>新增 server actions：</p><ul><li>signInWithOauth：處理 OAuth 登入並檢查用戶是否已存在。</li><li>getUserByEmail：根據 email 從資料庫查詢用戶資料。</li></ul><p><strong>參考資料</strong></p><p>NextAuth.js (Callbacks)－<a href="https://next-auth.js.org/configuration/callbacks">https://next-auth.js.org/configuration/callbacks</a></p><p><strong>回到目錄</strong><br><a href="https://medium.com/p/3d2a3ec1d3b6">https://medium.com/p/3d2a3ec1d3b6</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a865141be68b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/wei30172/%E5%85%A8%E6%A3%A7%E8%BA%AB%E4%BB%BD%E8%AA%8D%E8%AD%89%E5%AF%A6%E4%BD%9C-%E4%BD%BF%E7%94%A8-next-js-nextauth-js-%E5%92%8C-mongodb-9-google-oauth-%E8%88%87-mongoose-a865141be68b">全棧身份認證實作：使用 Next.js、NextAuth.js 和 MongoDB－9. Google OAuth 與 mongoose</a> was originally published in <a href="https://medium.com/wei30172">ClaireWei</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>