Browse Source

过期token处理

release_0.0.3
巴林闲侠 2 years ago
parent
commit
5f28d0facb
  1. 23
      code/api/app/lib/controllers/auth/index.js
  2. 4
      code/api/app/lib/routes/auth/index.js
  3. 4
      code/api/config.js
  4. 16
      code/web/client/src/sections/auth/actions/auth.js
  5. 25
      code/web/client/src/sections/auth/containers/cross.jsx
  6. 2
      code/web/client/src/utils/webapi.js

23
code/api/app/lib/controllers/auth/index.js

@ -105,8 +105,31 @@ async function loginAxy (ctx) {
}
}
async function checkCrossToken (ctx) {
try {
const { token } = ctx.request.body;
let cross = false
const expired = await ctx.redis.hget(token, 'expired');
// 也可以在这里做延时操作 需要同步数据库(也可能安心云)
if (expired && moment().valueOf() <= moment(expired).valueOf()) {
cross = true
}
ctx.status = 200;
ctx.body = {
cross,
}
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {}
}
}
module.exports = {
login,
logout,
loginAxy,
checkCrossToken,
};

4
code/api/app/lib/routes/auth/index.js

@ -21,4 +21,8 @@ module.exports = function (app, router, opts) {
app.fs.api.logAttr['PUT/logout/axy'] = { content: '安心云登出信息同步', visible: false };
router.put('/logout/axy', auth.logout);
//
app.fs.api.logAttr['POST/cross_token/check'] = { content: '登录信息鉴权信息检测', visible: true };
router.post('/cross_token/check', auth.checkCrossToken);
};

4
code/api/config.js

@ -47,7 +47,9 @@ const product = {
}, {
entry: require('./app').entry,
opts: {
exclude: [], // 不做认证的路由,也可以使用 exclude: ["*"] 跳过所有路由
exclude: [// 不做认证的路由,也可以使用 exclude: ["*"] 跳过所有路由
{ p: '/cross_token/check', o: 'POST' }
],
redis: {
host: IOTA_REDIS_SERVER_HOST,
port: IOTA_REDIS_SERVER_PORT,

16
code/web/client/src/sections/auth/actions/auth.js

@ -1,7 +1,7 @@
'use strict';
import { ApiTable } from '$utils'
import { Request } from '@peace/utils';
import { Request, basicAction } from '@peace/utils';
export const INIT_AUTH = 'INIT_AUTH';
export function initAuth () {
@ -72,8 +72,20 @@ export function logout () {
};
}
export function crossCheck (data) {
return (dispatch) =>
basicAction({
type: "post",
dispatch: dispatch,
actionType: "CROSS_CHECK",
url: `${ApiTable.crossCheck}`,
msg: {},
});
}
export default {
initAuth,
login,
logout
logout,
crossCheck,
}

25
code/web/client/src/sections/auth/containers/cross.jsx

@ -1,17 +1,22 @@
'use strict';
import React, { useEffect, useRef } from 'react';
import { connect } from "react-redux";
import authAction from '../actions'
const Cross = ({ dispatch, actions }) => {
useEffect(() => {
useEffect(async () => {
function preLogout () {
localStorage.removeItem('fs_iot_cross_user')
sessionStorage.removeItem('user')
}
function messageListen (e) {
//
const { data } = e
if (data && data.action) {
if (data.action == 'logout') {
localStorage.removeItem('fs_iot_cross_user')
sessionStorage.removeItem('user')
preLogout()
} else if (data.action = 'login') {
localStorage.setItem('fs_iot_cross_user', JSON.stringify(data.user))
}
@ -30,8 +35,20 @@ const Cross = ({ dispatch, actions }) => {
window.addEventListener('message', messageListen);
window.addEventListener("storage", storageListen);
const user = localStorage.getItem('fs_iot_cross_user')
if (user) {
window.parent.postMessage({ action: 'initUser', user: JSON.parse(user) }, '*');
const crossRslt = await dispatch(authAction.crossCheck({ token: user.token }))
if (crossRslt.success) {
if (crossRslt.payload.data.cross) {
window.parent.postMessage({ action: 'initUser', user: JSON.parse(user) }, '*');
} else {
window.parent.postMessage({ action: 'logout' }, '*');
preLogout()
}
} else {
window.parent.postMessage({ action: 'logout' }, '*');
preLogout()
}
} else {
window.parent.postMessage({ action: 'initNotice' }, '*');
}

2
code/web/client/src/utils/webapi.js

@ -3,6 +3,8 @@
export const ApiTable = {
login: 'login',
logout: 'logout',
crossCheck: 'cross_token/check',
};
export const RouteTable = {

Loading…
Cancel
Save