JS/Jquery and firebase login page issues - javascript

I am trying to build a login page. After a user has logged in correctly I want to exit the index.html page and redirect to my portfolio.html page.
firebase.auth().onAuthStateChanged(user => {
if(user) {
window.location = 'portfolio.html';
} else {
// No user is signed in.
// window.location = 'index.html';
console.log("Not logged in");
}
});
The code above does switch pages, but then constantly reloads the page over and over again in a loop. I am very stuck friends:(.
Thank you for your time.

Isn't it like, you got redirected to portfolio.html, the auth code is exectuted, the user object is already there, so it redirects you to portfolio again. Try adding
if(window.location.indexOf("portfolio.html") === -1){
//redirection here
}

I guess the problem is that the piece of code is also executed in your portfolio.html. So when the page is loaded, firebase checks if there is a user logged in from web storage and in your case, the user is logged in previously. So the listener will be triggered again and reload the page.

Related

Stripe Elements JS redirect user

I'm following the guide on Stripes docs https://stripe.com/docs/billing/subscriptions/fixed-price
Everything works, I just cannot work out how to actually redirect the user from the JS
function onSubscriptionComplete(result) {
// Payment was successful.
if (result.subscription.status === 'active') {
console.log('redirect');
location.href = 'https://google.com';
}
}
Here the console is logging redirect but the user is not redirected. What is the best way to redirect the user after confirming the payment?

Google Firebase - Polymerfire, getting a callback on the initial application initialization

Is there anyway to do something like this with firebase.
var loaded = false;
var app = firebase.initializeApp(config);
app.then(function(user) {
// initialization complete
loaded = true;
if(user){
// user logged in
}
else
{
// user not logged in
}
});
The above solution, if possible, would provide me with the knowledge that a user has or has not been logged in, as well as the information the application is attempting to get a user.
I can then only show the page to the user once I know that the page is not going to change in the next second.

Firebase: Store anonymous authentication in cookies

I am developing multi page application, that uses firebase as a back-end.
On every page after firebase initialization I have the following code:
firebase.auth().signInAnonymously().catch(...);
firebase.auth().onAuthStateChanged(function(user)
{
if (user) {
// ...
} else {
// User is signed out.
// ...
}
// ...
});
which sign in every visitor anonymously.
The problem is, when user navigates to another page of my application or opens it in a new tab, or just refreshes the current page - new anonymous user is generated.
I can use client side cookies to store anonymous user id, but how can I later (after page refresh) use that stored anonymous user id, to sign in user with that id?
It was my mistake,
firebase store anonymous user authorization in local storage by default.
My code sample should be rewritten this way to work properly:
firebase.auth().onAuthStateChanged(function(user)
{
if (user) {
// User is signed in
// ...
} else {
// User is signed out.
firebase.auth().signInAnonymously().catch(/*...*/);
}
// ...
});

how to prevent user from accessing webpage directly in node.js?

Hello I am having an issue preventing a user from accessing my site by typing in the URL. I have a login which I want to be the only way to access my /factory/homepage.html page.
But if a user types /factory/homepage.html into the URL field, they can access the homepage.html.
Im already using app.get('/factory/'), but my res.redirect('/website/userLogin.html') does not redirect to the login page.
Here is my code:
app.get('/factory/*', function(req,res,next){
console.log("going through app.get...");
res.redirect('../website/userLogin.html');
console.log('after redirect...');
});
The output is:
going through app.get...
after redirect...
And the user is taken to factory/homepage.html. I also tried app.use('/factory/*'), but no luck. The redirecting only works if a path that doesn't exist in my factory directory is entered, for example /factory/hello.html will redirect to /website/userLogin.html
I'll like for the directory /factory to not be accessible through typing in the url. Then after i will implement a middleware function to check if the user is authenticated.
Any help will be appreciated!
I'd try something like this.
app.get('/factory/*', function(req,res,next){
var authenticated = false;
//do something to authenticate user
if(authenticated === true){
//user is already authenticated
res.redirect('/factory/homepage.html');
}else{
//redirect to login
res.redirect('../website/userLogin.html');
}
});

FB.logout() - no access token - "Log in as Different User"

This question is in relation to this question: FB.logout() called without an access token
I had a question i was hoping someone could help with.
I get everything in the link above, but how do you handle when someone uses a public computer and leaves themselves logged in to FB by accident? Then a new user tries to log in to an app, but it is not them. I want to have a button to log them out of FB altogether to "log in as a different user".
I cannot do that until they authorize the app, right? So they need to authorize the app under someone else's account and then log out? There has to be another way.
Any feedback would be great - tried searching for a while on this, but to no avail. I might be over thinking it, but there should be some way to "log in as a different user".
You can do something like:
FB.getLoginStatus(function(response) {
if (response.status === "connected") {
FB.api("me", function(response2) {
// notify the user he is logged in as response2.name, and if it's not him call FB.logout
}
}
else {
// prompt the user to login
}
}
Modify your logout to something like this :-
function logout(response){
console.log("From logout" + response);
if(!response.authResponse){
window.location = "/web/login/";
return;
}
FB.logout(function(response){
FB.Auth.setAuthResponse(null,'unknown');
logout();
});
}
i.e keep on sending logout request to Facebook till the actual logout happens.
Try to use Session and save the user's ID in that session.
You can control the expiration time of that session( by default its 20 minute if the user doesn't have any activity on the page)
then in each page Load even check whether the season is still active for that user or not.
page Load event:
if(Session["LoggedIn"]==null)
{
Response.Redirect("~/Login.aspx");
}
This code above will redirect user to login page of your website if the session has expired.

Categories