Sof*_*mur 6 authentication local-storage angular-local-storage passport.js
我想重现 plunker 如何管理匿名帐户。
Plunker 可以识别匿名用户。例如,我们可以将一个 plunker 保存为anonym然后freeze它。因此,
只有同一用户(在清除浏览器历史记录之前)才能完全访问此插件(例如,保存修改、解冻)。
如果同一用户在另一个浏览器中打开它或其他用户打开同一链接,他们不能进行save任何修改;他们必须fork这样做。
在我的网站中,我使用管理命名用户local的策略。passport.js例如,
router.post('/login', function (req, res, next) {
if (!req.body.username || !req.body.password)
return res.status(400).json({ message: 'Please fill out all fields' });
passport.authenticate('local', function (err, user, info) {
if (err) return next(err);
if (user) res.json({ token: user.generateJWT() });
else return res.status(401).json(info);
})(req, res, next);
});
Run Code Online (Sandbox Code Playgroud)
我使用 alocalStorage来存储令牌。例如,
auth.logIn = function (user) {
return $http.post('/login', user).success(function (token) {
$window.localStorage['account-token'] = token;
})
};
auth.logOut = function () {
$window.localStorage.removeItem('account-token');
};
Run Code Online (Sandbox Code Playgroud)
有谁知道是否passport.js有任何策略或现有工具可以像 plunker 那样管理匿名帐户?否则,是否有常规方法可以实现这一目标?
小智 4
Passport 允许匿名身份验证。有一个护照匿名策略:
app.get('/',
// Authenticate using HTTP Basic credentials, with session support disabled,
// and allow anonymous requests.
passport.authenticate(['basic', 'anonymous'], { session: false }),
function(req, res){
if (req.user) {
res.json({ username: req.user.username, email: req.user.email });
} else {
res.json({ anonymous: true });
}
});
Run Code Online (Sandbox Code Playgroud)
这会使用您的基本策略,如果您使用本地身份验证,则可以将其替换为本地策略。如果没有提供任何内容,它会退回到匿名策略,如下所示:
passport.use(new BasicStrategy({
},
function(username, password, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
// Find the user by username. If there is no user with the given
// username, or the password is not correct, set the user to `false` to
// indicate failure. Otherwise, return the authenticated `user`.
findByUsername(username, function(err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (user.password != password) { return done(null, false); }
return done(null, user);
})
});
}
));
// Use the BasicStrategy within Passport.
// This is used as a fallback in requests that prefer authentication, but
// support unauthenticated clients.
passport.use(new AnonymousStrategy());
Run Code Online (Sandbox Code Playgroud)
完整的示例可以在这里找到:- https://github.com/jaredhanson/passport-anonymous/blob/master/examples/basic/app.js
| 归档时间: |
|
| 查看次数: |
4579 次 |
| 最近记录: |