Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Coffee
- app.get '/test', authenticate, (req, res) ->
- res.render 'index', { locals: { title: 'Express' } }
- authenticate = (req, res, next) ->
- DEBUG "headers[authorization]: #{req.headers['authorization']}"
- creds = base64_decode(req.headers['authorization'].split(' ')[1])
- DEBUG "creds: #{creds}"
- [username,password] = creds.split(":")
- User.load username, (err,user) ->
- if err
- TODO "User.load(#{username}) err: #{err}"
- next(new Error("User.load(#{username}) err: #{err}"))
- else if user and user.password == md5(password)
- req.authenticated = user
- next()
- else
- DEBUG "user:password #{username}:#{password} not found"
- next(new Error("Authentication Error: #{username}"))
- ##js
- app.get('/test', authenticate, function(req, res) {
- return res.render('index', {
- locals: {
- title: 'Express'
- }
- });
- });
- authenticate = function(req, res, next) {
- var creds, password, username, _ref;
- DEBUG("headers[authorization]: " + req.headers['authorization']);
- creds = base64_decode(req.headers['authorization'].split(' ')[1]);
- DEBUG("creds: " + creds);
- _ref = creds.split(":"), username = _ref[0], password = _ref[1];
- return User.load(username, function(err, user) {
- if (err) {
- TODO("User.load(" + username + ") err: " + err);
- return next(new Error("User.load(" + username + ") err: " + err));
- } else if (user && user.password === md5(password)) {
- req.authenticated = user;
- return next();
- } else {
- DEBUG("user:password " + username + ":" + password + " not found");
- return next(new Error("Authentication Error: " + username));
- }
- });
- };
Add Comment
Please, Sign In to add comment