diff options
| author | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-07-17 10:50:52 +0000 |
|---|---|---|
| committer | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-07-17 10:50:52 +0000 |
| commit | 2ef02e27dbe639876fa3b90c30307dda183545ec (patch) | |
| tree | e132ae7f3dd774e1ce767291c2849be4a63ae762 /lib/tech-vendor-invitation-token.ts | |
| parent | fb276ed3db86fe4fc0c0fcd870fd3d085b034be0 (diff) | |
(최겸) 기술영업 변경사항 적용
Diffstat (limited to 'lib/tech-vendor-invitation-token.ts')
| -rw-r--r-- | lib/tech-vendor-invitation-token.ts | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/lib/tech-vendor-invitation-token.ts b/lib/tech-vendor-invitation-token.ts new file mode 100644 index 00000000..83c82448 --- /dev/null +++ b/lib/tech-vendor-invitation-token.ts @@ -0,0 +1,83 @@ +"use server";
+
+import { SignJWT, jwtVerify } from "jose";
+
+// JWT_SECRET 환경변수 검증
+if (!process.env.JWT_SECRET) {
+ console.error("JWT_SECRET environment variable is not set. Please configure JWT_SECRET in your environment variables.");
+ throw new Error("JWT_SECRET environment variable is required");
+}
+
+const secretKey = process.env.JWT_SECRET;
+const key = new TextEncoder().encode(secretKey);
+
+export interface TechVendorInvitationPayload {
+ vendorId: number;
+ vendorName: string;
+ email: string;
+ type: "tech-vendor-invitation";
+ expiresAt: number;
+}
+
+/**
+ * 기술영업 벤더 초대용 일회성 토큰 생성
+ */
+export async function createTechVendorInvitationToken(payload: {
+ vendorId: number;
+ vendorName: string;
+ email: string;
+}): Promise<string> {
+ const expiresAt = Date.now() + 7 * 24 * 60 * 60 * 1000; // 7일
+
+ const token = await new SignJWT({
+ vendorId: payload.vendorId,
+ vendorName: payload.vendorName,
+ email: payload.email,
+ type: "tech-vendor-invitation",
+ expiresAt,
+ })
+ .setProtectedHeader({ alg: "HS256" })
+ .setIssuedAt()
+ .setExpirationTime("7d")
+ .sign(key);
+
+ return token;
+}
+
+/**
+ * 기술영업 벤더 초대 토큰 검증
+ */
+export async function verifyTechVendorInvitationToken(
+ token: string
+): Promise<TechVendorInvitationPayload | null> {
+ try {
+ const { payload } = await jwtVerify(token, key);
+
+ const tokenPayload = payload as unknown as TechVendorInvitationPayload;
+
+ // 토큰 타입 검증
+ if (tokenPayload.type !== "tech-vendor-invitation") {
+ console.error("Invalid token type:", tokenPayload.type);
+ return null;
+ }
+
+ // 만료 시간 검증
+ if (Date.now() > tokenPayload.expiresAt) {
+ console.error("Token has expired");
+ return null;
+ }
+
+ return tokenPayload;
+ } catch (error) {
+ console.error("Token verification failed:", error);
+ return null;
+ }
+}
+
+/**
+ * 초대 토큰을 포함한 가입 URL 생성
+ */
+export async function createTechVendorSignupUrl(token: string): Promise<string> {
+ const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || "http://localhost:3000";
+ return `${baseUrl}/ko/auth/tech-signup?token=${token}`;
+}
\ No newline at end of file |
