Authored by htoooth

refactor

@@ -16,7 +16,7 @@ module.exports = (req, res, next) => { @@ -16,7 +16,7 @@ module.exports = (req, res, next) => {
16 16
17 let loginPage = helpers.urlFormat('/passport/login', {refer: req.fullUrl()}); 17 let loginPage = helpers.urlFormat('/passport/login', {refer: req.fullUrl()});
18 18
19 - if (_.isEmpty(req.user) || !req.user.uid || !req.user.uid.session) { 19 + if (_.isEmpty(req.user) || !req.user.uid.isValid()) {
20 if (req.xhr) { 20 if (req.xhr) {
21 return res.json({ 21 return res.json({
22 code: 400, 22 code: 400,
@@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
4 */ 4 */
5 const headerModel = require('../models/header'); 5 const headerModel = require('../models/header');
6 const logger = global.yoho.logger; 6 const logger = global.yoho.logger;
  7 +const helpers = global.yoho.helpers;
7 8
8 const forceNoCache = (res) => { 9 const forceNoCache = (res) => {
9 if (res) { 10 if (res) {
@@ -48,6 +49,15 @@ exports.serverError = () => { @@ -48,6 +49,15 @@ exports.serverError = () => {
48 49
49 logger.error(`error at path: ${req.url}`); 50 logger.error(`error at path: ${req.url}`);
50 logger.error(err); 51 logger.error(err);
  52 +
  53 + if (err && err.code === 401) {
  54 + if (req.xhr) {
  55 + return res.json(err);
  56 + } else {
  57 + return res.redirect(helpers.urlFormat('/signin.html', {refer: req.fullUrl()}));
  58 + }
  59 + }
  60 +
51 if (!res.headersSent) { 61 if (!res.headersSent) {
52 if (req.xhr) { 62 if (req.xhr) {
53 return res.status(500).json({ 63 return res.status(500).json({
@@ -31,7 +31,10 @@ module.exports = () => { @@ -31,7 +31,10 @@ module.exports = () => {
31 return this.uid; 31 return this.uid;
32 }, 32 },
33 uid: cookie.getUid(req), 33 uid: cookie.getUid(req),
34 - session: req.cookies._SESSSION 34 + session: req.cookies._SESSION,
  35 + isValid() {
  36 + return this.uid && this.session;
  37 + }
35 }; 38 };
36 } 39 }
37 40