UNPKG

@wernerthiago/teo

Version:

Test Execution Optimizer

77 lines (60 loc) 1.85 kB
// Authentication module export class AuthService { constructor() { this.users = new Map() this.sessions = new Map() } async login(username, password) { const user = this.users.get(username) if (!user || user.password !== password) { throw new Error('Invalid credentials') } // Enhanced security: Check for account lockout if (user.locked) { throw new Error('Account is locked') } const sessionId = this.generateSessionId() this.sessions.set(sessionId, { userId: user.id, createdAt: new Date() }) // Reset failed login attempts on successful login user.failedAttempts = 0 return { sessionId, user: { id: user.id, username: user.username } } } async register(username, password, email) { if (this.users.has(username)) { throw new Error('Username already exists') } const user = { id: this.generateUserId(), username, password, // In real app, this would be hashed email, createdAt: new Date() } this.users.set(username, user) return { id: user.id, username: user.username } } async logout(sessionId) { return this.sessions.delete(sessionId) } async validateSession(sessionId) { const session = this.sessions.get(sessionId) if (!session) { return null } // Check if session is expired (24 hours) const now = new Date() const sessionAge = now - session.createdAt if (sessionAge > 24 * 60 * 60 * 1000) { this.sessions.delete(sessionId) return null } return session } generateSessionId() { return Math.random().toString(36).substring(2) + Date.now().toString(36) } generateUserId() { return Math.random().toString(36).substring(2) + Date.now().toString(36) } } export default AuthService