Parse Facebook login - Save input username and email - javascript

I'm working on a Facebook login for a Parse App, and have added the login element, but would now like to add the FB profile email and name into the database. However, I am unable to do so. How would I be able to insert the username and email of a new user who is using Facebook to register on the app?
$scope.loginFacebook = function() {
Parse.FacebookUtils.logIn("email", {
success: function(user) {
if (!user.existed()) {
FB.api('/me?fields=name,email', function (response) {
var user = new Parse.User();
user.set("username", response.username);
user.set("email", response.email);
});
} else {
alert("You're logged in with Facebook!")
//redirect to dashboard page
}
},
error: function(user, error) {
alert("User cancelled the Facebook login or did not fully authorize.");
}
});
};

Just found out the solution to this. I needed to make a request from FB graphs on the url:
$scope.loginFacebook = function() {
Parse.FacebookUtils.logIn(null, {
success: function(user) {
if (!user.existed()) {
FB.api('/me?fields=id,name,email,permissions', function (response) {
user.set('username', response.name);
user.set('email', response.email);
user.save();
alert("You're registered and logged with via Facebook!");
//redirect to dashboard
});
} else {
alert("You're logged in with Facebook!");
//redirect to dashboard page
}
},
error: function(user, error) {
alert("User cancelled the Facebook login or did not fully authorize.");
}
});
};

This should work for you...
FB.api('/me', function (me) {
user.set("facebook_id", me.id);
user.set("facebook_link", me.link);
user.set("firstName", me.first_name);
user.set("lastName", me.last_name);
user.setEmail(me.email);
user.save().then(function () {
//go to new page
});
});

Related

To post on facebook page using javascript SDK with page access token

I am trying to post to my facebook's wall using javascript SDK. I am first getting user access token. Then using user access token i am getting page access token and including it while posting to a wall. I'm getting following error
here's my code : First am trying to login using FB.login
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.'+JSON.stringify(response));
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
},{scope: 'manage_pages,publish_pages'});
Then trying to post to a page.
var body = 'Reading JS SDK documentation';
FB.getLoginStatus(function(response) {
console.log('login status',response);
if(!(response.status === 'connected')){
location.href = './fb-login.html';
} else {
uID = response.authResponse.userID;
accessToken = response.authResponse.accessToken;
console.log('accesstoken::',response.authResponse.accessToken);
FB.api('/me', {fields: 'last_name'}, { access_token : accessToken } ,function(response) {
console.log(response);
});
//get list of pages
if(accessToken!=null){
FB.api('/me/accounts','get',{ access_token : accessToken },function(response){
console.log('resp of pages',response);
if(response!=null){
var data = response.data;
pageAccessToken= data[0].access_token;
console.log('pageAccessToken::',pageAccessToken);
FB.api('/6599048*******/feed', 'post', {message :body, access_token : pageAccessToken }, function(response) {
console.log('response',response)
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
}
});
}

Firebase auth automatically signs me back in after logout

My login function works fine but after logging out it redirects me back to my login landing page. The AuthState in the login js doesnt change is it a bug or do i have an error in my code.
Login.js
function loginBtn(){
var userEmail = document.getElementById("email_field").value;
var userPass = document.getElementById("password_field").value;
firebase.auth().signInWithEmailAndPassword(userEmail,userPass).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
console.log(error.Message);
window.alert("Error : " + errorMessage);
});
}
firebase.auth().onAuthStateChanged(user => {
if(user) {
window.location = '/home';
}
});
function recaptchaCallback() {
$('#submitBtn').removeAttr('disabled');
};
Auth.js
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
} else {
// No user is signed in.
firebase.auth().signOut();
alert('NOT LOGGED IN');
window.location='/';
}
});
function logout(){
firebase.auth().signOut();
alert('SIGNED OUT');
window.location='/';
}
I solved it by placing the OnAuthStateChanged to document ready since the listener is too slow to load.
$(document).ready(function(){
firebase.auth().onAuthStateChanged(user => {
if(user) {
window.location = '/home';
}
});
});

Meteor : Stop login if email is unverified

In my meteor.js app I only want verified users signing in. They must click the link in their email first.
I don't see any official mention of this in the documentation. Any ideas?
client javascript
Template.login.events({
'submit #login-form' : function(e,t){
e.preventDefault();
var email = t.find('#email').value;
var password = t.find('#password').value;
Meteor.loginWithPassword(email, password, function(error){
if (error){
console.log(error);
}
else{
console.log("success");
}
});
return false;
}
});
There are some stackoverflow posts but these only cover blocking unverified users from viewing certain pages:
Meteor: Block access to application if user's email is not verified
What you could do is check for the verified token inside the Accounts db.
Something of this sort:
if (Meteor.isServer) {
Meteor.methods({
'get_users_by_email': function(email) {
return Users.findOne({ emails.address: email }).fetch()[0].verified;
}
});
}
if (Meteor.isClient) {
a = Meteor.call('get_users_by_email', 'email-entered-by-user');
}
Then, you can check whether a is true or false.
When a is false, you could login the user, and when it is true, you can prevent the login by showing an error or whateever you want to tell people who have verified email adresses.
/lib/methods.js
Meteor.startup(function() {
checkEmailVerification: function(email) {
found_user = Meteor.users.findOne({ 'emails.address' : email })
if(found_user){
if(found_user.emails[0].verified == true){
return "verified";
}else{
return "unverified";
}
}else{
return "notfound";
}
}
});
});
/client/login.js
Template.login.events({
'submit #login' : function(event, t){
event.preventDefault();
var email = t.find('#email').value;
var password = t.find('#password').value;
Meteor.call('checkEmailVerification', email, function(error,data){
if (data == "verified"){
Meteor.loginWithPassword(email, password, function(err){
if (err){
FlashMessages.sendError("Either email or password is incorrect");
}
else{
FlashMessages.sendSuccess("Logged in");
}
});
}else if(data == "unverified"){
FlashMessages.sendError("Check your email for a verification link");
}else{
FlashMessages.sendError("Either email or password is incorrect");
}
});
}
});

ParseJS, AngularJS user registration error

I am using AngularJS to create a user profile registration with a Parse Backend. However, on my console I am getting an error saying Parse is not defined. How would I be able to make it work and insert a new user into the database?
Below is my code:
.controller('reg',['$scope',function($scope, $state, $rootScope){
$scope.signUp = function() {
var user = new Parse.User();
user.set("email", $scope.user.email);
user.set("password", $scope.user.password);
user.set("firstname", $scope.user.firstname);
user.set("lastname", $scope.user.lastname);
user.signUp(null, {
success: function(user) {
$rootScope.user = user;
$state.go('home');
},
error: function(user, error) {
alert("error here");
}
});
};
}])

Facebook JS API - How to redirect user after login

I have the following code:
FB.getLoginStatus(function (response) {
if (response.session) {
} else {
FB.ui({
method: 'oauth',
perms: 'email,publish_stream',
},
function (response) {
if (response && response.installed) {
window.location.reload();
}
});
}
});
What I want is to basically redirect the user to the same page so that the serverside can use the REST API. Is there anyway to make this work?
Subscribe to the login event:
FB.Event.subscribe('auth.login', function (response) {
window.location.reload();
});
Use this.
FB.Event.subscribe('auth.sessionChange', function (response) {
if (response.session) {
//user is logged in!
window.location = document.URL;
}
});

Categories

Resources