-
Notifications
You must be signed in to change notification settings - Fork 0
49/auth #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
49/auth #52
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,8 @@ | ||||||||||||||||||||
| /* | ||||||||||||||||||||
| Warnings: | ||||||||||||||||||||
|
|
||||||||||||||||||||
| - Added the required column `hashedPassword` to the `User` table without a default value. This is not possible if the table is not empty. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| */ | ||||||||||||||||||||
| -- AlterTable | ||||||||||||||||||||
| ALTER TABLE "public"."User" ADD COLUMN "hashedPassword" TEXT NOT NULL; | ||||||||||||||||||||
|
||||||||||||||||||||
| ALTER TABLE "public"."User" ADD COLUMN "hashedPassword" TEXT NOT NULL; | |
| -- Step 1: Add the column as nullable | |
| ALTER TABLE "public"."User" ADD COLUMN "hashedPassword" TEXT; | |
| -- Step 2: Backfill existing rows with a safe default (empty string) | |
| UPDATE "public"."User" SET "hashedPassword" = '' WHERE "hashedPassword" IS NULL; | |
| -- Step 3: Set the column as NOT NULL | |
| ALTER TABLE "public"."User" ALTER COLUMN "hashedPassword" SET NOT NULL; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ async function main() { | |
| update: {}, | ||
| create: { | ||
| id: 'e99335bd-9dd7-4260-8977-2eeaa4df799c', | ||
| hashedPassword: 'abcd', | ||
|
cherman23 marked this conversation as resolved.
|
||
| name: 'Admin User', | ||
| email: 'admin@techcorp.com', | ||
| }, | ||
|
|
@@ -20,6 +21,7 @@ async function main() { | |
| create: { | ||
| id: '68992d1e-e119-4874-b768-bf685d10194e', | ||
| name: 'John Doe', | ||
| hashedPassword: 'abcd', | ||
|
cherman23 marked this conversation as resolved.
|
||
| email: 'john.doe@techcorp.com', | ||
| }, | ||
| }), | ||
|
|
@@ -29,6 +31,7 @@ async function main() { | |
| create: { | ||
| id: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890', | ||
| name: 'Jane Smith', | ||
| hashedPassword: 'abcd', | ||
|
cherman23 marked this conversation as resolved.
|
||
| email: 'jane.smith@startupxyz.com', | ||
| }, | ||
| }), | ||
|
|
@@ -38,6 +41,7 @@ async function main() { | |
| create: { | ||
| id: 'b2c3d4e5-f6g7-8901-bcde-f12345678901', | ||
| name: 'Bob Wilson', | ||
| hashedPassword: 'abcd', | ||
|
||
| email: 'bob.wilson@enterprise.com', | ||
| }, | ||
| }), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export default function LoginPage() { | ||
| return <div className="">login</div>; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export default function SignupPage() { | ||
| return <div className="">signup</div>; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export default function DashboardPage() { | ||
| return <div className=""></div>; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { type NextRequest } from 'next/server'; | ||
| import { sargeApiError, sargeApiResponse } from '@/lib/responses'; | ||
| import { login } from '@/lib/auth/auth-service'; | ||
| import { InvalidInputError } from '@/lib/schemas/errors'; | ||
|
|
||
| export async function POST(request: NextRequest) { | ||
| try { | ||
| const body = await request.json(); | ||
|
|
||
| if (!body.email || !body.password) { | ||
| return sargeApiError('Email and password are required', 400); | ||
| } | ||
|
|
||
| const user = await login({ | ||
| email: body.email, | ||
| password: body.password, | ||
| }); | ||
|
|
||
| return sargeApiResponse(user, 200); | ||
| } catch (error) { | ||
| if (error instanceof InvalidInputError) { | ||
| return sargeApiError(error.message, 400); | ||
| } | ||
|
Comment on lines
+21
to
+23
|
||
|
|
||
| const message = error instanceof Error ? error.message : String(error); | ||
|
|
||
| if (message.includes('Invalid credentials')) { | ||
| return sargeApiError('Invalid email or password', 401); | ||
| } | ||
|
|
||
| if (message.includes('Login implementation needed')) { | ||
| return sargeApiError('Authentication not yet configured', 501); | ||
| } | ||
|
Comment on lines
+27
to
+33
|
||
|
|
||
| return sargeApiError(message, 500); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,13 @@ | ||||||||||||||||||||||
| import { type NextRequest } from 'next/server'; | ||||||||||||||||||||||
| import { sargeApiResponse } from '@/lib/responses'; | ||||||||||||||||||||||
| import { logout } from '@/lib/auth/auth-service'; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| export async function POST(_request: NextRequest) { | ||||||||||||||||||||||
| try { | ||||||||||||||||||||||
| await logout(); | ||||||||||||||||||||||
|
Comment on lines
+3
to
+7
|
||||||||||||||||||||||
| import { logout } from '@/lib/auth/auth-service'; | |
| export async function POST(_request: NextRequest) { | |
| try { | |
| await logout(); | |
| import { deleteSession } from '@/lib/auth/auth-service'; | |
| export async function POST(_request: NextRequest) { | |
| try { | |
| await deleteSession(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { type NextRequest } from 'next/server'; | ||
| import { sargeApiResponse, sargeApiError } from '@/lib/responses'; | ||
| import { getCurrentUser } from '@/lib/auth/auth-service'; | ||
|
|
||
| export async function GET(_request: NextRequest) { | ||
| try { | ||
| const user = await getCurrentUser(); | ||
|
|
||
| if (!user) { | ||
| return sargeApiError('Not authenticated', 401); | ||
| } | ||
|
|
||
| return sargeApiResponse(user, 200); | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| return sargeApiError(message, 500); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This migration will fail on non-empty tables because it adds a NOT NULL column without a default/backfill. Use a two-step migration (add nullable column, backfill, then set NOT NULL) or provide a safe default and backfill before enforcing NOT NULL.