mirror of
https://github.com/vbenjs/vue-vben-admin.git
synced 2025-01-26 12:48:48 +08:00
01d60336a6
* feat: refreshToken * chore: store refreshToken * chore: generate token using jsonwebtoken * chore: set refreshToken in httpOnly cookie * perf: authHeader verify * chore: add add response interceptor * chore: test refresh * chore: handle logout * chore: type * chore: update pnpm-lock.yaml * chore: remove test code * chore: add todo comment * chore: update pnpm-lock.yaml * chore: remove default interceptors * chore: copy codes * chore: handle refreshToken invalid * chore: add refreshToken preference * chore: typo * chore: refresh token逻辑调整 * refactor: interceptor presets * chore: copy codes * fix: ci errors * chore: add missing await * feat: 完善refresh-token逻辑及文档 * fix: ci error * chore: filename --------- Co-authored-by: vince <vince292007@gmail.com>
34 lines
835 B
TypeScript
34 lines
835 B
TypeScript
import {
|
|
clearRefreshTokenCookie,
|
|
getRefreshTokenFromCookie,
|
|
setRefreshTokenCookie,
|
|
} from '~/utils/cookie-utils';
|
|
import { verifyRefreshToken } from '~/utils/jwt-utils';
|
|
import { forbiddenResponse } from '~/utils/response';
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const refreshToken = getRefreshTokenFromCookie(event);
|
|
if (!refreshToken) {
|
|
return forbiddenResponse(event);
|
|
}
|
|
|
|
clearRefreshTokenCookie(event);
|
|
|
|
const userinfo = verifyRefreshToken(refreshToken);
|
|
if (!userinfo) {
|
|
return forbiddenResponse(event);
|
|
}
|
|
|
|
const findUser = MOCK_USERS.find(
|
|
(item) => item.username === userinfo.username,
|
|
);
|
|
if (!findUser) {
|
|
return forbiddenResponse(event);
|
|
}
|
|
const accessToken = generateAccessToken(findUser);
|
|
|
|
setRefreshTokenCookie(event, refreshToken);
|
|
|
|
return accessToken;
|
|
});
|