Skip to content

Commit

Permalink
fix: active directly
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyLv committed Mar 3, 2023
1 parent b996ecd commit eae586e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
6 changes: 3 additions & 3 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Redis } from "@upstash/redis";
import type { NextFetchEvent, NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { checkLicenseKey } from "./utils/3rd/lemon";
import { activeLicenseKey } from "./utils/3rd/lemon";
import { checkOpenaiApiKey } from "./utils/3rd/openai";
import { ratelimit } from "./utils/3rd/upstash";
import { isDev } from "./utils/env";
Expand All @@ -23,12 +23,12 @@ export async function middleware(req: NextRequest, context: NextFetchEvent) {
}

// 3. something-invalid-sdalkjfasncs-key
if (!(await checkLicenseKey(apiKey))) {
if (!(await activeLicenseKey(apiKey, bvId))) {
return NextResponse.redirect(new URL("/shop", req.url));
}
}
// TODO: unique to a user (userid, email etc) instead of IP
const identifier = req.ip ?? "127.0.0.5";
const identifier = req.ip ?? "127.0.0.6";
const { success, remaining } = await ratelimit.limit(identifier);
console.log(`======== ip ${identifier}, remaining: ${remaining} ========`);
if (!apiKey && !success) {
Expand Down
42 changes: 18 additions & 24 deletions utils/3rd/lemon.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import { ratelimit } from "./upstash";

export async function checkLicenseKey(licenseKey: string) {
const response = await fetch(`https://api.lemonsqueezy.com/v1/license-keys`, {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.LEMON_API_KEY ?? ""}`,
},
});
const keysData = await response.json();
const licenseKeys = keysData.data?.map((i: any) => {
console.log("========i.attributes========", i.attributes);
return i.attributes.key;
});

if (licenseKeys?.includes(licenseKey.toLowerCase())) {
// TODO: change to supabase after implement user-login
const { remaining } = await ratelimit.limit(licenseKey.toLowerCase());
// TODO: log to hit licenseKey
console.log(
`!!!!!!!!! {short-xxxx-licenseKey}, remaining: ${remaining} ========`
);
return remaining > 0;
}

return false;
export async function activeLicenseKey(licenseKey: string, bvId?: string) {
// https://docs.lemonsqueezy.com/help/licensing/license-api
const response = await fetch(
`https://api.lemonsqueezy.com/v1/licenses/activate`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.LEMON_API_KEY ?? ""}`,
},
body: JSON.stringify({
license_key: licenseKey,
instance_name: bvId || "b.jimmylv.cn",
}),
}
);
const result = await response.json();
return result.actived;
}

1 comment on commit eae586e

@vercel
Copy link

@vercel vercel bot commented on eae586e Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.