2016-11-10

Sails.js Tutorial : 관리자 비밀번호 변경하기


  • 뷰 디렉토리에 xyz/views/mgmt/admin/chg_passwd_form.ejs 파일을 생성한다.
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <h4 class="modal-title">관리자 비밀번호 변경</h4>
    </div>
    <div class="modal-body">
      <form name="chg_passwd_form" action="/mgmt/admin/chg_passwd" method="post">
        <input type='hidden' name='_csrf' value='<%= _csrf %>'>
        <div class="form-group">
          <label>아이디</label>
          <input type="text" name="userid" class="form-control" readonly required value="<%= admin.userid %>"/>
          <input type="hidden" name="id" value="<%= admin.id %>" />
        </div>
        <div class="form-group">
          <label>비밀번호 <small>(필수)</small></label>
          <input type="password" name="passwd1" class="form-control" required />
        </div>
        <div class="form-group">
          <label>비밀번호 확인 <small>(필수)</small></label>
          <input type="password" name="passwd2" class="form-control" required />
        </div>
        <div class="form-group" style="text-align: right">
          <input class="btn btn-primary" type="submit" value="관리자 비밀번호 변경" />
        </div>
      </form>
    </div>
  • 컨트롤러(xyz/api/controllers/mgmt/AdminController.js)에 다음을 추가한다.
    ...
     
      // 관리자 비밀번호변경 폼
      // /mgmt/admin/chg_passwd_form/:id
      chg_passwd_form: function (req, res) {
        let id = req.params.id;
     
        Admin.findOne({id: id}).exec(function (err, admin) {
          res.locals.layout = null;
          res.view('mgmt/admin/chg_passwd_form', { admin: admin });
        });
     
      },
     
    ...
  • 라우터(xyz/config/routes.js)에 다음을 추가한다.
      'get  /mgmt/admin/chg_passwd_form/:id' : 'mgmt/AdminController.chg_passwd_form',
  • 이제, 비밀번호변경 버튼을 클릭하면 비밀번호변경을 위한 모달 다이얼로그박스가 나타날 것이다.
  • 실제로 비밀번호를 변경하는 작업을 해보자. 컨트롤러(xyz/api/controllers/mgmt/AdminController.js)에 다음을 추가한다.
      // 관리자 비밀번호변경
      // /mgmt/admin/chg_passwd
      chg_passwd: function (req, res) {
        let id      = req.body.id;
        let passwd1 = req.body.passwd1;
        let passwd2 = req.body.passwd2;
     
        if (passwd1 != passwd2) {
          res.redirect('/mgmt/admin');
          return;
        }
     
        Admin.update({id: id}, {password: passwd1}).exec(function (err, updated) {
          console.log(updated); // 비밀번호 변경 확인용 출력
          res.redirect('/mgmt/admin');
        });
      },
  • 라우터(xyz/config/routes.js)에는 다음을 추가한다.
      'post /mgmt/admin/chg_passwd' : 'mgmt/AdminController.chg_passwd',
  • 비밀번호를 변경해보고, DB의 내용이 잘 반영되었는지 확인해보자.
    ...
     
    [ { userid: 'userid1',
        password: '12',
        nick: '태연',
        createdAt: '2016-11-09T06:09:11.596Z',
        updatedAt: '2016-11-09T06:09:18.356Z',
        id: 162 } ]
     
    ...


댓글 없음:

댓글 쓰기