Skip to content

Commit

Permalink
fix: check token validate before request send
Browse files Browse the repository at this point in the history
  • Loading branch information
leoliu committed Jan 15, 2020
1 parent 51bfa60 commit f53eaf5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/controllers/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const handleLogin = async ctx => {

ctx.session = {}
ctx.cookies.set('token', user.token)
ctx.cookies.set('currentUser', user.username)
ctx.cookies.set('currentUser', user.username, { httpOnly: false })
ctx.cookies.set('referer', null)

if (lastUser && lastUser !== user.username) {
Expand Down
10 changes: 10 additions & 0 deletions src/utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const get = require('lodash/get')
const set = require('lodash/set')
const merge = require('lodash/merge')
const qs = require('qs')
const cookie = require('./cookie').default

const methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE']

Expand Down Expand Up @@ -103,6 +104,15 @@ function buildRequest({
responseHandler = handler
}

if (
cookie('currentUser') &&
globals.user &&
globals.user.username !== cookie('currentUser')
) {
location.href = '/'
return
}

return fetch(requestURL, request).then(resp => responseHandler(resp, reject))
}
/**
Expand Down

0 comments on commit f53eaf5

Please sign in to comment.