핵심 :
함수 + GET + 미들웨어 사용하여서
라우터에서 유저정보를 빼서 ejs로 보내준다.
getSelf 함수를 이용하는데
유저라우터와 유저스키마와 연결되어있고,
getself 함수이용해서 method get 과 미들웨어를 거쳐오면서 유저정보를 ejs로 가져옴
function getSelf(callback) {
$.ajax({
type: "GET",
url: "/api/users/me",
headers: {
authorization: `Bearer ${localStorage.getItem("token")}`,
},
success: function (response) {
callback(response.user);
},
error: function (xhr, status, error) {
if (status == 401) {
alert("로그인이 필요합니다.");
} else {
localStorage.clear();
alert("알 수 없는 문제가 발생했습니다. 관리자에게 문의하세요.");
}
window.location.href = "/login";
},
});
}
let userid; 같이 빈값을 선언해놓고 겟셀프 돌려서 유저정보 ejs로 빼온 담에 userid에 유저정보 담기
(비밀번호, 주민등록번호 같이 중요한건xxx)
let userIdzz;
$(document).ready(function () {
// 비회원, 회원 전부 볼 수 있어야하기 때문에 에러가 발생하지 않아야한다.
if (localStorage.getItem("token")) {
getSelf(function (user) {
userIdzz = user.nickname; // userId = gudwls5925
$(`#btnModify`).show();
});
}
get_detail();
getComments();
});
'nodejs' 카테고리의 다른 글
For TDD , simple start with node.js (0) | 2022.05.05 |
---|---|
OOP (0) | 2022.01.31 |
router에서 유저정보 사용하기 (0) | 2021.11.26 |
location 객체 사용법만 ! (0) | 2021.11.26 |
JWT 이해 (0) | 2021.11.23 |