Verify user password in Meteor

后端 未结 2 805
耶瑟儿~
耶瑟儿~ 2020-12-01 13:48

There are some irreversible actions that user can do in my app. To add a level of security, I\'d like to verify that the person performing such an action is actually the log

2条回答
  •  半阙折子戏
    2020-12-01 14:06

    I haven't done this before, but I think you will need something like this on your server

    
        Accounts.registerLoginHandler(function(loginRequest) {
            console.log(loginRequest)
            var userId    = null;
            var username  = loginRequest.username;
            // I'M NOT SURE HOW METEOR PASSWORD IS HASHED...
            // SO YOU NEED TO DO A BIT MORE RESEARCH ON THAT SIDE
            // BUT LET'S SAY YOU HAVE IT NOW
            var password  = loginRequest.password;
    
            var user = Meteor.users.findOne({
                $and: [
                    {username: username},
                    {password: password} 
                ]
            });
            if(!user) {
                // ERROR
            } else {
                // VERIFIED
            }
        });
    
    

    then you can call this function from the client side like this:

    
        // FETCH THE USERNAME AND PASSWORD SOMEHOW
        var loginRequest = {username: username, password: password};
    
        Accounts.callLoginMethod({
            methodArguments: [loginRequest]
        });
    
    

    I have a project on github for different purpose, but you can get a sense of how it is structured: https://github.com/534N/apitest

    Hope this helps,

提交回复
热议问题