|
|
@@ -1,11 +1,13 @@
|
|
|
import jwt from "jsonwebtoken";
|
|
|
import redis from "../../../config/redis";
|
|
|
import {
|
|
|
- User
|
|
|
-} from "../../../models/User";
|
|
|
+ Subscription,
|
|
|
+ User,
|
|
|
+ Plan
|
|
|
+} from "../../../models/index";
|
|
|
import {
|
|
|
- RefreshTokenInput,
|
|
|
- RefreshTokenResult
|
|
|
+ RefreshTokenResult,
|
|
|
+ RefreshTokenInput
|
|
|
} from "./types";
|
|
|
|
|
|
const refreshToken = async (input: RefreshTokenInput): Promise<RefreshTokenResult> => {
|
|
|
@@ -23,18 +25,11 @@ const refreshToken = async (input: RefreshTokenInput): Promise<RefreshTokenResul
|
|
|
|
|
|
const userID = user._id.toString();
|
|
|
|
|
|
- let decoded: {
|
|
|
- companyName: string;
|
|
|
- fullName: string;
|
|
|
- userID: string;
|
|
|
- token: string;
|
|
|
- };
|
|
|
-
|
|
|
try {
|
|
|
- decoded = jwt.verify(
|
|
|
+ jwt.verify(
|
|
|
token,
|
|
|
process.env.JWT_SECRET as string
|
|
|
- ) as typeof decoded;
|
|
|
+ );
|
|
|
} catch {
|
|
|
return {
|
|
|
message: "invalid-refresh-token",
|
|
|
@@ -42,13 +37,37 @@ const refreshToken = async (input: RefreshTokenInput): Promise<RefreshTokenResul
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+ const activeSubscription = await Subscription.findOne({
|
|
|
+ userID: userID,
|
|
|
+ status: "active",
|
|
|
+ isActive: true
|
|
|
+ });
|
|
|
+
|
|
|
+ let planLimits = null;
|
|
|
+
|
|
|
+ if (activeSubscription) {
|
|
|
+ const plan = await Plan.findById(activeSubscription.planID);
|
|
|
+ if (plan) {
|
|
|
+ planLimits = {
|
|
|
+ recommendedProductLimit: plan.recommendedProductLimit,
|
|
|
+ categoryLimit: plan.categoryLimit,
|
|
|
+ productLimit: plan.productLimit,
|
|
|
+ planID: plan._id.toString(),
|
|
|
+ menuLimit: plan.menuLimit
|
|
|
+ };
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const freshTokenPayload = {
|
|
|
+ companyName: user.companyName,
|
|
|
+ fullName: user.fullName,
|
|
|
+ planDetails: planLimits,
|
|
|
+ userID: user._id,
|
|
|
+ mail: user.mail
|
|
|
+ };
|
|
|
+
|
|
|
const newAccessToken = jwt.sign(
|
|
|
- {
|
|
|
- companyName: decoded.companyName,
|
|
|
- fullName: decoded.fullName,
|
|
|
- userID: decoded.userID,
|
|
|
- token: token
|
|
|
- },
|
|
|
+ freshTokenPayload,
|
|
|
process.env.JWT_SECRET as string,
|
|
|
{
|
|
|
expiresIn: "4h"
|
|
|
@@ -56,12 +75,7 @@ const refreshToken = async (input: RefreshTokenInput): Promise<RefreshTokenResul
|
|
|
);
|
|
|
|
|
|
const newRefreshToken = jwt.sign(
|
|
|
- {
|
|
|
- companyName: decoded.companyName,
|
|
|
- fullName: decoded.fullName,
|
|
|
- userID: decoded.userID,
|
|
|
- token: token
|
|
|
- },
|
|
|
+ freshTokenPayload,
|
|
|
process.env.JWT_SECRET as string,
|
|
|
{
|
|
|
expiresIn: "30d"
|