2

私は最初のアプリに取り組んでおり、フロントエンドと angularjs から始めました。一般的には非常に直感的だと思いますが、バックエンドとフロントエンドの関係は、私にとって物事がぼやけ始めるところです。

ユーザーが認証されているかどうかに応じて、いくつかのページでわずかに異なる機能を提供したいところまで来ました (この場合、フォーム内のいくつかのフォーム フィールドを編集する機能)。

パブリック angularjs 側からは、基本的な if ステートメントを記述して認証済みユーザーにさまざまな機能を提供するのは簡単に思えます (以下の基本的な試みを参照)。 '(データベースに保存) したくありません。

angular.module('core').controller('myCtrl', ['$scope', 'Authentication', 'Menus',
    function($scope, Authentication, Menus) {
        $scope.authentication = Authentication;

        if(typeof $scope.authentication.user == "object"){
           // behaviour for authenticated
        }else{
          // for unauthenticated
        }
    }

私は、主に php の専門家であり、一般的には meanjs と node.js を初めて使用するので、私の質問が根拠のないものである場合は、優しくしてください。

4

1 に答える 1

1

ユーザー認証用に npm モジュールのパスポートを使用することをお勧めします。開始するためのコードを次に示します。このscotch.io チュートリアルもご覧ください。

// load all the things we need
var LocalStrategy   = require('passport-local').Strategy;

// load up the user model
var User            = require('../app/models/user');

// expose this function to our app using module.exports
module.exports = function(passport) {

passport.serializeUser(function(user, done) {
done(null, user.id);
});

// used to deserialize the user
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
  done(err, user);
  });
});


passport.use('local-signup', new LocalStrategy({
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request         to the callback
 },
   function(req, email, password, done) {

// asynchronous
// User.findOne wont fire unless data is sent back
process.nextTick(function() {

  // find a user whose email is the same as the forms email
  // we are checking to see if the user trying to login already exists
  User.findOne({ 'local.email' :  email }, function(err, user) {
    // if there are any errors, return the error
    if (err)
      return done(err);

    // check to see if theres already a user with that email
    if (user) {
      return done(null, false, req.flash('signupMessage', 'That email  is already taken.'));
    } else {

      // if there is no user with that email
      // create the user
      var newUser            = new User();

      // set the user's local credentials
      newUser.local.email    = email;
      newUser.local.password = newUser.generateHash(password);

      // save the user
      newUser.save(function(err) {
        if (err)
          throw err;
        return done(null, newUser);
      });
    }

  });

});

 }));

  passport.use('local-login', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
  },
   function(req, email, password, done) { // callback with email and password from our form

// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({ 'local.email' :  email }, function(err, user) {
  // if there are any errors, return the error before anything else
  if (err)
    return done(err);

  // if the user is found but the password is wrong
  if (!user || !user.validPassword(password))
    return done(null, false, req.flash('loginMessage', 'Oops! Wrong username or password.')); // create the loginMessage and save it to session as flashdata

  // all is well, return successful user
  return done(null, user);
});

 }));

};
于 2015-02-05T23:35:28.670 に答える