Skip to content

Commit b59f454

Browse files
committed
Refactor session creation and suspicious checks
Add UserSession typing and adjust session creation parameters (use milliseconds for 7 days). Make detectSuspiciousActivity usage stricter by only running it when prior sessions exist, construct a sessionWithLocation object for the check, and adapt to the new suspiciousFlags shape (suspicious + reasons). Update security notification payloads to use the request IP and fallback 'Unknown' location, and change clearRateLimit call to accept the request with a key callback. Left mock/allUserSessions and DB save TODOs intact.
1 parent 4a1fde9 commit b59f454

1 file changed

Lines changed: 39 additions & 23 deletions

File tree

app/api/auth/login/route.ts

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { NextRequest, NextResponse } from 'next/server'
22
import { loginRateLimit, clearRateLimit } from '@/lib/utils/rate-limiter'
33
import { verifyMFAToken } from '@/lib/utils/mfa'
44
import { sendSecurityNotification } from '@/lib/utils/security-notifications'
5-
import { createSession, detectSuspiciousActivity } from '@/lib/utils/session-manager'
5+
import { createSession, detectSuspiciousActivity, type UserSession } from '@/lib/utils/session-manager'
66
import bcrypt from 'bcryptjs'
77

88
// TODO: Replace with your actual database client
@@ -116,11 +116,11 @@ export async function POST(request: NextRequest) {
116116
}
117117

118118
// 7. Create session
119-
const session = await createSession(
119+
const session = createSession(
120120
user.id,
121121
userAgent,
122122
ip,
123-
{ expiresInDays: 7 }
123+
7 * 24 * 60 * 60 * 1000 // 7 days in milliseconds
124124
)
125125

126126
// 8. Check for suspicious activity
@@ -129,23 +129,39 @@ export async function POST(request: NextRequest) {
129129
// where: { userId: user.id, isActive: true }
130130
// })
131131

132-
const allUserSessions = [] // Mock empty sessions
133-
134-
const suspiciousFlags = detectSuspiciousActivity(session, allUserSessions)
132+
const allUserSessions: UserSession[] = [] // Mock empty sessions
135133

136-
if (suspiciousFlags.length > 0) {
137-
await sendSecurityNotification({
138-
type: 'suspicious_activity',
139-
userEmail: user.email,
140-
userName: user.name,
141-
details: {
142-
reason: suspiciousFlags.join(', '),
143-
ipAddress: session.ipAddress,
144-
location: session.location,
145-
device: `${session.deviceInfo.browser} on ${session.deviceInfo.os}`,
146-
timestamp: new Date().toISOString(),
134+
// Only check for suspicious activity if there's a previous session
135+
if (allUserSessions.length > 0) {
136+
const previousSession = allUserSessions[0]
137+
// Add location to current session for suspicious activity check
138+
const sessionWithLocation: UserSession = {
139+
...session,
140+
location: {
141+
ip: ip,
142+
country: undefined,
143+
city: undefined
147144
},
148-
})
145+
isActive: true,
146+
isCurrent: true
147+
}
148+
149+
const suspiciousFlags = detectSuspiciousActivity(sessionWithLocation, previousSession)
150+
151+
if (suspiciousFlags.suspicious && suspiciousFlags.reasons.length > 0) {
152+
await sendSecurityNotification({
153+
type: 'suspicious_activity',
154+
userEmail: user.email,
155+
userName: user.name,
156+
details: {
157+
reason: suspiciousFlags.reasons.join(', '),
158+
ipAddress: ip,
159+
location: 'Unknown',
160+
device: `${session.deviceInfo.browser} on ${session.deviceInfo.os}`,
161+
timestamp: new Date().toISOString(),
162+
},
163+
})
164+
}
149165
}
150166

151167
// 9. Save session to database
@@ -165,16 +181,16 @@ export async function POST(request: NextRequest) {
165181
// })
166182

167183
// 10. Clear rate limit on successful login
168-
await clearRateLimit(`login:${ip}`)
184+
await clearRateLimit(request, () => `login:${ip}`)
169185

170186
// 11. Send login notification
171187
await sendSecurityNotification({
172188
type: 'login',
173189
userEmail: user.email,
174190
userName: user.name,
175191
details: {
176-
ipAddress: session.ipAddress,
177-
location: session.location,
192+
ipAddress: ip,
193+
location: 'Unknown',
178194
device: `${session.deviceInfo.browser} on ${session.deviceInfo.os}`,
179195
timestamp: new Date().toISOString(),
180196
},
@@ -186,8 +202,8 @@ export async function POST(request: NextRequest) {
186202
// where: { id: user.id },
187203
// data: {
188204
// lastLoginAt: new Date(),
189-
// lastLoginIP: session.ipAddress,
190-
// lastLoginLocation: session.location,
205+
// lastLoginIP: ip,
206+
// lastLoginLocation: 'Unknown',
191207
// }
192208
// })
193209

0 commit comments

Comments
 (0)