2014-11-17 2 views
2

첫 번째 앱을 작성 중이며 프런트 엔드 및 angularjs로 시작했습니다. 일반적으로 나는 그것을 매우 직관적이라고 생각했지만, 백엔드와 프론트 엔드 간의 관계는 상황이 나를 위해 흐려지기 시작하는 곳이다.anglejs를 사용할 때 Meanjs에서만 인증 된 사용자의 기능을 보호하는 방법

사용자 인증 여부에 따라 일부 페이지에서 약간 다른 기능을 제공하고자하는 시점에 이르렀습니다.이 경우 폼의 일부 양식 필드를 편집 할 수 있습니다.

공용 anglejs 측에서 인증 된 사용자에게 다른 기능을 제공하기 위해 기본 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를을 의미하는 새로운 오전, 그래서 부드러운 주시기 바랍니다.

+1

인증이 서버에 구현되어야합니다. 서버에서 모든 조치가 점검되고 권한이없는 상태 401이 리턴됩니다. 따라서 모든 사용자가 서버에서 확인되기 때문에 사용자가 클라이언트를 수정하는 것은 중요하지 않습니다. – dfsq

답변

1

사용자 인증을 위해 npm 모듈을 사용하는 것이 좋습니다. 시작하는 데 필요한 몇 가지 코드가 있습니다. 또한 이것 좀 봐 scotch.io tutorial

// 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); 
}); 

})); 

};