如何確保使用者在React中密碼輸入安全

如何確保使用者在React中密碼輸入安全

PIN 輸入是許多 React 應用程式的重要組成部分,尤其是那些處理雙因素身份驗證(2FA)程式碼或一次性密碼(OTP)等敏感資訊的應用程式。它們為使用者輸入敏感資訊提供了一種安全且使用者友好的方式,而不會洩露他們的資料,本文將向您展示如何使用 React 庫來實現這一點。

React-pin-input 是一個多功能、可定製的庫,可幫助開發人員為其 React 應用程式設計安全、使用者友好的 PIN 輸入介面。它的遮蔽功能、輸入驗證和回撥機制可確保使用者資料得到保護,同時提供無縫的輸入體驗。其可定製的樣式和附加 API 進一步增強了其適應性,使其成為在 React 表單中構建強大的 PIN 輸入功能的理想選擇。

實施 PIN 輸入具有重要意義,原因有以下幾點:

  • 快速便捷的身份驗證:PIN 可提供更快、更方便的驗證過程。使用者可以快速輸入一個簡短的數字程式碼,減少登入或驗證過程中的摩擦。
  • 增強安全性:在輸入敏感資料時,PIN 碼輸入比傳統的文字輸入更安全。通過將使用者輸入限制為數字字元並遮蔽輸入的數字,PIN 輸入可降低意外資料暴露或未經授權訪問的風險。
  • 改善使用者體驗:PIN 輸入為輸入敏感資訊提供了一個視覺上獨特且易於使用的介面。單個 PIN 數字的清晰分隔和輸入時的視覺反饋使使用者更容易輸入 PIN 碼,不會出現混淆或錯誤。
  • 實際應用:PIN 輸入廣泛應用於各種 React 應用程式,包括
    • 雙因素身份驗證 (2FA):PIN 輸入通常用於在登入或金融交易過程中輸入 2FA 程式碼。
      一次性密碼(OTP):PIN 輸入用於在賬戶啟用或重置密碼時輸入 OTP。
      支付處理:PIN 輸入用於為銀行卡支付或安全交易輸入 PIN 碼。
      裝置驗證:PIN 輸入可用於確保裝置解鎖或敏感資料訪問的安全。

在處理敏感資訊的 React 應用程式中,PIN 輸入對於增強安全性、改善使用者體驗和確保可訪問性至關重要。開發人員可以通過整合 PIN 輸入為各種用例建立安全、使用者友好的介面。

React-pin-input 庫簡介

React-pin-input 是一個流行的開源 React 元件庫,旨在簡化 React 應用程式中安全 PIN 輸入功能的整合。該庫簡化了 PIN 輸入的實現過程,為開發人員提供了一個強大的可定製解決方案,用於增強使用者身份驗證和驗證流程的安全性。

您可能想知道為什麼要在眾多可選項中選擇 React-pin-input。答案就在於它的簡單性、靈活性和強大功能。它不僅僅是一個庫,更是一個工具包,能讓開發人員毫不費力地增強安全性和使用者體驗。以下是考慮在 React 應用程式中使用 React-pin-input 庫的一些原因:

  • 簡單直觀的 API:React-pin-input 擁有簡單直觀的 API,開發人員可以輕鬆地將 PIN 輸入欄位整合到他們的 React 應用程式中。該元件的道具定義明確、文件齊全,開發人員可以快速掌握其功能,並根據自己的需要進行定製。
  • 可定製的 PIN 輸入樣式React-pin-input 提供了高度的自定義功能,使開發人員能夠定製 PIN 輸入欄位的外觀,以符合其應用程式的設計美學。開發人員可以使用 CSS 或樣式化元件為單個 PIN 輸入數字、輸入容器和錯誤資訊區域設計樣式。
  • 實時反饋:React-pin-input 可在使用者輸入 PIN 碼時向其提供實時反饋。這種互動式功能可提供即時的視覺提示,如突出顯示已完成的數字,從而增強使用者體驗。支
  • 持各種輸入長度React-pin-input 支援各種輸入長度,以滿足不同的使用情況。開發人員可以指定 PIN 輸入欄位所需的長度(四位或六位),以實現安全交易。
  • 輕量級:React-pin-input 採用輕量級設計,最大限度地減少了對應用程式效能的影響。它針對高效渲染進行了優化,使其適用於生產級應用。

React-pin-input 為在 React 應用程式中實現安全和可定製的引腳輸入欄位提供了全面和使用者友好的解決方案。它的簡單性和靈活性使其成為開發人員增強應用程式安全性和使用者體驗的重要工具。

安裝

該庫易於安裝和設定,並提供多種功能,是開發人員的多功能工具。

  • 導航至專案目錄並安裝 React-pin-input 庫:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm install react-pin-input
or
yarn add react-pin-input
npm install react-pin-input or yarn add react-pin-input
npm install react-pin-input
or
yarn add react-pin-input
  • 將 React-pin-input 元件匯入 React 元件:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import React from "react";
import PinInput from "react-pin-input";
const App = () => {
return <div>App</div>;
};
export default App;
import React from "react"; import PinInput from "react-pin-input"; const App = () => { return <div>App</div>; }; export default App;
import React from "react";
import PinInput from "react-pin-input";
const App = () => {
return <div>App</div>;
};
export default App;

react-pin-input 庫中的 PinInput 元件提供了在 React 應用程式中建立 PIN 輸入元件的功能。

建立基本的 PIN 輸入元件

建立基本的 React Pin-input 元件是一個簡單明瞭的過程,可以增強 React 應用程式的安全性和使用者體驗。安裝該庫後,開發人員可以利用該庫中的 PinInput 元件建立一個專用元件。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import React, { useState } from "react";
import PinInput from "react-pin-input";
const App = () => {
const [pin, setPin] = useState("");
const handleComplete = (value) => {
// Handle the completed PIN input
setPin(value);
};
console.log("Submitting PIN:", pin);
return (
<div className="flex flex-col items-center justify-center min-h-screen bg-gray-100">
<h1 className="text-4xl font-bold mb-8">
Basic Example of React-Pin-Input{" "}
</h1>
<PinInput
length={4} // Set the PIN length
onComplete={handleComplete}
/>
</div>
);
};
export default App;
import React, { useState } from "react"; import PinInput from "react-pin-input"; const App = () => { const [pin, setPin] = useState(""); const handleComplete = (value) => { // Handle the completed PIN input setPin(value); }; console.log("Submitting PIN:", pin); return ( <div className="flex flex-col items-center justify-center min-h-screen bg-gray-100"> <h1 className="text-4xl font-bold mb-8"> Basic Example of React-Pin-Input{" "} </h1> <PinInput length={4} // Set the PIN length onComplete={handleComplete} /> </div> ); }; export default App;
import React, { useState } from "react";
import PinInput from "react-pin-input";
const App = () => {
const [pin, setPin] = useState("");
const handleComplete = (value) => {
// Handle the completed PIN input
setPin(value);
};
console.log("Submitting PIN:", pin);
return (
<div className="flex flex-col items-center justify-center min-h-screen bg-gray-100">
<h1 className="text-4xl font-bold mb-8">
Basic Example of React-Pin-Input{" "}
</h1>
<PinInput
length={4} // Set the PIN length
onComplete={handleComplete}
/>
</div>
);
};
export default App;

我們將使用 useState 鉤子來管理 PIN 的狀態。pin 變數儲存當前的 PIN 值,而 setPin 是一個更新 PIN 的函式。該元件通常包括一個回撥函式,如 handleComplete,用於捕捉和處理已完成的 PIN 輸入。長度道具允許開發人員自定義所需的 PIN 長度,從而提供基於安全性和使用者體驗考慮的適應性。

React-pin-input基礎示例

這一基本設定為在 React 應用程式中建立一個安全、使用者友好的 PIN 輸入系統奠定了基礎,併為進一步定製和整合到各種用例中留出了空間。

React-pin-input 應用介面

該庫的 API 包括:

  • lengthlength屬性指的是構成整個 PIN 輸入的單個輸入框或插槽的數量。從本質上講,它定義了使用者在輸入 PIN 碼時需要輸入多少個字元或數字。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<PinInput length={4} />
<PinInput length={4} />
<PinInput length={4} />
  • initialValueinitialValue 屬性用於指定渲染元件時輸入框的初始內容或值。當你想用預設值或以前儲存的值預先填充 PIN 輸入框時,這個屬性非常有用。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<PinInput length={4} initialValue="1234" />
<PinInput length={4} initialValue="1234" />
<PinInput length={4} initialValue="1234" />
  • secretsecret 屬性通過隱藏輸入的實際字元或數字,增加了一層安全性。這在隱私和安全至關重要的情況下特別有用,例如輸入敏感交易的 PIN 碼或密碼時。祕密屬性提高了輸入資訊的保密性,確保即使有人從使用者的肩膀上偷窺,也無法辨別實際值。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<PinInput length={4} secret />
<PinInput length={4} secret />
<PinInput length={4} secret />
  •  secretDelayreact-pin-input 元件中的 secretDelay API 允許你在祕密道具設定為 true 時,在隱藏輸入值之前指定一個毫秒延遲。如果你想在隱藏輸入值之前短暫顯示輸入值,以提高安全性,那麼這種延遲就非常有用。例如,你可以使用 500 毫秒(半秒)的延遲,這樣使用者就可以在密碼隱藏前確認他們輸入了正確的密碼。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<PinInput length={4} secret secretDelay={500} />
<PinInput length={4} secret secretDelay={500} />
<PinInput length={4} secret secretDelay={500} />
  • disableddisabled 屬性用於使整個 PinInput 元件無響應,阻止使用者與其互動。這通常用於暫時禁用使用者輸入,比如在載入狀態或滿足某些條件時。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<PinInput length={4} disabled />
<PinInput length={4} disabled />
<PinInput length={4} disabled />
  • focus:當 PinInput 元件呈現時,focus 屬性會將預設焦點設定在第一個輸入元素上。這意味著當頁面載入或元件載入時,游標或焦點將自動放在第一個輸入框中,使用者無需點選輸入框即可開始輸入 PIN 碼。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<PinInput length={4} focus />
<PinInput length={4} focus />
<PinInput length={4} focus />
  • type:確定輸入型別,是數字、字母還是字母數字。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<ReactPinInput type="numeric" />
<ReactPinInput type="alphanumeric" />
<ReactPinInput type="alphabet" />
<ReactPinInput type="numeric" /> <ReactPinInput type="alphanumeric" /> <ReactPinInput type="alphabet" />
<ReactPinInput type="numeric" />
<ReactPinInput type="alphanumeric" />
<ReactPinInput type="alphabet" />
  • onChange:在 PinInput 元件的上下文中, onChange 屬性是一個回撥函式,每當 PIN 輸入發生變化時,該函式就會被呼叫。該回撥函式提供了有關當前輸入值和焦點輸入框索引的資訊。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<PinInput length={4} onChange={(value, index) => handlePinChange(value, index)} />
<PinInput length={4} onChange={(value, index) => handlePinChange(value, index)} />
<PinInput length={4} onChange={(value, index) => handlePinChange(value, index)} />

onChange 屬性被設定為一個名為 handlePinChange 的函式,該函式需要兩個引數: value – 表示特定索引處 PIN 輸入的當前值。 index – 表示當前處於焦點的輸入的位置或索引。

  • onComplete:在 PinInput 元件的上下文中,onComplete 屬性是一個回撥函式,當所有輸入都具有有效值且 PIN 輸入被視為完成時,該函式將被呼叫。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<PinInput length={4} onComplete={(value, index) => handlePinComplete(value, index)} />
<PinInput length={4} onComplete={(value, index) => handlePinComplete(value, index)} />
<PinInput length={4} onComplete={(value, index) => handlePinComplete(value, index)} />

onComplete 屬性被設定為一個名為 handlePinComplete 的函式,該函式也需要兩個引數: value – 表示 PIN 輸入的最終值。 index – 表示完成時當前輸入的焦點位置或索引。

  • inputModeinputMode 屬性用於指定使用者與輸入框互動時應顯示的鍵盤型別。inputMode 的 number 值表示應向使用者顯示數字鍵盤,這在希望使用者輸入數值(如 PIN 碼)時特別有用。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<PinInput length={4} inputMode="number" />
<PinInput length={4} inputMode="number" />
<PinInput length={4} inputMode="number" />

有了這種配置,當使用者點選或單擊 PIN 輸入時,在移動裝置上會觸發數字鍵盤,在桌上型電腦上則會觸發數字輸入模式。這將提供更友好的使用者體驗,因為它可以引導使用者只輸入數值。

自定義樣式

inputStyle 命令用於為接受使用者輸入的元件應用自定義樣式。該道具允許你傳遞一個代表類或內聯樣式的字串,以便為元件中的輸入元素設計樣式。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import React from "react";
import PinInput from "react-pin-input";
const App = () => {
return (
<div className="flex items-center pt-8 h-screen flex-col">
<h2 className=" font-extrabold text-3xl p-4"> Custom Styling</h2>
<PinInput
length={4}
inputStyle={{
border: "2px solid #3498db", // Customize the border color
borderRadius: "8px", // Adjust border radius
fontSize: "18px", // Change font size
padding: "10px", // Add some padding
margin: "5px", // Adjust margin
}}
/>
</div>
);
};
export default App;
import React from "react"; import PinInput from "react-pin-input"; const App = () => { return ( <div className="flex items-center pt-8 h-screen flex-col"> <h2 className=" font-extrabold text-3xl p-4"> Custom Styling</h2> <PinInput length={4} inputStyle={{ border: "2px solid #3498db", // Customize the border color borderRadius: "8px", // Adjust border radius fontSize: "18px", // Change font size padding: "10px", // Add some padding margin: "5px", // Adjust margin }} /> </div> ); }; export default App;
import React from "react";
import PinInput from "react-pin-input";
const App = () => {
return (
<div className="flex items-center pt-8 h-screen  flex-col">
<h2 className=" font-extrabold text-3xl p-4"> Custom Styling</h2>
<PinInput
length={4}
inputStyle={{
border: "2px solid #3498db", // Customize the border color
borderRadius: "8px", // Adjust border radius
fontSize: "18px", // Change font size
padding: "10px", // Add some padding
margin: "5px", // Adjust margin
}}
/>
</div>
);
};
export default App;

可以使用 inputStyle 道具自定義 paddingborderborderRadiusfontSize

自定義PIN輸入樣式

React-pin-input 在實際專案中的應用例項

react-pin-input 庫整合到實際專案(如構建學校登入頁面)中時,您可以通過實施安全且使用者友好的 PIN 身份驗證系統來增強使用者體驗。通過整合 react-pin-input,使用者可以安全地輸入自己的 PIN 碼,從而提供多一層身份驗證。登入成功後,系統可以無縫過渡到使用者歡迎頁面。

我們將建立一個 LoginAuth 元件,在輸入 PIN 碼時對其進行驗證。我們將使用 useState 鉤子進行狀態管理,監督輸入的 PIN、錯誤資訊和成功資訊等變數。為便於演示,我們將在 correctPin 變數中儲存一個固定的 PIN 碼。

建立一個 handlePinComplete 函式,用於在使用者完成輸入 pin 碼時更新元件的狀態引腳。該函式接收輸入的 PIN 作為 value 引數,並使用 setPin 函式更新狀態。當使用者點選登入按鈕時,會呼叫 handleLogin 函式。它的主要職責是根據正確的 PIN 碼驗證輸入的 PIN 碼。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import React, { useState } from "react";
import PinInput from "react-pin-input";
const LoginAuth = () => {
const correctPin = "9657"; // Fixed PIN for demonstration
const [pin, setPin] = useState("");
const [errorMessage, setErrorMessage] = useState("");
const [successMessage, setSuccessMessage] = useState("");
const handlePinComplete = (value) => {
setPin(value);
};
const handleLogin = () => {
// Validate the entered PIN
if (pin === correctPin) {
// PIN is correct
setSuccessMessage("🎉 Welcome back! Login successful!");
setErrorMessage("");
console.log("User logged in successfully");
} else {
// PIN is incorrect
setErrorMessage("❌ Incorrect PIN. Please try again.");
setSuccessMessage("");
console.log("Incorrect PIN attempt");
}
};
return (
<div>
</div>
);
};
export default LoginAuth;
import React, { useState } from "react"; import PinInput from "react-pin-input"; const LoginAuth = () => { const correctPin = "9657"; // Fixed PIN for demonstration const [pin, setPin] = useState(""); const [errorMessage, setErrorMessage] = useState(""); const [successMessage, setSuccessMessage] = useState(""); const handlePinComplete = (value) => { setPin(value); }; const handleLogin = () => { // Validate the entered PIN if (pin === correctPin) { // PIN is correct setSuccessMessage("🎉 Welcome back! Login successful!"); setErrorMessage(""); console.log("User logged in successfully"); } else { // PIN is incorrect setErrorMessage("❌ Incorrect PIN. Please try again."); setSuccessMessage(""); console.log("Incorrect PIN attempt"); } }; return ( <div> </div> ); }; export default LoginAuth;
import React, { useState } from "react";
import PinInput from "react-pin-input";
const LoginAuth = () => {
const correctPin = "9657"; // Fixed PIN for demonstration
const [pin, setPin] = useState("");
const [errorMessage, setErrorMessage] = useState("");
const [successMessage, setSuccessMessage] = useState("");
const handlePinComplete = (value) => {
setPin(value);
};
const handleLogin = () => {
// Validate the entered PIN
if (pin === correctPin) {
// PIN is correct
setSuccessMessage("🎉 Welcome back! Login successful!");
setErrorMessage("");
console.log("User logged in successfully");
} else {
// PIN is incorrect
setErrorMessage("❌ Incorrect PIN. Please try again.");
setSuccessMessage("");
console.log("Incorrect PIN attempt");
}
};
return (
<div>
</div>
);
};
export default LoginAuth;

現在,我們將設計登入元件的使用者介面,使應用程式更加完美。為了便於在應用程式中導航,該元件利用了 ” react-router-dom ” 庫中的 useNavigate 鉤子。該鉤子可在登入嘗試成功後實現程式設計重定向。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// Welcome Page
import React, { useState } from "react";
import PinInput from "react-pin-input";
import { useNavigate } from "react-router-dom";
const LoginAuth = () => {
const correctPin = "9657"; // Fixed PIN for demonstration
const [pin, setPin] = useState("");
const [errorMessage, setErrorMessage] = useState("");
const [successMessage, setSuccessMessage] = useState("");
const navigate = useNavigate();
const handlePinComplete = (value) => {
setPin(value);
};
const handleLogin = () => {
// Validate the entered PIN
if (pin === correctPin) {
// PIN is correct
setSuccessMessage("🎉 Welcome back! Login successful!");
setErrorMessage("");
console.log("User logged in successfully");
// Navigate to WelcomePage
navigate("/welcome");
} else {
// PIN is incorrect
setErrorMessage("❌ Incorrect PIN. Please try again.");
setSuccessMessage("");
console.log("Incorrect PIN attempt");
}
};
return (
<div>
<div className="flex flex-col items-center justify-center h-screen bg-black text-white">
<h1 className="text-3xl font-bold mb-6">🔒 Secure Login</h1>
<PinInput
onComplete={handlePinComplete}
length={4}
focus
secret
secretDelay={500}
inputStyle={{ borderColor: "white", color: "white" }}
inputFocusStyle={{ borderColor: "lightgray" }}
/>
<p className="mt-4">Enter your secure PIN to login✨</p>
<button
onClick={handleLogin}
className="mt-8 bg-green-500 text-white py-2 px-4 rounded-md hover:bg-green-600 focus:outline-none focus:ring focus:border-green-300"
>
🚀 Log In
</button>
{errorMessage && (
<div className="mt-6 text-red-600 font-semibold">{errorMessage}</div>
)}
{successMessage && (
<div className="mt-6 text-green-600 font-semibold">
{successMessage}
</div>
)}
</div>
</div>
);
};
export default LoginAuth;
// Welcome Page import React, { useState } from "react"; import PinInput from "react-pin-input"; import { useNavigate } from "react-router-dom"; const LoginAuth = () => { const correctPin = "9657"; // Fixed PIN for demonstration const [pin, setPin] = useState(""); const [errorMessage, setErrorMessage] = useState(""); const [successMessage, setSuccessMessage] = useState(""); const navigate = useNavigate(); const handlePinComplete = (value) => { setPin(value); }; const handleLogin = () => { // Validate the entered PIN if (pin === correctPin) { // PIN is correct setSuccessMessage("🎉 Welcome back! Login successful!"); setErrorMessage(""); console.log("User logged in successfully"); // Navigate to WelcomePage navigate("/welcome"); } else { // PIN is incorrect setErrorMessage("❌ Incorrect PIN. Please try again."); setSuccessMessage(""); console.log("Incorrect PIN attempt"); } }; return ( <div> <div className="flex flex-col items-center justify-center h-screen bg-black text-white"> <h1 className="text-3xl font-bold mb-6">🔒 Secure Login</h1> <PinInput onComplete={handlePinComplete} length={4} focus secret secretDelay={500} inputStyle={{ borderColor: "white", color: "white" }} inputFocusStyle={{ borderColor: "lightgray" }} /> <p className="mt-4">Enter your secure PIN to login✨</p> <button onClick={handleLogin} className="mt-8 bg-green-500 text-white py-2 px-4 rounded-md hover:bg-green-600 focus:outline-none focus:ring focus:border-green-300" > 🚀 Log In </button> {errorMessage && ( <div className="mt-6 text-red-600 font-semibold">{errorMessage}</div> )} {successMessage && ( <div className="mt-6 text-green-600 font-semibold"> {successMessage} </div> )} </div> </div> ); }; export default LoginAuth;
// Welcome Page
import React, { useState } from "react";
import PinInput from "react-pin-input";
import { useNavigate } from "react-router-dom";
const LoginAuth = () => {
const correctPin = "9657"; // Fixed PIN for demonstration
const [pin, setPin] = useState("");
const [errorMessage, setErrorMessage] = useState("");
const [successMessage, setSuccessMessage] = useState("");
const navigate = useNavigate();
const handlePinComplete = (value) => {
setPin(value);
};
const handleLogin = () => {
// Validate the entered PIN
if (pin === correctPin) {
// PIN is correct
setSuccessMessage("🎉 Welcome back! Login successful!");
setErrorMessage("");
console.log("User logged in successfully");
// Navigate to WelcomePage
navigate("/welcome");
} else {
// PIN is incorrect
setErrorMessage("❌ Incorrect PIN. Please try again.");
setSuccessMessage("");
console.log("Incorrect PIN attempt");
}
};
return (
<div>
<div className="flex flex-col items-center justify-center h-screen bg-black text-white">
<h1 className="text-3xl font-bold mb-6">🔒 Secure Login</h1>
<PinInput
onComplete={handlePinComplete}
length={4}
focus
secret
secretDelay={500}
inputStyle={{ borderColor: "white", color: "white" }}
inputFocusStyle={{ borderColor: "lightgray" }}
/>
<p className="mt-4">Enter your secure PIN to login✨</p>
<button
onClick={handleLogin}
className="mt-8 bg-green-500 text-white py-2 px-4 rounded-md hover:bg-green-600 focus:outline-none focus:ring focus:border-green-300"
>
🚀 Log In
</button>
{errorMessage && (
<div className="mt-6 text-red-600 font-semibold">{errorMessage}</div>
)}
{successMessage && (
<div className="mt-6 text-green-600 font-semibold">
{successMessage}
</div>
)}
</div>
</div>
);
};
export default LoginAuth;

<PinInput/> 元件配置了各種屬性,以增強安全性和使用者體驗。onComplete 屬性被設定為 handlePinComplete 函式,以確保應用程式在使用者完成 PIN 輸入時作出動態響應。

length 道具指定為 4,表示要輸入的 PIN 碼的預期長度,與四位數 PIN 碼的常見安全做法一致。secretsecretDelay 兩個道具用於遮蓋輸入的 PIN 數字,以提高輸入過程中的隱私性和安全性。焦點道具將預設焦點設定在第一個輸入元素上。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// Welcome Page
import React from "react";
const Welcome = () => {
return (
<div className="flex flex-col items-center pt-8 h-screen px-8 text-center bg-gray-100 text-gray-800">
<h1 className="text-4xl font-extrabold mb-6">
📚 Welcome to Maitama High!
</h1>
<p className="text-lg mb-4">
Get ready for an exciting academic journey!
</p>
<div className="text-center">
<p className="mb-4">
Explore the vast resources and opportunities within the school portal.
🏫
</p>
<p>
Your learning adventure begins now. Embrace knowledge, connect with
peers, and excel in every endeavor. 🚀
</p>
<button className="bg-blue-500 text-white px-16 py-2 rounded-full mt-8 hover:bg-blue-600 transition duration-300">
Explore
</button>
</div>
</div>
);
};
export default Welcome;
// Welcome Page import React from "react"; const Welcome = () => { return ( <div className="flex flex-col items-center pt-8 h-screen px-8 text-center bg-gray-100 text-gray-800"> <h1 className="text-4xl font-extrabold mb-6"> 📚 Welcome to Maitama High! </h1> <p className="text-lg mb-4"> Get ready for an exciting academic journey! </p> <div className="text-center"> <p className="mb-4"> Explore the vast resources and opportunities within the school portal. 🏫 </p> <p> Your learning adventure begins now. Embrace knowledge, connect with peers, and excel in every endeavor. 🚀 </p> <button className="bg-blue-500 text-white px-16 py-2 rounded-full mt-8 hover:bg-blue-600 transition duration-300"> Explore </button> </div> </div> ); }; export default Welcome;
// Welcome Page
import React from "react";
const Welcome = () => {
return (
<div className="flex flex-col items-center pt-8 h-screen px-8 text-center bg-gray-100 text-gray-800">
<h1 className="text-4xl font-extrabold mb-6">
📚 Welcome to Maitama High!
</h1>
<p className="text-lg mb-4">
Get ready for an exciting academic journey!
</p>
<div className="text-center">
<p className="mb-4">
Explore the vast resources and opportunities within the school portal.
🏫
</p>
<p>
Your learning adventure begins now. Embrace knowledge, connect with
peers, and excel in every endeavor. 🚀
</p>
<button className="bg-blue-500 text-white px-16 py-2 rounded-full mt-8 hover:bg-blue-600 transition duration-300">
Explore
</button>
</div>
</div>
);
};
export default Welcome;

我們使用 useNavigation 鉤子在登入成功後重定向到歡迎頁面。如果輸入的 PIN 碼與預定義的正確 PIN 碼一致,則 handleLogin 函式會呼叫 navigate 函式。該函式會將使用者引導至” /welcome” 路徑,從而有效地將使用者過渡到歡迎頁面。

接下來,將 WelcomeLoginAuth 元件匯入 App.js 元件。在其中,從 react-router-dom 庫中匯入 BrowserRouter 元件,用於導航。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// App.js
import React from "react";
import { BrowserRouter, Route, Routes } from "react-router-dom";
import LoginAuth from "./LoginAuth";
import Welcome from "./Welcome";
const App = () => {
return (
<div>
<BrowserRouter>
<Routes>
<Route path="/" element={<LoginAuth />} />
<Route path="/welcome" element={<Welcome />} />
</Routes>
</BrowserRouter>
</div>
);
};
export default App;
// App.js import React from "react"; import { BrowserRouter, Route, Routes } from "react-router-dom"; import LoginAuth from "./LoginAuth"; import Welcome from "./Welcome"; const App = () => { return ( <div> <BrowserRouter> <Routes> <Route path="/" element={<LoginAuth />} /> <Route path="/welcome" element={<Welcome />} /> </Routes> </BrowserRouter> </div> ); }; export default App;
// App.js
import React from "react";
import { BrowserRouter, Route, Routes } from "react-router-dom";
import LoginAuth from "./LoginAuth";
import Welcome from "./Welcome";
const App = () => {
return (
<div>
<BrowserRouter>
<Routes>
<Route path="/" element={<LoginAuth />} />
<Route path="/welcome" element={<Welcome />} />
</Routes>
</BrowserRouter>
</div>
);
};
export default App;

當 PIN 碼輸入正確時,使用者會被無縫導航到歡迎頁面。

正確輸入密碼後導航到歡迎頁面

密碼輸入錯誤時,系統會顯示錯誤資訊,說明密碼輸入錯誤。

系統會顯示錯誤資訊

常見挑戰

  • 輸入選項限制:限制只能輸入數字可能會給需要輸入特殊字元(如 @ 或 -)的使用者帶來不便。
  • 手機響應能力:PIN 輸入欄位可能無法很好地適應較小的螢幕。
  • 與表單庫整合:將 PIN 輸入整合到由 Formik 或 Redux Form 等庫管理的大型表單中。
  • PIN 可見性:未掩蓋的 PIN 可能會受到肩部衝浪的影響,即未經授權的個人會觀察到輸入的數字。
  • 資料攔截:被攔截的網路請求可能會暴露敏感的 PIN 資料。

有針對性的解決方案:React-pin-input 如何應對這些挑戰

為了解決常見的 PIN 輸入痛點,React-pin-input 庫配備了各種解決方案來應對常見的 PIN 輸入相關挑戰。其中包括:

  • 可配置的輸入型別:允許使用者根據自己的特定需求在數字和字母數字輸入之間進行選擇。
  • 自定義鍵盤選項:整合帶有更大、間距更均勻按鍵的自定義鍵盤,以改進移動螢幕上的 PIN 輸入。
  • 與表單庫整合:確保 PIN 輸入與表單狀態和驗證無縫整合。
  • 視覺化遮蔽:React-pin-input 允許用點或星號遮蓋 PIN 字元,以提高私密性。
  • 安全資料處理:該庫可在傳輸和儲存過程中對 PIN 進行加密,防止未經授權的訪問。

小結

React-pin-input 使開發人員能夠在其 React 應用程式中建立安全、使用者友好且視覺效果極佳的 PIN 輸入體驗。其靈活性、可訪問性和強大的 API 使其成為無縫、安全地處理敏感使用者資料的首選。

通過了解 PIN 輸入的重要性以及使用 React-pin-input 等庫的優勢,您就可以在 React 應用程式中自信地設計和實施安全、使用者友好的 PIN 輸入解決方案。

評論留言