1
0
mirror of synced 2026-02-03 18:01:02 -05:00

(newapp) Remove app/auth/auth-utils.ts by moving code into the login mutation (#1740)

This commit is contained in:
Brandon Bayer
2021-01-18 18:44:03 -05:00
committed by GitHub
parent fb8a352349
commit 2fc04d3f02
5 changed files with 36 additions and 41 deletions

View File

@@ -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
}

View File

@@ -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(),

View File

@@ -91,7 +91,6 @@ __name__
│   │   ├── pages/
│   │   │   ├── login.tsx
│   │   │   └── signup.tsx
│   │   ├── auth-utils.ts
│   │   └── validations.ts
│   └── users/
│   └── queries/

View File

@@ -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
}

View File

@@ -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)