Possibility to create another User while logged in - javascript

I'm creating a game using Parse.com as back-end. I want to create another user in Parse database while I'm still logged in. I'm using Parse user.signUp() method to register another user, since Parse.com provides it as a standard method for user registration. But I don't want the current user's session to expire. Currently, it works but as soon as another user is registered, the new user's session is directly logged in.
Here is the case for your information:
I want the user (e.g. myself here) to enter the email for user to invite other user to this game. When user enters email id, an email is sent actually (using PHP) and then the function creates a temporary user. For this I'm using the following code:
function inviteFriend() {
var email = $("#inviteEmail").val();
var pass = 'randomPass123';
var varData = 'email=' + email;
console.log(varData);
if (email != null) {
$.ajax({
type : 'POST',
url : './src/php.php',
data : varData,
success : function() {
alert("Invite sent successfully.");
var user = new Parse.User();
user.set("username", "tempUser");
user.set("password", pass);
user.set("email", email);
user.signUp(null, {
success : function(user) {
alert("User created successfully.");
console.debug(user);
},
error : function(user, error) {
console.log("LogIn error := " + error.message);
}
});
}
});
}
}
Please suggest any workarounds.

Related

Firebase Web unlink password success but the email still exist in console

I have some function to Unlink a Password Provider from a user using this code :
var user = firebase.auth().currentUser;
user.providerData.forEach(function(providerData) {
console.log(providerData.providerId);
if (providerData.providerId == "password") {
user.unlink("password").then(function() {
alert("success");
}).catch(function(error) {
alert(error);
});
}
});
The above code is successful without any problem, i can't log in with the email that already unlinked which is great.
But when I check in my console, the email still exists and I can't create a new user with the same name and got the error User already exists.
Is this a Bug ? or is there anything that I miss?
I can unlink user data in Android just fine and the user that unlinked will be deleted too, but not on the web.
EDIT :
I'm using this code to link the Password to the current user, maybe this will help
var user = firebase.auth().currentUser;
var credential = firebase.auth.EmailAuthProvider.credential(vartxtEmail, vartxtPassword);
user.linkWithCredential(credential)
.then(function(usercred) {
alert("success");
}).catch(function(error) {
alert(error);
});

Meteor change database at run time

I have three database i.e, main_db it is default load database. I want load database after login.
Database are:-
main_db
->user_collection
psm_2017_db
->abc_collection
->xyz_collection
psm_2018_db
->abc_collection
->xyz_collection
Here is my project structure
here is my login script.
client
|->login
|->login.js
Template.login.rendered = function(){
SessionStore.set("login_user",false);
};
Template.login.events({
'submit #formLogin': function (event, target){
event.preventDefault();
var email = target.find('#loginEmail').value;
var password = target.find('#loginPassword').value;
// console.log(email +" "+password);
Meteor.loginWithPassword(email, password, function(err){
if(err){
console.log(err);
alert("Invalid Login!");
}
else {
SessionStore.set("login_user",true);
console.log('successfully')
Router.go("/dashboard")
}
});
}
});
Template.layout.helpers({
"isLoggedin": function () {
return SessionStore.get("login_user");
}
});
here is my load collection file
lib
|->collection.js
abcCollection=new Mongo.Collection("abc_collection");
xyzCollection=new Mongo.Collection("xyz_collection");
You can connect to multiple dbs using the below approach.
var database = new MongoInternals.RemoteCollectionDriver("<<mongo url>>");
MyCollection = new Mongo.Collection("collection_name", { _driver: database });
<<mongo_url>> is your standard mongodb url.
Eg. mongodb://127.0.0.1:27017/database_name
Now, in your specific scenario, main_db contains the user collection (I'm under the assumption that this is pertaining to meteor user collection). You need to have this loaded at all times. You can't have it load after login since user information - which is required for logging in resides in that db!
Once you take care of the above, connecting to the remaining two dbs can be done on login as below:
/lib/dbconnection.js (this will be common to both server and clinet)
Meteor.methods({
loadDB: function(){
if(Meteor.userId()){ // if a user has logged in
var database = new MongoInternals.RemoteCollectionDriver("<<mongo url>>");
MyCollection = new Mongo.Collection("collection_name", { _driver: database });
}
}
})
Meteor.call("loadDB");
loadDB will get called each time a user logs in. But I fear that it will be run each time any user logs in. In order to avoid it being re-initialized for each user login, you might want to do a check on whether database or myCollection already exists.

How do I set a cookie in iOS Phonegap?

I'm using the reddit API and can successfully login and receive a cookie value in return. The code I use is as follows
loginPost = $.ajax({
type: 'POST',
url: 'http://www.reddit.com/api/login/' + username + '?user=' + username + '&passwd=' + password + '&api_type=json',
xhrFields: {
withCredentials: true
},
dataType: "json",
success: function() {
console.log("Define response variables");
var header = loginPost.getAllResponseHeaders();
var responseText = loginPost.responseText;
var match = header.match(/(Set-Cookie|set-cookie): reddit_session=(.+?);/);
if (match) {
reddit_session = match[2];
window.localStorage.setItem("reddit_session", reddit_session);
window.localStorage.setItem("reddit_username", username);
window.localStorage.setItem("reddit_password", password);
console.log("Logged in!");
//alert(responseText);
$('.loginWrapper').slideUp('fast', function() {
$('#feedWrapper').css("top", "44px");
$('#feedWrapper').css("bottom", "0");
// Animation complete.
});
}
else {
reddit_session = null;
window.localStorage.setItem("reddit_session", null);
navigator.notification.alert('Your username or password is incorrect. Please try again.');
console.log("Login Failed");
}
},
});
I am storing the cookie using
var header = loginPost.getAllResponseHeaders();
var match = header.match(/(Set-Cookie|set-cookie): reddit_session=(.+?);/);
if(match){
reddit_session = match[2];
window.localStorage.setItem("reddit_session", reddit_session);
But the cookie isn't sent with a simple request to http://www.reddit.com/api/me.json. All that is returned is an empty JSON response like this {}. If I browse to that link in my browser (and am, of course, logged into reddit) it returns a JSON string with all the user data.
Any idea how I can store the cookie so that it is usable in a UIWebView?
Any idea how I can store the cookie so that it is usable in a UIWebView?
It appears that iOS cookies have been problematic since phonegap 2.5; please see [Handling cookies in PhoneGap/Cordova
I can't see any more current (phonegap 3.3) info [http://docs.phonegap.com/en/3.3.0/_index.html]

Facebook FQL returns NaN value

In this script
function fql(){
FB.api('/me', function(response) {
var query = FB.Data.query('select uid, name, email from user where uid={0}', response.id);
query.wait(function(rows) {
document.getElementById('name').innerHTML =
'Your name: ' + rows[0].name + "<br />" +
+ rows[0].email + "<br />" + rows[0].uid;
});
});
}
Function returns name and pic, but email value is NaN. App requires basic data and email (it was set in app properties/Auth dialog) but i cant get user email.
When I tested this query in Facebook test console, I've got email field with correct value.
Check that in Test Console and on your site you use the same application id and ensure that user have granted the email permission to your application!
This can be done with simple Graph API call:
FB.api('/me/permissions', function(response){
if (response && response.data){
var userPermissions = response.data.shift();
if (userPermissions.email){
alert('User is granted email permission');
} else {
alert('User is NOT granted email permission');
}
} else {
// No response or results from the Graph API
}
});
Couple of things you should be aware of!
FB.Data.* is gone! It's deprecated, undocumented and should not be used anymore! It's just a matter of time for it to disappear from the JavaScript SDK code...
You just doesn't want extra call just to figure the id of current user (most chances that you already have it, just check FB.getUserID()), in FQL use me() to refer to current user.
Rewrite your usage of FB.Data.* and query.wait with:
FB.api('/fql', {q:'YOUR_FQL_QUERY_HERE'}, function(response){
var rows = response.data;
});
In your case same results may be received by Graph API call without usage of FQL:
FB.api('/me', {fields: 'id,name,email'}, function(response){
// response -> {id: UID, name: 'User Name', email: 'user#email.dz'}
console.log(response);
});

facebook javascript sdk - how to check if user likes page without fb.login pop up

I have a javascript function that uses fb.login to retrieve the users info and checks to see if the user likes my fb page. fb.login causes a pop up where the user must click a login button. If the user likes my page i need to first create a cookie then redirect them to the main app:
function checkUser() {
var page_id = "my id goes here"; //
FB.login(function (response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function (response) {
var fql_query = "SELECT uid FROM page_fan WHERE page_id = " + page_id + "and uid=" + response.id;
var the_query = FB.Data.query(fql_query);
the_query.wait(function (rows) {
if (rows.length == 1 && rows[0].uid == response.id) {
//$("#container_like").show();/
//set cookie
document.cookie = "fbId=" + response.id;
window.location = "/kisses.aspx";
} else {
$("#likepageholder").show();
//$("#container_notlike").show();
//window.location = "/kisses.aspx";
//and here you could get the content for a non liker in ajax...
}
});
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
}
If there a way around using fb.login to do this?
Nope. Unfortunately, you need the user to log in (and grant your app basic permissions [based on the appId you use in FB.init) in order to query the user's likes.
That being said, it looks like your code should work.

Categories

Resources