Subversion Repositories SmartDukaan

Rev

Rev 35458 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

var googleProfile;
var partnerName;

$(function() {

        $('form').on('submit', function() {
                if($(this).find("#password").length > 0) {
                        login();
                } else {
                        var email=$("#email").val();
                        console.log(email);
                        if(email==""|email==null||email==undefined)
                                {
                                        alert("Input field email can't be empty");
                                        return;
                                }
                        forgetPassword(email);
                }
                return false;
        });
        
        $(document).on('click', ".forgetPassword", function() {
                $("#auth").toggle();
                $('#forget-password').toggle();
        });
});
$(document).ajaxComplete(function() {
});

function login() {
        console.log("Login DashBoard");
        var emailIdOrMobileNumber = $("#emailormobile").val();
        console.log(emailormobile);
        var password = $("input[type='password']").val();
        if (emailIdOrMobileNumber == "" || emailIdOrMobileNumber == undefined || emailIdOrMobileNumber == null) {
                
                        alert("Input field emailIdOrMobileNumber can't be empty")
                        return;
                }
        if (password == "" || password == null || password == undefined) {
                alert("password can't be empty")
                return;
        }
        submitUser(null, emailIdOrMobileNumber, password)
}

function onSignIn(googleUser) {
        var profile = googleUser.getBasicProfile();
        submitUser(googleUser, profile.getEmail(), null);
}

function onLoad() {
        gapi.load('auth2', function () {
                gapi.auth2.init();
        });
}

// Function to handle sign-out
function signOut() {
        var auth2 = gapi.auth2.getAuthInstance();
        auth2.signOut().then(function () {
                console.log('User signed out.');
                // Add your custom logic for sign-out here
        });
}

// Function to toggle between sign-in and sign-out
function toggleSignInSignOut() {
        var auth2 = gapi.auth2.getAuthInstance();
        if (auth2.isSignedIn.get()) {
                // User is signed in, sign them out
                signOut();
        } else {
                // User is not signed in, initiate sign-in
                auth2.signIn().then(onSignIn);
        }
}

function submitUser(googleUser, emailIdOrMobileNumber, password) {

        if (googleUser) {
                var params = {
                        "token" : googleUser.getAuthResponse().id_token,
                        "emailIdOrMobileNumber" : emailIdOrMobileNumber,
                        "password" : ""
                };
        } else {
                var params = {
                        "token":"",
                        "emailIdOrMobileNumber" : emailIdOrMobileNumber,
                        "password" : password
                };
        }
        // var response = doPostAjaxRequestWithParams(context+"/login", params);
        doAjaxRequestWithParamsHandler(context + "/login", "POST", params,
                        function(response) {
                                response = JSON.parse(response);
                                if (response.status) {
                                        if(response.name)
                                                {
                                                partnerName=response.name;
                                                }
                                        addProfileInLocalDb();
                                        if (!!window.location.search) {
                                                const urlParams = new URLSearchParams(window.location.search);
                                                if (urlParams.get("redirect") !== null) {
                                                        window.location.href = `${context}${urlParams.get("redirect")}`;
                                                        return;
                                                }
                                        }
                                        window.location.href = response.redirectUrl;
                                } else {
                                        alert("Retailer not found, Please login");
                                }
                        });

}
function forgetPassword(emailId)
{
        var params={"emailId":emailId };
        doAjaxRequestWithParamsHandler(context + "/forgetPassword", "POST", params, function(response) {
                console.log(response, typeof response);
                if(response=="true") {
                        alert("Your password has been sent to your email, Please login");
                        setTimeout(function(){ window.location.reload(); }, 5000);
                }
        });
}

function addProfileInLocalDb() {
        if(googleProfile){
        var profile = {
                'image_url' : googleProfile.getImageUrl(),
                'name' : googleProfile.getName()
        };
        console.log(googleProfile.getImageUrl);
        console.log(googleProfile.getName());
        localStorage.setItem("profile", JSON.stringify(profile));
}
        else
                {
                var profile = {
                                'image_url' : "",
                                'name' : partnerName
                        };
                        console.log(partnerName);
                        //console.log(googleProfile.getName());
                        localStorage.setItem("profile", JSON.stringify(profile));
                }
        }