Meteor - บัญชี

แพคเกจนี้ช่วยให้สามารถตรวจสอบผู้ใช้งานได้อย่างสมบูรณ์ คุณสามารถเพิ่มได้โดยเรียกใช้รหัสต่อไปนี้ในหน้าต่างพรอมต์คำสั่ง

C:\Users\username\Desktop\meteorApp>meteor add accounts-password

ตัวอย่างการพิสูจน์ตัวตน

ตัวอย่างนี้จะแสดงการพิสูจน์ตัวตนพื้นฐาน เราจะสร้างregister, loginและ homeเทมเพลต หากมีไฟล์currentUser (หากผู้ใช้ลงทะเบียนหรือเข้าสู่ระบบสำเร็จ) ไฟล์ homeแม่แบบจะแสดงขึ้น ถ้าไม่มีcurrentUser, register และ login แม่แบบจะมองเห็นได้

eorApp.html

<head>
   <title>meteorApp</title>
</head>
 
<body>
   {{#if currentUser}}
      {{> home}}
      {{else}}
      {{> register}}
      {{> login}}
   {{/if}}
</body>

<template name = "register">
   <h2>REGISTER:</h2>
   <form>
      <input type = "email" name = "registerEmail"><br>
      <input type = "password" name = "registerPassword"><br>
      <input type = "submit" value = "Register"><br>
   </form>
</template>

<template name = "login">
   <h2>LOGIN:</h2>
   <form>
      <input type = "email" name = "loginEmail"><br>
      <input type = "password" name="loginPassword"><br>
      <input type = "submit" value = "Login"><br>
   </form>
</template>

<template name = "home">
   <p>You're logged in.</p>
   <button class = "logout">Logout</button>
</template>

ขั้นแรกเราต้องสร้างไฟล์ registerเหตุการณ์. ฟังก์ชันนี้จะอ่านอินพุตรีจิสเตอร์สร้างผู้ใช้ใหม่และจัดเก็บไว้ในฐานข้อมูล

เหตุการณ์ที่สองคือ login. คราวนี้ฟังก์ชั่นจะอ่านอินพุตจากไฟล์login แม่แบบเข้าสู่ระบบผู้ใช้หากอีเมลและรหัสผ่านถูกต้องหรือส่งคืนข้อผิดพลาดหากไม่เป็นเช่นนั้น

และสุดท้าย logout เหตุการณ์จะถูกใช้เพื่อล็อกเอาต์ผู้ใช้เมื่อคลิกปุ่ม

eorApp.js

if (Meteor.isClient) {

   Template.register.events({
      'submit form': function(event) {
         event.preventDefault();

         var registerData = {
            email: event.target.registerEmail.value,
            password: event.target.registerPassword.value
         }

         Accounts.createUser(registerData, function(error) {
         
            if (Meteor.user()) {
               console.log(Meteor.userId());
            } else {
               console.log("ERROR: " + error.reason);
            }
         });
      }
   });

   Template.login.events({
   
      'submit form': function(event) {
         event.preventDefault();
         var myEmail = event.target.loginEmail.value;
         var myPassword = event.target.loginPassword.value;
			
         Meteor.loginWithPassword(myEmail, myPassword, function(error) {

            if (Meteor.user()) {
               console.log(Meteor.userId());
            } else {
               console.log("ERROR: " + error.reason);
            }
         });
      }
   });

   Template.home.events({

      'click .logout': function(event) {
         event.preventDefault();
			
         Meteor.logout(function(error) {

            if(error) {
               console.log("ERROR: " + error.reason);
            }
         });
      }
   });
}

เมื่อแอปเริ่มต้นเราจะได้รับหน้าต่อไปนี้

ในการป้อนอีเมลและรหัสผ่านในไฟล์ registerแบบฟอร์มเราสามารถลงทะเบียนและเข้าสู่ระบบผู้ใช้ใหม่ เราจะเห็นว่าคอนโซลบันทึกผู้ใช้id และ home เทมเพลตถูกแสดงผล

loginเหตุการณ์จะตรวจสอบฐานข้อมูลและเข้าสู่ระบบผู้ใช้หากอีเมลและรหัสผ่านถูกต้อง มิฉะนั้นคอนโซลจะบันทึกข้อผิดพลาด

หากผู้ใช้คลิกไฟล์ LOGOUT แอพจะล็อกผู้ใช้ออกและแสดงไฟล์ register และ login เทมเพลต