How can I use localStorage to store login credentials for my application? I'm thinking it will store it somewhere when I exit the app, and then when I open the application again the fields will be prefilled with the information from localStorage. Here is my code so far.
function onDeviceReady() {
alert("ready");
var email = window.localStorage.getItem("email");
var password = window.localStorage.getItem("password");
document.getElementById("email").value = email;
document.getElementById("password").value = password;
}
//If checkbox gets checked then save credentials, if unchecked then forget
function rememberMe() {
if(document.getElementById('remember_me').checked) {
alert("checked");
window.localStorage.setItem("email", document.getElementById("email").value);
window.localStorage.setItem("password", docuement.getElementById("password").value);
}
else {
alert("unchecked");
window.localstorage.clear();
}
}
Best way to do it is use webSQL or SQLite database. save credentials when anybody tries to login and then when someone opens the app, fill the boxes by reading data from database on pageload or device ready function.
localStorage can hold object´s data until it gets removed from the same, but sessionStorage will clear the data when you close the tab/browser.
Related
I'm trying to run a function when body loads, that will check if the user is logged in, and if not redirect them to the login page. Here is the login function :
function login() {
var mail = document.getElementById('mail').value; //get values
var psw = document.getElementById('psw').value; //get values
localStorage.setItem('logged_in', true); //specify that user is logged in
localStorage.setItem('mail', mail); //store mail
window.location.replace('pages/home.html'); //redirect to home page
}
And the function to check if the user is logged in :
function check_logged_in() {
const logged_in = localStorage.getItem('logged_in');
if (logged_in == null) { //check if user is logged in
alert('You are not logged in, you are about to be redirected. '); //alert user
window.location.replace("../index.html"); //redirect to login page
}
}
The problem is that even if the login function run before, I am redirected. I think that the localStorage resets on each redirection. If that is the problem, do you know the way to prevent this, or if it isn't the problem, do you know what it might be?
Based on the documentation a localStorage item can either be null (when it's empty) or a string. It does not store any other data types.
The expression
localStorage.setItem("logged_in", true);
Does not save a boolean value to the localStorage item. instead, it saves the string value "true"
Tried hours on this one now - is there any way to add a parameter to the URL on server side Google Script in a deployed web app, that is open for anyone (not only Google accounts)?
I want to store a key for when a user is logged in, so that the user doesn't have to log in again each time the browser is refreshed.. Say my current url is:
https://script.google.com/a/macros.../exec
Can I in some way add in a GAS function at login so that the URL gets modified to something like
https://script.google.com/a/macros.../exec?key=abcdef&name=John%20Doe
So that if the doGet(e) function runs again (website reloaded) and if the temporary generated key matches the username in my database, the user will be directed to page B, and if the key doesn't match (or have been deleted on a time trigger) the user will be directed to page A (the login page)?
I also tried to find ways of putting something in the local cache but can't figure that out either.. Google CacheService seem to only store keys on me as a user and not client side.
Example of what I want to achieve:
function doGet(e){
var name = e.parameter.name, output;
if(name === undefined){
/*/
Add parameter name=john to URL..
obviously what I intend to is to create a HtmlOutput on a login-page
and then in a later (function login()) add this parameter there and not
here, but if anyone can work this out for me I got the solution
/*/
} else {
output = HtmlService.createHtmlOutput('Welcome back ' + name);
}
return output;
}
Here is a possible solution that saves usernames to the PropertiesService based on the URL parameters provided, and if already exists, gets the key.
function doGet(e) {
//use the session service to get the active users email
var currentUser = e.parameter.name;
var key = e.parameter.key;
var props = PropertiesService.getUserProperties().getProperty(currentUser);
if(props) {
//do something with the key
} else {
//if there is no username key in the properties service, then add it
PropertiesService.getUserProperties().setProperty(currentUser, key);
}
}
Here are the docs:
https://developers.google.com/apps-script/reference/properties/properties-service#getUserProperties()
I'm trying to do Remember Me with an interesting algorithm. I was thinking, when i click the Remember Me and Login, the value of the text fields (username&password) will be saved as a default value. My login button click event here:
var username = Ext.getCmp('setNo').getValue();
var password= Ext.getCmp('setPass').getValue();
if(refs.checkStudent.value === true){
Ext.getCmp('setNo').setValue(username);
Ext.getCmp('setPass').setValue(password);
}
else{
Ext.getCmp('setNo').setValue("");
Ext.getCmp('setParola').setValue("");
}
On console, it is working. But I'm working with a local store, no server. So when i refresh the page, it's gone. Is there a way to not lose them?
on your view onAfterRender event:
Ext.getCmp('setNo').setValue(localStorage.username);
Ext.getCmp('setPass').setValue(localStorage.password);
on your button click event:
if (refs.checkStudent.value === true) {
localStorage.username = Ext.getCmp('setNo').getValue();
localStorage.password = Ext.getCmp('setPass').getValue();
} else {
localStorage.removeItem('username');
localStorage.removeItem('password');
}
Use ExtJs utility to set and get values in cookies. At time of login set
username and password in cookies and after refresh the page read username
and password value from the cookie.
Ext.util.Cookies.set('username', username); // To set value in cookie.
Ext.util.Cookies.get('username'); // to get value form cookie.
I have an onbeforeunload and it is working fine. However, I wanted a way to trigger my DB method to delete my existing item if the user choose to leave the page. How is it possible to detect the result as well as to trigger my DB method? My DB method is on the server-side.
Here is my javascript for the onbeforeunload:
var showMsg = true;
window.onbeforeunload = function () {
if (showMsg) {
return "You must publish your profile. Leaving this page will delete the profile information you have entered."
}
}
I am working on one web application in which i want to make sure that website allow only one login per user at a time.
when user login i am updating table column islogged=1 when logoff i am updating islogged =0
but the problem is when user session timeout or if user closing browser i am unable to capture this events.i am using sqlserver state to store sessions so Session_End event is not fired in this state .
it only works in InProc State. and also in browser close event i have tried onunload event. but this event is fired on tab closed also. not able to differentiate tab close and browser close.
i want only one concurrent user login. is there any other way to achieve this.
i am using this code to capture browser close event.
<body class="fixed-top" onclick="clicked=true;" onunload="CheckBrowser()">
<script type="text/javascript">
var clicked = false;
// var xmlHttp
// var browser = navigator.appName;
function CheckBrowser() {
debugger
// var browserClose = isUserClickedCloseButton();
if (clicked == false) {
$.ajax({
url: "#Url.Content("~/Account/LogOff")",
type: 'Post',
success: function (data) {
},
error: function () { alert("Error On Logut..."); }
});
}
alternatively you can store store session id in DB along with the current loggedin status as true or false. when user hits just check if loggedin flag is true and take the action needed
Please check the below links which has similar Q&A.
Only one concurrent login per user in Asp.net
Only one concurrent login per UserID in Asp.net