htmlgoogle-sheetsgoogle-apps-scripthttp-redirectweb-applications

Webapp does not redirect the page


Google Apps Script does not redirect the page due to sandboxFrame... How can I solve this problem?

The workspace is Google Sheets and Google Apps Script, html pages.

login.html

<!DOCTYPE html>
<html>
<head>
    <base target="_top">
    <style>
        body { font-family: Arial, sans-serif; }
        .form-container { margin: 20px; }
        .form-input { padding: 10px; margin-bottom: 10px; width: 100%; }
        .form-submit { padding: 10px 20px; background-color: #4CAF50; color: white; border: none; cursor: pointer; width: 100%; }
        .form-submit:hover { background-color: #45a049; }
    </style>
</head>
<body>
    <div class="form-container">
        <h2>Oturum Aç</h2>
        <form>
            <input class="form-input" type="text" id="username" name="username" placeholder="Kullanıcı Adı" required><br>
            <input class="form-input" type="password" id="password" name="password" placeholder="Şifre" required><br>
            <input class="form-submit" type="button" value="Giriş Yap" onclick="submitForm()">
        </form>
    </div>
    
    <script>
        function submitForm() {
            var username = document.getElementById("username").value;
            var password = document.getElementById("password").value;
            
            google.script.run.withSuccessHandler(loginSuccess).authenticateUser(username, password);
        }
        
        function loginSuccess(isAuthenticated) {
            if (isAuthenticated) {
                google.script.run.withSuccessHandler(function() {
                    google.script.host.close();
                }).setUserLoggedIn();
            } else {
                alert("Kullanıcı adı veya şifre hatalı. Tekrar deneyin.");
            }
        }
    </script>
</body>
</html>

main.html

<!DOCTYPE html>
<html>
<head>
    <base target="_top">
    <style>
        /* CSS stil tanımları */
        body { font-family: Arial, sans-serif; }
        .logout-button { padding: 10px 20px; background-color: #f44336; color: white; border: none; cursor: pointer; }
        .logout-button:hover { background-color: #da190b; }
    </style>
</head>
<body>
    <h2>Hoşgeldiniz!</h2>
    <button class="logout-button" onclick="logout()">Oturumu Kapat</button>
    
    <script>
        function logout() {
            google.script.run.withSuccessHandler(function() {
                google.script.host.close();
                window.location.href = 'login.html';
            }).logoutUser();
        }
    </script>
</body>
</html>     

code.gs

function doGet() {
    var userProperties = PropertiesService.getUserProperties();
    var loggedIn = userProperties.getProperty('loggedIn');
    
    if (loggedIn === 'true') {
        return HtmlService.createHtmlOutputFromFile('main').setTitle('Ana Sayfa');
    } else {
        return HtmlService.createHtmlOutputFromFile('login').setTitle('Oturum Aç');
    }
}

function authenticateUser(username, password) {
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('user');
    var dataRange = sheet.getDataRange();
    var values = dataRange.getValues();
    
    for (var i = 0; i < values.length; i++) {
        var savedUsername = values[i][0]; // assuming usernames are in column A
        var savedPassword = values[i][1]; // assuming passwords are in column B
        
        if (savedUsername === username && savedPassword === password) {
            var userProperties = PropertiesService.getUserProperties();
            userProperties.setProperty('loggedIn', 'true');
            return true; // Kullanıcı doğrulandı
        }
    }
    
    return false; // Kullanıcı doğrulanamadı
}

function setUserLoggedIn() {
    var userProperties = PropertiesService.getUserProperties();
    userProperties.setProperty('loggedIn', 'true');
}

function logoutUser() {
    var userProperties = PropertiesService.getUserProperties();
    userProperties.deleteProperty('loggedIn'); // Oturumu sonlandır

    return HtmlService.createHtmlOutput("<script>window.top.location.href = '/';</script>");
}


Solution

  • This issue is occurring because Google Apps Script places certain restrictions to protect users from malicious code. More information can be found here.