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
15 changes: 15 additions & 0 deletions lib/auth-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { z } from 'zod';

export const LoginSchema = z.object({
email: z.string().email('Invalid email address'),
password: z.string()
.min(8, 'Password must be at least 8 characters')
.max(50, 'Password must be less than 50 characters')
});

export const RegisterSchema = LoginSchema.extend({
name: z.string()
.min(2, 'Name must be at least 2 characters')
.max(50, 'Name must be less than 50 characters')
.optional()
});
35 changes: 35 additions & 0 deletions lib/auth-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { LoginCredentials, RegisterCredentials, User } from '../types/auth';
import { v4 as uuidv4 } from 'uuid';

// Simulated in-memory storage (replace with actual database later)
const USERS: User[] = [];

export const authService = {
register: async (credentials: RegisterCredentials): Promise<User> => {
const existingUser = USERS.find(u => u.email === credentials.email);
if (existingUser) {
throw new Error('User already exists');
}

const newUser: User = {
id: uuidv4(),
email: credentials.email,
name: credentials.name
};

USERS.push(newUser);
return newUser;
},

login: async (credentials: LoginCredentials): Promise<User> => {
const user = USERS.find(u => u.email === credentials.email);
if (!user) {
throw new Error('Invalid credentials');
}
return user;
},

getCurrentUser: (): User | null => {
return USERS.length > 0 ? USERS[0] : null;
}
};
Loading