Search code examples
javascriptnode.jsexpressmean-stackmean.io

How to do a login with Node using Mean.io stack


I'm developing a webapp using stack MEAN.io and for now I have got the frontend part with HTML, CSS and AngularJS with some logic in it. But now I want to do login on server side and I don't know where to start because for example AngularJS it has the file where it defines the routes, what templete it will be used and which controller as well but what about the Express/Node part?

How can I implement this new login? I'm kinda lost.

Server folders

I want to do some "administration" with registered users that they can add favourite profiles. Like add one bookmark to right side of screen. But I want to do this in server side.

The problem is I do not find where to write the server side code and this relationate with the same file to the frontend.

For example when I am at Index page so I want to show the favourites profile i added before. And stored to MongoDB of course.


Solution

  • You have two options use regular forms to post data or use $http angular ajax post.

    regular form kind of posting data to server

    <form action="/" method="post">
      <input type"email" name="email" />
      <input type"password" name="password" />
      <input type="submit" value="login" />
    </form>
    

    posting data using ajax angular $http method

    <form >
      <input type"email" ng-model="user.email" />
      <input type"password" ng-model="user.password" />
      <button ng-click="login">login</button>
    </form>
    
    $scope.user = {};
    $scope.login= function () {
       $http({
          url: 'http://localhost:3000/',
          method: 'POST',
          data: {
            email: user.email,
            password:user.password
          }
       });
    });
    

    server side

    router.post('/', function (req, res, next) {
      console.log(req.body);
      //custom authentication or use passport.js
    });