認証認証
This tutorial is a continuation from my . We are still learning how to build an authentication system with react. Check out to bring you up to speed.
このチュートリアルの手順に従ってくださいhere
ログイン
今、我々は注意を向ける
Login.js
ファイル.あなたが最後の記事から来ているならば、以下のステップの大部分はよく知られています.email
, password
and login
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [login, setLogin] = useState(false);
name
and value
属性をemail
and password
入力フィールド.これは私のです.
{/* email */}
<Form.Group controlId="formBasicEmail">
<Form.Label>Email address</Form.Label>
<Form.Control
type="email"
name="email"
value={email}
placeholder="Enter email"
/>
</Form.Group>
{/* password */}
<Form.Group controlId="formBasicPassword">
<Form.Label>Password</Form.Label>
<Form.Control
type="password"
name="password"
value={password}
placeholder="Password"
/>
</Form.Group>
この時点で、あなたはもはやあなたに入力することはできませんLogin
フォームフィールド.これは、前の状態から現在の状態まで更新するフィールドを設定していないためです.それをしましょうonChange={(e) => setEmail(e.target.value)}
and onChange={(e) => setPassword(e.target.value)}
にemail
and password
入力フィールド.これは私のです.
{/* email */}
<Form.Group controlId="formBasicEmail">
<Form.Label>Email address</Form.Label>
<Form.Control
type="email"
name="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="Enter email"
/>
</Form.Group>
{/* password */}
<Form.Group controlId="formBasicPassword">
<Form.Label>Password</Form.Label>
<Form.Control
type="password"
name="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="Password"
/>
</Form.Group>
これで、フォームフィールドに入力することができますonSubmit={(e)=>handleSubmit(e)}
and onClick={(e)=>handleSubmit(e)}
にform
and button
要素.The onSubmit
フォームの送信を有効にするEnter
キーはonClick
ボタンをクリックしてフォームの提出を有効にします.フォームは以下のようになります.
<Form onSubmit={(e)=>handleSubmit(e)}>
{/* email */}
<Form.Group controlId="formBasicEmail">
<Form.Label>Email address</Form.Label>
<Form.Control
type="email"
name="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="Enter email"
/>
</Form.Group>
{/* password */}
<Form.Group controlId="formBasicPassword">
<Form.Label>Password</Form.Label>
<Form.Control
type="password"
name="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="Password"
/>
</Form.Group>
{/* submit button */}
<Button
variant="primary"
type="submit"
onClick={(e) => handleSubmit(e)}
>
Login
</Button>
</Form>
return
ライン
const handleSubmit = (e) => {
// prevent the form from refreshing the whole page
e.preventDefault();
// make a popup alert showing the "submitted" text
alert("Submited");
}
ボタンをクリックするか、Enterキーを押すと、結果は次のようになります.関数の構築
alert
関数からのステートメント
npm i axios
import axios from "axios";
// set configurations
const configuration = {
method: "post",
url: "https://nodejs-mongodb-auth-app.herokuapp.com/login",
data: {
email,
password,
},
};
The method
データの処理方法を示します.url
API関数がアクセスするエンドポイントですdata
すべての入力またはrequest body
バックエンドが期待していること.うまくいけばそれは十分明確です.
axios(configuration)
これでAPIコールが完了しました.しかし、実際に成功したかどうかを確認する必要があります.そして、おそらく我々のユーザーに結果を表示します.それで、それを修正するために、我々はそれから使用します...キャッチ.ブロック
// make the API call
axios(configuration)
.then((result) => {console.log(result);})
.catch((error) => {console.log(error);})
テスト用のコンソールにログインしていますもちろん、ログインしようとした結果をチェックするためにコンソールにユーザーを指示しません.では、ユーザに伝える方法を見つけましょう
// make the API call
axios(configuration)
.then((result) => {
setLogin(true);
})
.catch((error) => {
error = new Error();
});
設定によってlogin
to true
, ログイン処理が完了したときに、今すぐ教えてください.では、ユーザに伝えましょうForm
元素
{/* display success message */}
{login ? (
<p className="text-success">You Are Logged in Successfully</p>
) : (
<p className="text-danger">You Are Not Logged in</p>
)}
このコードは、login
is true
. さあ試してみましょうこれは私のです.
あなたが私のものと同じ結果を得ているならば、あなたはそれをしました!
あなたは素晴らしい
結論
我々は、我々が出発したところから、このチュートリアルを始めました.我々はすでに登録したユーザーにログインする方法を見てきました.
All codes are here
次に、見ていきます.
ジャストスティックアラウンド
Reference
この問題について(認証認証), 我々は、より多くの情報をここで見つけました https://dev.to/ebereplenty/react-authentication-login-h3iテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol