feat: cookie expired time
This commit is contained in:
parent
de6ac0f589
commit
b200731d17
@ -3,7 +3,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';
|
|||||||
import { jsonRes } from '@/service/response';
|
import { jsonRes } from '@/service/response';
|
||||||
import { connectToDatabase } from '@/service/mongo';
|
import { connectToDatabase } from '@/service/mongo';
|
||||||
import { User } from '@/service/models/user';
|
import { User } from '@/service/models/user';
|
||||||
import { generateToken } from '@/service/utils/tools';
|
import { setCookie } from '@/service/utils/tools';
|
||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
try {
|
try {
|
||||||
@ -32,7 +32,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
throw new Error('密码错误');
|
throw new Error('密码错误');
|
||||||
}
|
}
|
||||||
|
|
||||||
res.setHeader('Set-Cookie', `token=${generateToken(user._id)}; Path=/; HttpOnly`);
|
setCookie(res, user._id);
|
||||||
|
|
||||||
jsonRes(res, {
|
jsonRes(res, {
|
||||||
data: {
|
data: {
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import { jsonRes } from '@/service/response';
|
|||||||
import { User } from '@/service/models/user';
|
import { User } from '@/service/models/user';
|
||||||
import { AuthCode } from '@/service/models/authCode';
|
import { AuthCode } from '@/service/models/authCode';
|
||||||
import { connectToDatabase } from '@/service/mongo';
|
import { connectToDatabase } from '@/service/mongo';
|
||||||
import { generateToken } from '@/service/utils/tools';
|
import { setCookie } from '@/service/utils/tools';
|
||||||
import { UserAuthTypeEnum } from '@/constants/common';
|
import { UserAuthTypeEnum } from '@/constants/common';
|
||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||||
@ -56,7 +56,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
|||||||
username
|
username
|
||||||
});
|
});
|
||||||
|
|
||||||
res.setHeader('Set-Cookie', `token=${generateToken(user._id)}; Path=/; HttpOnly`);
|
setCookie(res, user._id);
|
||||||
|
|
||||||
jsonRes(res, {
|
jsonRes(res, {
|
||||||
data: {
|
data: {
|
||||||
|
|||||||
@ -4,8 +4,8 @@ import { jsonRes } from '@/service/response';
|
|||||||
import { User } from '@/service/models/user';
|
import { User } from '@/service/models/user';
|
||||||
import { AuthCode } from '@/service/models/authCode';
|
import { AuthCode } from '@/service/models/authCode';
|
||||||
import { connectToDatabase } from '@/service/mongo';
|
import { connectToDatabase } from '@/service/mongo';
|
||||||
import { generateToken } from '@/service/utils/tools';
|
|
||||||
import { UserAuthTypeEnum } from '@/constants/common';
|
import { UserAuthTypeEnum } from '@/constants/common';
|
||||||
|
import { setCookie } from '@/service/utils/tools';
|
||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||||
try {
|
try {
|
||||||
@ -48,7 +48,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
|||||||
throw new Error('获取用户信息异常');
|
throw new Error('获取用户信息异常');
|
||||||
}
|
}
|
||||||
|
|
||||||
res.setHeader('Set-Cookie', `token=${generateToken(user._id)}; Path=/; HttpOnly`);
|
setCookie(res, user._id);
|
||||||
|
|
||||||
jsonRes(res, {
|
jsonRes(res, {
|
||||||
data: {
|
data: {
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import { NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
import { openaiError, openaiError2, proxyError, ERROR_RESPONSE, ERROR_ENUM } from './errorCode';
|
import { openaiError, openaiError2, proxyError, ERROR_RESPONSE, ERROR_ENUM } from './errorCode';
|
||||||
|
import { clearCookie } from './utils/tools';
|
||||||
|
|
||||||
export interface ResponseType<T = any> {
|
export interface ResponseType<T = any> {
|
||||||
code: number;
|
code: number;
|
||||||
@ -23,7 +24,7 @@ export const jsonRes = <T = any>(
|
|||||||
if (ERROR_RESPONSE[errResponseKey]) {
|
if (ERROR_RESPONSE[errResponseKey]) {
|
||||||
// login is expired
|
// login is expired
|
||||||
if (errResponseKey === ERROR_ENUM.unAuthorization) {
|
if (errResponseKey === ERROR_ENUM.unAuthorization) {
|
||||||
res.setHeader('Set-Cookie', 'token=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT');
|
clearCookie(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.json(ERROR_RESPONSE[errResponseKey]);
|
return res.json(ERROR_RESPONSE[errResponseKey]);
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import type { NextApiResponse } from 'next';
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
import jwt from 'jsonwebtoken';
|
import jwt from 'jsonwebtoken';
|
||||||
|
|
||||||
@ -19,6 +20,15 @@ export const generateToken = (userId: string) => {
|
|||||||
return token;
|
return token;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* set cookie */
|
||||||
|
export const setCookie = (res: NextApiResponse, userId: string) => {
|
||||||
|
res.setHeader('Set-Cookie', `token=${generateToken(userId)}; Path=/; HttpOnly; Max-Age=604800`);
|
||||||
|
};
|
||||||
|
/* clear cookie */
|
||||||
|
export const clearCookie = (res: NextApiResponse) => {
|
||||||
|
res.setHeader('Set-Cookie', 'token=; Path=/; Max-Age=0');
|
||||||
|
};
|
||||||
|
|
||||||
/* openai axios config */
|
/* openai axios config */
|
||||||
export const axiosConfig = () => ({
|
export const axiosConfig = () => ({
|
||||||
httpsAgent: global.httpsAgent,
|
httpsAgent: global.httpsAgent,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user