使用express+mongoDB搭建多人博客 学习(4)登录与登出

登录

修改index.ejs

<%- include header %>
    <% if(locals.error){ %>
        <span><%= locals.error %></span>
    <% } %>
    <form method="post">
        用户名:<input type="text" name="name"/><br/>
        密码:<input type="password" name="password"/><br/>
        <input type="submit" value="登录"/>
    </form>
<%- include footer %>

修改index.js

router.post('/login',function(req,res,next){
    var md5=crypto.createHash("md5");
    password=md5.update(req.body.password).digest('hex');
    console.log(req.body.name);
    User.get(req.body.name,function(err,user){
        if(!user){
            req.flash('error',"用户不存在");
            return res.redirect("/login");
        }
        console.log(user.password,password);
        if(user.password!==password){
            req.flash("error","密码错误");
            return res.redirect("/login");
        }
        req.session.user=user;
        req.flash('success',"登录成功");
        res.redirect("/");
    })
});

登出

index.js中修改:

router.get("/logout",function(req,res){
    req.session.user=null;
    req.flash("success","登出成功");
    res.redirect("/");
});

现在登录后浏览器中输入:localhost:3000/logout

修改模板

login.ejs index.ejs 和req.ejs中都有

<% if(locals.xxx){ %>
<span><%= locals.xxx %></span>
<% } %>

这类的代码,删掉它们。

修改header.ejs如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Blog</title>
    <link rel="stylesheet" href="/stylesheets/style.css">
</head>
<body>
    <header>
        <h1><%= title %></h1>
    </header>
    <nav>
        <span><a title="主页" href="/">home</a></span>
        <% if(locals.user){ %>
        <span><a title="发表" href="/post">post</a></span>
        <span><a title="登出" href="/logout">logout</a></span>
        <% }else{ %>
        <span><a title="登录" href="/login">login</a></span>
        <span><a title="注册" href="/reg">register</a></span>
        <% } %>
    </nav>
    <article>
        <% if(locals.error){ %>
            <span><%= locals.error %></span>
        <% } %>    
        <% if(locals.success){ %>
            <span><%= locals.success %></span>
        <% } %>

修改index.js

router.post('/login',function(req,res,next){
    var md5=crypto.createHash("md5");
    password=md5.update(req.body.password).digest('hex');
    console.log(req.body.name);
    User.get(req.body.name,function(err,user){
        if(!user){
            req.flash('error',"用户不存在");
            return res.redirect("/login");
        }
        console.log(user.password,password);
        if(user.password!==password){
            req.flash("error","密码错误");
            return res.redirect("/login");
        }
        req.flash("user",req.session.user=user);
        req.flash('success',"登录成功");
        res.redirect("/");
    })
});

router.get("/logout",function(req,res){
    req.flash("user",req.session.user=null);
    req.flash("success","登出成功");
    res.redirect("/");
});

修改app.js

app.use(function(req, res, next) {
    res.locals.error = req.flash('error').toString();
    res.locals.success = req.flash('success').toString();
    res.locals.user=req.flash("user")[0];
    next();
});

posted on 2015-01-27 10:40  叫我钱了个浅  阅读(289)  评论(0编辑  收藏  举报

导航