forked from StellarLend/Stellarlend-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
206 lines (177 loc) · 5.24 KB
/
Copy pathauth.ts
File metadata and controls
206 lines (177 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { NextRequest, NextResponse } from "next/server";
import { cookies } from "next/headers";
import jwt from "jsonwebtoken";
import { User, Session, AuthError } from "@/types/common";
/**
* Auth Configuration
* These should be set via environment variables for production
*/
const AUTH_CONFIG = {
sessionCookieName: process.env.NEXT_PUBLIC_SESSION_COOKIE || "session",
sessionSecret: process.env.AUTH_SECRET || "dev-secret-change-in-production",
sessionExpiryHours: parseInt(process.env.AUTH_SESSION_EXPIRY || "24", 10),
};
const JWT_SECRET = process.env.JWT_SECRET || AUTH_CONFIG.sessionSecret;
import { SignJWT, jwtVerify } from "jose";
/**
* Create a new session JWT
*/
export async function createSession(user: Partial<User>): Promise<string> {
const secret = new TextEncoder().encode(AUTH_CONFIG.sessionSecret);
const alg = "HS256";
const token = await new SignJWT({
userId: user.id,
walletAddress: user.walletAddress,
email: user.email,
name: user.name,
})
.setProtectedHeader({ alg })
.setIssuedAt()
.setExpirationTime(`${AUTH_CONFIG.sessionExpiryHours}h`)
.sign(secret);
return token;
}
/**
* Set session cookie
*/
export async function setSessionCookie(token: string) {
const cookieStore = await cookies();
cookieStore.set({
name: AUTH_CONFIG.sessionCookieName,
value: token,
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "lax",
path: "/",
maxAge: AUTH_CONFIG.sessionExpiryHours * 60 * 60,
});
}
/**
* Get the current session from cookies
* @returns Session if valid, null otherwise
*/
export async function getSession(): Promise<Session | null> {
try {
const cookieStore = await cookies();
const sessionCookie = cookieStore.get(AUTH_CONFIG.sessionCookieName);
if (!sessionCookie?.value) {
return null;
}
const secret = new TextEncoder().encode(AUTH_CONFIG.sessionSecret);
// Verify and parse session
try {
const { payload } = await jwtVerify(sessionCookie.value, secret);
return {
user: {
id: (payload.sub || payload.userId) as string,
email: payload.email as string | undefined,
name: payload.name as string | undefined,
walletAddress: payload.walletAddress as string | undefined,
createdAt: new Date((payload.iat || 0) * 1000),
},
issuedAt: new Date((payload.iat || 0) * 1000),
expiresAt: new Date((payload.exp || 0) * 1000),
} as Session;
} catch (e) {
// Invalid signature or expired token
return null;
}
} catch (error) {
console.error("Session retrieval error:", error);
return null;
}
}
/**
* Get the current authenticated user
* @returns User if authenticated, null otherwise
*/
export async function getUser(): Promise<User | null> {
try {
const session = await getSession();
return session?.user || null;
} catch (error) {
console.error("User retrieval error:", error);
return null;
}
}
/**
* Check if the current user is authenticated
*/
/**
* Get authenticated user with error handling
* @returns User or throws AuthError
*/
export async function getAuthenticatedUser(): Promise<User> {
const user = await getUser();
if (!user) {
throw {
code: "UNAUTHENTICATED",
message: "User is not authenticated",
} as AuthError;
}
return user;
}
/**
* Get session expiry info (useful for client-side logic)
* @returns Expiry info or null
*/
export async function getSessionExpiry(): Promise<{ expiresAt: Date; expiresIn: number } | null> {
try {
const session = await getSession();
if (!session?.expiresAt) return null;
const now = new Date();
const expiresIn = Math.max(0, session.expiresAt.getTime() - now.getTime());
return {
expiresAt: session.expiresAt,
expiresIn,
};
} catch (error) {
console.error("Session expiry retrieval error:", error);
return null;
}
}
/**
* Check if user is authenticated (server-side)
* @returns true if user has valid session
*/
export async function isAuthenticated(): Promise<boolean> {
const user = await getUser();
return user !== null;
}
export interface AuthUser {
id: string;
email: string;
}
export function getAuthUser(req: NextRequest): AuthUser | null {
try {
const authHeader = req.headers.get("authorization") ?? "";
const bearerToken = authHeader.startsWith("Bearer ")
? authHeader.slice(7)
: null;
const cookieToken = req.cookies.get("session")?.value ?? null;
const token = bearerToken ?? cookieToken;
if (!token) return null;
const payload = jwt.verify(token, JWT_SECRET) as jwt.JwtPayload;
if (typeof payload.sub !== "string" || typeof payload.email !== "string") {
return null;
}
return { id: payload.sub, email: payload.email };
} catch {
return null;
}
}
export function requireAuth(req: NextRequest): AuthUser {
const user = getAuthUser(req);
if (!user) {
throw NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
return user;
}
/**
* Create a signed JWT for testing or internal use.
*/
export function signToken(user: AuthUser, expiresIn = "1h"): string {
return jwt.sign({ sub: user.id, email: user.email }, JWT_SECRET, {
expiresIn: expiresIn as any,
});
}