diff --git a/examples/auth/app/auth/auth-utils.ts b/examples/auth/app/auth/auth-utils.ts deleted file mode 100644 index 03d882409..000000000 --- a/examples/auth/app/auth/auth-utils.ts +++ /dev/null @@ -1,18 +0,0 @@ -import {SecurePassword, AuthenticationError} from "blitz" -import db from "db" - -export const authenticateUser = async (email: string, password: string) => { - const user = await db.user.findFirst({where: {email}}) - if (!user) throw new AuthenticationError() - - const result = await SecurePassword.verify(user.hashedPassword, password) - - if (result === SecurePassword.VALID_NEEDS_REHASH) { - // Upgrade hashed password with a more secure hash - const improvedHash = await SecurePassword.hash(password) - await db.user.update({where: {id: user.id}, data: {hashedPassword: improvedHash}}) - } - - const {hashedPassword, ...rest} = user - return rest -} diff --git a/examples/auth/app/auth/mutations/login.ts b/examples/auth/app/auth/mutations/login.ts index b0d022c88..a92715444 100644 --- a/examples/auth/app/auth/mutations/login.ts +++ b/examples/auth/app/auth/mutations/login.ts @@ -1,7 +1,23 @@ -import {Ctx} from "blitz" -import {authenticateUser} from "app/auth/auth-utils" +import {Ctx, SecurePassword, AuthenticationError} from "blitz" +import db from "db" import * as z from "zod" +export const authenticateUser = async (email: string, password: string) => { + const user = await db.user.findFirst({where: {email}}) + if (!user) throw new AuthenticationError() + + const result = await SecurePassword.verify(user.hashedPassword, password) + + if (result === SecurePassword.VALID_NEEDS_REHASH) { + // Upgrade hashed password with a more secure hash + const improvedHash = await SecurePassword.hash(password) + await db.user.update({where: {id: user.id}, data: {hashedPassword: improvedHash}}) + } + + const {hashedPassword, ...rest} = user + return rest +} + export const LoginInput = z.object({ email: z.string().email(), password: z.string(), diff --git a/packages/generator/templates/app/README.md b/packages/generator/templates/app/README.md index 9222a09d1..6bcbc6f10 100644 --- a/packages/generator/templates/app/README.md +++ b/packages/generator/templates/app/README.md @@ -91,7 +91,6 @@ __name__ │   │   ├── pages/ │   │   │   ├── login.tsx │   │   │   └── signup.tsx -│   │   ├── auth-utils.ts │   │   └── validations.ts │   └── users/ │   └── queries/ diff --git a/packages/generator/templates/app/app/auth/auth-utils.ts b/packages/generator/templates/app/app/auth/auth-utils.ts deleted file mode 100644 index c0e8a2d82..000000000 --- a/packages/generator/templates/app/app/auth/auth-utils.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { SecurePassword, AuthenticationError } from "blitz" -import db from "db" - -export const authenticateUser = async (email: string, password: string) => { - const user = await db.user.findFirst({ where: { email } }) - if (!user) throw new AuthenticationError() - - const result = await SecurePassword.verify(user.hashedPassword, password) - - if (result === SecurePassword.VALID_NEEDS_REHASH) { - // Upgrade hashed password with a more secure hash - const improvedHash = await SecurePassword.hash(password) - await db.user.update({ where: { id: user.id }, data: { hashedPassword: improvedHash } }) - } - - const { hashedPassword, ...rest } = user - return rest -} diff --git a/packages/generator/templates/app/app/auth/mutations/login.ts b/packages/generator/templates/app/app/auth/mutations/login.ts index c6ddf5ac3..c7113df0a 100644 --- a/packages/generator/templates/app/app/auth/mutations/login.ts +++ b/packages/generator/templates/app/app/auth/mutations/login.ts @@ -1,7 +1,23 @@ -import { Ctx } from "blitz" -import { authenticateUser } from "app/auth/auth-utils" +import { Ctx, SecurePassword, AuthenticationError } from "blitz" +import db from "db" import { LoginInput, LoginInputType } from "../validations" +export const authenticateUser = async (email: string, password: string) => { + const user = await db.user.findFirst({ where: { email } }) + if (!user) throw new AuthenticationError() + + const result = await SecurePassword.verify(user.hashedPassword, password) + + if (result === SecurePassword.VALID_NEEDS_REHASH) { + // Upgrade hashed password with a more secure hash + const improvedHash = await SecurePassword.hash(password) + await db.user.update({ where: { id: user.id }, data: { hashedPassword: improvedHash } }) + } + + const { hashedPassword, ...rest } = user + return rest +} + export default async function login(input: LoginInputType, { session }: Ctx) { // This throws an error if input is invalid const { email, password } = LoginInput.parse(input)