実現したいこと
GASにて、ログイン認証にトライしています。
認証コードを送り、認証画面で認証コードを入力し成功するとindex.htmlに遷移します。失敗ならerror.htmlに遷移します。
ボールドテキスト
該当のソースコード
【html】 <body> <div class="form-wrapper"> <h1>認証コード入力</h1> <div class="form-item"> <label for="verificationCode"></label> <input type="text" id="verificationCode" name="verificationCode" required="required" placeholder="認証コード" /> </div> <div class="button-panel"> <input type="submit" class="button" title="Verify" value="認証" onclick="verifyCode()"></input> </div> </div> <script> function verifyCode() { var verificationCode = document.getElementById('verificationCode').value; google.script.run.withSuccessHandler(onVerificationResult).processVerificationCode(verificationCode); } function onVerificationResult(result) { alert(result); // 結果をダイアログで表示 } </script> </body>
【gas】 function processVerificationCode(verificationCode) { try { const b2Value = SpreadsheetApp.openById(passwordSpreadsheetId) .getSheetByName("パスワード").getRange('B2').getValue(); if (verificationCode === b2Value) { return '認証成功!'; } else { return '認証失敗:認証コードが正しくありません。'; } } catch (error) { return 'エラーが発生しました:' + error.message; } }
試したこと
ここまでで、両方の値があっていたら「認証成功」,
間違っていたら「認証失敗」までダイアログに表示することができましたが、htmlに遷移するのが失敗続きです。
doPost(e)にかえてみたり、
HtmlService.createHtmlOutput('<script>window.location.href="index.html";</script>');
にしてみたりしましたが、「null」が返ってきて先に進みません
まだ勉強し始めて2週間の初心者なので、色々調べながらデプロイしたりしましたが
よくわからなくなってきました。ただ、ページに移動したいだけなのに。。。
どなたか優しい方よろしくお願いします。

0 コメント