How to login in Meteor.loginWithPassword with username - javascript

I tried to login in Meteor with username instead of email but it is not working.
Meteor.loginWithPassword(username, password, error => {
// error ==> user not found
});
According to docs it says user can be either username, email or id
I have saved username in profile of User

Fixed, I saved my username in profile sections. Username should be saved as separate key just like email, then it will work

Related

Get value from push key generated

I want to get points of respectived logged in user to be displayed from database corresponding to its push key i tried many ways but i just couldn't make it possible here is my code
const autoid=firebase.database().ref("user").push().key;
firebase.database().ref("/").child(autoid).set({
email :email,
password : password,
points :"500",
Id:autoid
})
And below is my firebase realtime database picture:
The following code inserts the email, password, points and ID on realtime database with different key for every user that registers and i want to show the user when he is logged in to see his email and points? I tried trying different methods but I was unsuccessful anyway to do that?
firebase.auth().createUserWithEmailAndPassword(email, password)
.then((credential) => {
firebase.database().ref("/users/" + credential.user.uid).set({
email: credential.user.email,
uid: credential.user.uid
});
alert("signed in");
})
I also tried this above code to get uid as a parent file rather then getting a random push id, but I have failed to do so and I don't know where the error is the rules used for my database is:-
{
"rules":{
".read": true,
".write":true
}
}
Do I need to change the rule?
To write the data in a node for the currently signed in user, you'd do:
const uid = firebase.auth().currentUser.uid;
firebase.database().ref("users").child(uid).set({
email: email,
password : password,
points: "500"
});
To subsequently read the data for the currently signed in user, you'd do:
const uid = firebase.auth().currentUser.uid;
firebase.database().ref("users").child(uid).once("value").then((snapshot) => {
console.log(snapshot.val());
});

How can I verify if the email belongs to an account that has a password?

I have a lot of kind of users, some users is log up with google account, others with google account and password, i only want to send the reset mail sendPasswordResetEmail if the email belong to user have a password, not to every kind of account, this each user have to do when the user is not login.
i used this function sendPasswordResetEmail but that function sent mail to everybody, i had a google account without password, that is wrong to me
You can check user's auth providers and check if that includes Email-Password:
const user = firebase.auth().currentUser;
if (user !== null) {
if (user.providerData.find(p => p.providerId === "password")) {
// user had email-password account
// send email
}
}

Difference between firebase auth verifyBeforeUpdateEmail and updateEmail

You can use updateEmail with the following workflow:
await user.updateEmail(newEmail)
await user.getIdToken()
await user.sendEmailVerification()
and there's also a function verifyBeforeUpdateEmail.
These two workflows seems identical to me, but is there any difference?
The documentation lacks examples and explanations of the difference.
Reference: https://firebase.google.com/docs/reference/js/firebase.User#verifybeforeupdateemail
The User.verifyBeforeUpdateEmail method is documented as:
Sends a verification email to a new email address. The user's email will be updated to the new one after being verified.
So the process here sends a verification email to the new email address. Only once the user clicks the link in that email will their email address be updated and the emailVerified property of their account set to true.
The user.updateEmail method is documented as:
Updates the user's email address.
So when you use updateEmail, the user's email address ends up being unverified. If you care about email verification, you'll need to call sendEmailVerification again to verify the updates email address. But even if you call sendEmailVerification right after updating the email address, the user account will have its emailVerified property set fo false for a while.

i am creating login api and hashed password using bcrypt

I want to create a register login logout system. at time of registration i am taking email and password
and storing hashed password. now when logging in i want to compare password entered to hashed password. so basically I want to know how can I compare and check plain password with hashed passwords
I think this is a duplicate question but am not able to mark it as such.
This should work, taken from the bcryptJS docs.
Sync
// Load hash from your password DB.
bcrypt.compare(myPlaintextPassword, hash, function(err, result) {
// result == true
});
Promise
// Load hash from your password DB.
bcrypt.compare(myPlaintextPassword, hash).then(function(result) {
// result == true
});

how to store signup data into redis with node.js

i want to store signup data it contains name and email and password.
i will store this data in mongodb like this
db.save({"name":"xxxx","email":"xxxxx","password":'xxxxxxxx'},function(err,result){});
when user login ,they surely give their email id or username with password so i will find this user exist in db or not by using like this
db.find({'email:'xxxxxxx','password':'xxxxxxxxx'},function(err,result){});
i have tried to do same in redis,by like this
db.hmset('key' name xxxxx email xxxx pass xxxxxx,function(){});
it is stored but how can i check email id usename already exist becz user will give email and password only.if i know key then only i can find that data.even if i know key i can get only data i could not be found data already exist ot not like mongodb
how can i solve this?
You could store your users both in a Set and a Hash for details.
You can then check in the Set if a user exists with: http://redis.io/commands/sismember
I think you should break things down into chunks instead of trying to do everything with one query. So, for example, to add a new user, first check if the user exists:
(My example assumes a Mongoose User Model has been defined)
User.findOne({$or : [{'email': req.body.email}, {'username': req.body.username}],
function(err, result) {
if (err) {next(err);}
if (result) {
// you found an existing user
res.send(309, {'message':'Error: User exists'});
} else {
// no user found
var user = new User({
'email': req.body.email,
'username': req.body.username,
'password': req.body.password,
'and-so-on': req.body.moredata
});
user.save(function(err){
if (err) {next(err);}
res.send(200, {'message':'User Registered Successfully'});
});
}
Honestly though, I wouldn't recommend writing a user authentication system from scratch, because it is pretty limiting in todays world of auth methods. I personally use Passport, because it gives you the ability to use multiple auth systems with your app, including Facebook, Twitter, and so on and so forth.

Categories

Resources