Ecco una semplice configurazione che utilizza il passaporto per accedere/disconnettere gli utenti:
Nel file app.js principale
/* Auth config --------------------------------*/
// @see http://frederiknakstad.com/authentication-in-single-page-applications-with-angular-js/
var passport = require('passport');
var User = require('./app/models/User'),
passport.use(User.localStrategy);
passport.serializeUser(User.serializeUser);
passport.deserializeUser(User.deserializeUser);
// Default session handling. Won't explain it as there are a lot of resources out there
app.use(express.session({
secret: "mylittlesecret",
cookie: {maxAge: new Date(Date.now() + 3600000)}, // 1 hour
maxAge: new Date(Date.now() + 3600000), // 1 hour
store: new RedisStore(config.database.redis), // You can not use Redis
}));
// The important part. Must go AFTER the express session is initialized
app.use(passport.initialize());
app.use(passport.session());
// Set up your express routes
var auth = require('./app/controllers/authController.js');
app.post('/auth/login', auth.login);
app.post('/auth/logout', auth.logout);
app.get('/auth/login/success', auth.loginSuccess);
app.get('/auth/login/failure', auth.loginFailure);
Sul tuo modello utente (es. app/models/User.js)
Sto usando il modulo passport-local, che semplifica ulteriormente la logica di accesso:https://github.com/jaredhanson/passport-local
/* Your normal user model ----------------------*/
var mongoose = require('mongoose'),
ObjectId = mongoose.Schema.Types.ObjectId,
PassportLocalStrategy = require('passport-local').Strategy;
var schema = new mongoose.Schema({
name: {type:String, required:true, trim:true},
email: {type:String, required: true, trim: true, lowercase:true, unique: true},
image: {type:String},
password: {type:String, required: true },
created: {type: Date, default: Date.now}
});
/* Auth properties ---------------------------*/
/* (passport) ---------------------------*/
// This is your main login logic
schema.statics.localStrategy = new PassportLocalStrategy({
usernameField: 'email',
passwordField: 'password',
},
// @see https://github.com/jaredhanson/passport-local
function (username, password, done){
var User = require('./User');
User.findOne({email: username}, function(err, user){
if (err) { return done(err); }
if (!user){
return done(null, false, { message: 'User not found.'} );
}
if (!user.validPassword(password)){
return done(null, false, { message: 'Incorrect password.'} );
}
// I'm specifying the fields that I want to save into the user's session
// *I don't want to save the password in the session
return done(null, {
id: user._id,
name: user.name,
image: user.image,
email: user.email,
});
});
}
);
schema.methods.validPassword = function(password){
if (this.password == password){
return true;
}
return false;
}
schema.statics.serializeUser = function(user, done){
done(null, user);
};
schema.statics.deserializeUser = function(obj, done){
done(null, obj);
};
var model = mongoose.model('User', schema);
exports = module.exports = model;
Su app/controllers/authController.js
Sto usando un'applicazione a pagina singola, quindi sto restituendo JSON al login/logout. Se desideri reindirizzare da qualche altra parte, dovrai modificare le funzioni "accesso riuscito" e "accesso fallito" (o chiamare res.render(...) o altro).
var passport = require('passport');
var AuthController = {
// Login a user
login: passport.authenticate('local', {
successRedirect: '/auth/login/success',
failureRedirect: '/auth/login/failure'
}),
// on Login Success callback
loginSuccess: function(req, res){
res.json({
success: true,
user: req.session.passport.user
});
},
// on Login Failure callback
loginFailure: function(req, res){
res.json({
success:false,
message: 'Invalid username or password.'
});
},
// Log out a user
logout: function(req, res){
req.logout();
res.end();
},
};
exports = module.exports = AuthController;
Infine, dovresti puntare il tuo modulo di accesso (che deve avere il method="post"
set di attributi) su /auth/login. In caso di accesso riuscito, verrà eseguito il callback "loginSuccess". In caso di errore di accesso, verrà eseguita la richiamata "loginFailure".
Modifica:
Puoi creare nuovi utenti nel tuo database mongo eseguendo qualcosa come:
// On your main app.js file
app.post('/auth/register', auth.register);
// On your authController.js file, as per the previous example
var User = require('./app/models/User'); // The model we defined in the previous example
...
register: function(req, res){
User.create({name: req.body.name, email: req.body.email, password: req.body.password}, function(err){
if (err) {
console.log(err);
... // Your register error logic here
res.redirect('/* Your error redirection path */');
return;
}
res.redirect('/* Your success redirection path */');
});
},
...
Quindi, punta un modulo di registrazione su /auth/register. Non ho convalidato i dati, ma dovresti convalidarli prima di provare a salvare l'utente.