Skip to content
Merged
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
76 changes: 76 additions & 0 deletions app/api/auth/log-in/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// app/api/auth/log-in/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';
import { createServerClient } from '@/lib/supabase/server';

// Validation schema for log-in
const logInSchema = z.object({
email: z
.string()
.email('Invalid email address')
.transform((val) => val.toLowerCase().trim()),
password: z
.string()
.min(8, 'Password must be at least 8 characters')
.regex(
/^(?=.*[A-Z])(?=.*[!@#$%^&*])/,
'Password must contain at least 1 uppercase letter and 1 special symbol',
),
});

export async function POST(request: NextRequest) {
try {
const body = await request.json();

// Validate request body
const validatedData = logInSchema.parse(body);

// Create Supabase client
const supabase = createServerClient();

// Log in user with Supabase Auth
const { data, error } = await supabase.auth.signInWithPassword({
email: validatedData.email,
password: validatedData.password,
});

if (error) {
return NextResponse.json(
{
error: error.message,
},
{ status: 401 },
);
}

return NextResponse.json(
{
message: 'Login successful',
user: {
id: data.user?.id,
email: data.user?.email,
},
session: {
access_token: data.session?.access_token,
},
},
{ status: 200 },
);
} catch (error: unknown) {
if (error instanceof z.ZodError) {
return NextResponse.json(
{
error: 'Validation failed',
details: (error as z.ZodError).errors,
},
{ status: 400 },
);
}

console.error('Log-in error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 },
);
}
}
79 changes: 79 additions & 0 deletions app/api/auth/sign-in/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// app/api/auth/sign-in/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';
import { createServerClient } from '@/lib/supabase/server';

// Validation schema matching acceptance criteria
const signInSchema = z
.object({
email: z
.string()
.email('Invalid email address')
.transform((val) => val.toLowerCase().trim()),
password: z
.string()
.min(8, 'Password must be at least 8 characters')
.regex(
/^(?=.*[A-Z])(?=.*[!@#$%^&*])/,
'Password must contain at least 1 uppercase letter and 1 special symbol',
),
confirm_password: z.string(),
})
.refine((data) => data.password === data.confirm_password, {
message: 'Passwords must match exactly',
path: ['confirm_password'],
});

export async function POST(request: NextRequest) {
try {
const body = await request.json();

// Validate request body
const validatedData = signInSchema.parse(body);

// Create Supabase client
const supabase = createServerClient();

// Sign up user with Supabase Auth
const { data, error } = await supabase.auth.signUp({
email: validatedData.email,
password: validatedData.password,
});

if (error) {
return NextResponse.json(
{
error: error.message,
},
{ status: 400 },
);
}

return NextResponse.json(
{
message: 'User registered successfully',
user: {
id: data.user?.id,
email: data.user?.email,
},
},
{ status: 201 },
);
} catch (error: unknown) {
if (error instanceof z.ZodError) {
return NextResponse.json(
{
error: 'Validation failed',
details: (error as z.ZodError).errors,
},
{ status: 400 },
);
}

console.error('Sign-in error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 },
);
}
}
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"react": "19.2.1",
"react-dom": "19.2.1",
"tailwind-merge": "^3.4.0",
"zod": "^4.3.6",
"zustand": "^5.0.9"
},
"devDependencies": {
Expand Down
Loading