Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface Bindings {
IMAGES_ACCOUNT_ID?: string
IMAGES_API_TOKEN?: string
ENVIRONMENT?: string
JWT_SECRET?: string
BUCKET_NAME?: string
GOOGLE_MAPS_API_KEY?: string
}
Expand Down
20 changes: 11 additions & 9 deletions packages/core/src/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ type JWTPayload = {
iat: number
}

// JWT secret - in production this should come from environment variables
const JWT_SECRET = 'your-super-secret-jwt-key-change-in-production'
// Fallback JWT secret for local development only (no wrangler secret set)
const JWT_SECRET_FALLBACK = 'your-super-secret-jwt-key-change-in-production'

export class AuthManager {
static async generateToken(userId: string, email: string, role: string): Promise<string> {
static async generateToken(userId: string, email: string, role: string, secret?: string): Promise<string> {
const payload: JWTPayload = {
userId,
email,
role,
exp: Math.floor(Date.now() / 1000) + (60 * 60 * 24), // 24 hours
iat: Math.floor(Date.now() / 1000)
}
return await sign(payload, JWT_SECRET, 'HS256')

return await sign(payload, secret || JWT_SECRET_FALLBACK, 'HS256')
}

static async verifyToken(token: string): Promise<JWTPayload | null> {
static async verifyToken(token: string, secret?: string): Promise<JWTPayload | null> {
try {
const payload = await verify(token, JWT_SECRET, 'HS256') as JWTPayload
const payload = await verify(token, secret || JWT_SECRET_FALLBACK, 'HS256') as JWTPayload

// Check if token is expired
if (payload.exp < Math.floor(Date.now() / 1000)) {
Expand Down Expand Up @@ -112,7 +112,8 @@ export const requireAuth = () => {

// If not cached, verify token
if (!payload) {
payload = await AuthManager.verifyToken(token)
const jwtSecret = (c.env as any)?.JWT_SECRET
payload = await AuthManager.verifyToken(token, jwtSecret)

// Cache the verified payload for 5 minutes
if (payload && kv) {
Expand Down Expand Up @@ -186,7 +187,8 @@ export const optionalAuth = () => {
}

if (token) {
const payload = await AuthManager.verifyToken(token)
const jwtSecret = (c.env as any)?.JWT_SECRET
const payload = await AuthManager.verifyToken(token, jwtSecret)
if (payload) {
c.set('user', payload)
}
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/plugins/available/magic-link-auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ export function createMagicLinkAuthPlugin(): Plugin {
const jwtToken = await AuthManager.generateToken(
user.id,
user.email,
user.role
user.role,
(c.env as any).JWT_SECRET
)

// Set auth cookie
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export function createOTPLoginPlugin(): Plugin {
}

// Generate JWT token
const token = await AuthManager.generateToken(user.id, user.email, user.role)
const token = await AuthManager.generateToken(user.id, user.email, user.role, (c.env as any).JWT_SECRET)

// Set HTTP-only cookie
setCookie(c, 'auth_token', token, {
Expand Down
16 changes: 8 additions & 8 deletions packages/core/src/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ authRoutes.post('/register',
).run()

// Generate JWT token
const token = await AuthManager.generateToken(userId, normalizedEmail, 'viewer')
const token = await AuthManager.generateToken(userId, normalizedEmail, 'viewer', c.env.JWT_SECRET)

// Set HTTP-only cookie
setCookie(c, 'auth_token', token, {
Expand Down Expand Up @@ -226,8 +226,8 @@ authRoutes.post('/login', async (c) => {
}

// Generate JWT token
const token = await AuthManager.generateToken(user.id, user.email, user.role)
const token = await AuthManager.generateToken(user.id, user.email, user.role, c.env.JWT_SECRET)

// Set HTTP-only cookie
setCookie(c, 'auth_token', token, {
httpOnly: true,
Expand Down Expand Up @@ -323,7 +323,7 @@ authRoutes.post('/refresh', requireAuth(), async (c) => {
}

// Generate new token
const token = await AuthManager.generateToken(user.userId, user.email, user.role)
const token = await AuthManager.generateToken(user.userId, user.email, user.role, c.env.JWT_SECRET)

// Set new cookie
setCookie(c, 'auth_token', token, {
Expand Down Expand Up @@ -436,7 +436,7 @@ authRoutes.post('/register/form', async (c) => {
).run()

// Generate JWT token
const token = await AuthManager.generateToken(userId, normalizedEmail, role)
const token = await AuthManager.generateToken(userId, normalizedEmail, role, c.env.JWT_SECRET)

// Set HTTP-only cookie
setCookie(c, 'auth_token', token, {
Expand Down Expand Up @@ -516,8 +516,8 @@ authRoutes.post('/login/form', async (c) => {
}

// Generate JWT token
const token = await AuthManager.generateToken(user.id, user.email, user.role)
const token = await AuthManager.generateToken(user.id, user.email, user.role, c.env.JWT_SECRET)

// Set HTTP-only cookie
setCookie(c, 'auth_token', token, {
httpOnly: true,
Expand Down Expand Up @@ -884,7 +884,7 @@ authRoutes.post('/accept-invitation', async (c) => {
).run()

// Generate JWT token for auto-login
const authToken = await AuthManager.generateToken(invitedUser.id, invitedUser.email, invitedUser.role)
const authToken = await AuthManager.generateToken(invitedUser.id, invitedUser.email, invitedUser.role, c.env.JWT_SECRET)

// Set HTTP-only cookie
setCookie(c, 'auth_token', authToken, {
Expand Down