Login Logout SweetAlert2



code.gs
function doGet() {
  return HtmlService.createTemplateFromFile('index')
  .evaluate()
  .setTitle('FormLogin')
  .setFaviconUrl('https://ik.imagekit.io/kmpeld0nh/GoshenN%20(3).png')
  .addMetaTag('viewport', 'width=device-width, initial-scale=1')
  .setSandboxMode(HtmlService.SandboxMode.IFRAME)
  .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)

}

/**Include */
function include(filename){
  return HtmlService.createHtmlOutputFromFile(filename).getContent()
}
**************************************

index.html
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.7.28/dist/sweetalert2.all.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@11.7.28/dist/sweetalert2.min.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">

  </head>
  <body>
    <div class='container' id='phome'>
    <button class="btn btn-primary" onclick="login()">Login</button>
    <?!= include('home'); ?>
    </div>
  <div class='container' id='pcontent' style='display:none'>
  <button class="btn btn-danger" onclick="logout()">Logout</button>
    <?!= include ('content'); ?>
  </div>

    <script>
      window.onload = function(){
       login()
       }

  function login(){
Swal.fire({
  title: 'Login Form',
  html: '<input type="text" id="login" class="swal2-input" placeholder="Username"><input type="password" id="password" class="swal2-input" placeholder="Password">',
  confirmButtonText: 'Sign in',
  focusConfirm: false,
  preConfirm: () => {
    const login = Swal.getPopup().querySelector('#login').value
    const password = Swal.getPopup().querySelector('#password').value
    if (!login || !password) {
      Swal.showValidationMessage(`กรุณากรอก username และ password ให้ถูกต้อง`)
    }
    return { login: login, password: password }
  }
}).then((result) => {
const user = result.value.login
const pwd = result.value.password
if (user == 'admin' && pwd == '1234'){
  document.getElementById('phome').style.display = 'none';
  document.getElementById('pcontent').style.display = 'block';
}else{
Swal.fire({
title: 'รหัสผ่านไม่ถูกต้อง?',
  // text: "You won't be able to revert this!",
   icon: 'error',
  showCancelButton: true,
   confirmButtonColor: '#3085d6',
   cancelButtonColor: '#d33',
   confirmButtonText: 'กรอกรหัสผ่านใหม่!'
 }).then((result) => {
   if (result.isConfirmed) {
    login()
   }
})
}
})
 }

function logout(){
    Swal.fire({
  title: 'ออกจากระบบ!',
  text: "คุณต้องการออกจากระบบ ใช่หรือไม่?",
  icon: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  confirmButtonText: 'ตกลง!',
  cancelButtonColor: '#d33',
  cancelButtonText: 'ยกเลิก',
}).then((result) => {
  if (result.isConfirmed) {
  document.getElementById('phome').style.display = 'block';
  document.getElementById('pcontent').style.display = 'none';
  }
})
}
    </script>
  <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

  </body>
</html>

*****************

*************
home.html
<center><h1>Home</h1></center>

<table class="table">
  <thead>
    <tr>
      <th scope="col">#</th>
      <th scope="col">First</th>
      <th scope="col">Last</th>
      <th scope="col">Handle</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">1</th>
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
      <th scope="row">2</th>
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
      <th scope="row">3</th>
      <td colspan="2">Larry the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>

**********************
content.html
  <center><h1>หน้าที่ 2 เข้าระบบสำเร็จ!!</h1></center>
  <div class="accordion" id="accordionExample">
  <div class="accordion-item">
    <h2 class="accordion-header">
      <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
        <!-- หน้า Admin เข้าระบบสำเร็จ!! -->
      </button>
    </h2>
    <div id="collapseOne" class="accordion-collapse collapse show" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the first item's accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
        Accordion Item #2
      </button>
    </h2>
    <div id="collapseTwo" class="accordion-collapse collapse" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the second item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
        Accordion Item #3
      </button>
    </h2>
    <div id="collapseThree" class="accordion-collapse collapse" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the third item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
</div>


ไม่มีความคิดเห็น:

แสดงความคิดเห็น

หมายเหตุ: มีเพียงสมาชิกของบล็อกนี้เท่านั้นที่สามารถแสดงความคิดเห็น