Skip to content

Commit

Permalink
Throw invalid credentials error explictly
Browse files Browse the repository at this point in the history
  • Loading branch information
infomiho committed Oct 28, 2024
1 parent 2ac228e commit 808acbb
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 23 deletions.
6 changes: 3 additions & 3 deletions waspc/data/Generator/templates/sdk/wasp/auth/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { type AuthUserData } from '../server/auth/user.js';

import { auth } from "./lucia.js";
import type { Session } from "lucia";
import { throwInvalidCredentialsError } from "./utils.js";
import { createInvalidCredentialsError } from "./utils.js";

import { prisma } from 'wasp/server';
import { createAuthUserData } from "../server/auth/user.js";
Expand Down Expand Up @@ -66,10 +66,10 @@ async function getAuthUserData(userId: {= userEntityUpper =}['id']): Promise<Aut
})

if (!user) {
throwInvalidCredentialsError()
throw createInvalidCredentialsError()
}

return createAuthUserData(user!);
return createAuthUserData(user);
}

// PRIVATE API
Expand Down
4 changes: 2 additions & 2 deletions waspc/data/Generator/templates/sdk/wasp/auth/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,6 @@ function providerDataHasPasswordField(
}

// PRIVATE API
export function throwInvalidCredentialsError(message?: string): void {
throw new HttpError(401, 'Invalid credentials', { message })
export function createInvalidCredentialsError(message?: string): HttpError {
return new HttpError(401, 'Invalid credentials', { message })
}
8 changes: 4 additions & 4 deletions waspc/data/Generator/templates/sdk/wasp/core/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { handleRejection } from 'wasp/server/utils'
import { getSessionAndUserFromBearerToken } from 'wasp/auth/session'
import { throwInvalidCredentialsError } from 'wasp/auth/utils'
import { createInvalidCredentialsError } from 'wasp/auth/utils'

/**
* Auth middleware
Expand Down Expand Up @@ -28,11 +28,11 @@ const auth = handleRejection(async (req, res, next) => {
const sessionAndUser = await getSessionAndUserFromBearerToken(req)

if (sessionAndUser === null) {
throwInvalidCredentialsError()
throw createInvalidCredentialsError()
}

req.sessionId = sessionAndUser!.session.id
req.user = sessionAndUser!.user
req.sessionId = sessionAndUser.session.id
req.user = sessionAndUser.user

next()
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from 'express';
import { throwInvalidCredentialsError } from 'wasp/auth/utils'
import { createInvalidCredentialsError } from 'wasp/auth/utils'
import { verifyPassword } from 'wasp/auth/password'
import {
createProviderId,
Expand All @@ -22,16 +22,16 @@ export function getLoginRoute() {
const providerId = createProviderId("email", fields.email)
const authIdentity = await findAuthIdentity(providerId)
if (!authIdentity) {
throwInvalidCredentialsError()
throw createInvalidCredentialsError()
}
const providerData = deserializeAndSanitizeProviderData<'email'>(authIdentity.providerData)
if (!providerData.isEmailVerified) {
throwInvalidCredentialsError()
throw createInvalidCredentialsError()
}
try {
await verifyPassword(providerData.hashedPassword, fields.password);
} catch(e) {
throwInvalidCredentialsError()
throw createInvalidCredentialsError()
}

const auth = await findAuthWithUserBy({ id: authIdentity.authId })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{={= =}=}}
import { throwInvalidCredentialsError } from 'wasp/auth/utils'
import { createInvalidCredentialsError } from 'wasp/auth/utils'
import { handleRejection } from 'wasp/server/utils'
import { verifyPassword } from 'wasp/auth/password'

Expand All @@ -20,15 +20,15 @@ export default handleRejection(async (req, res) => {
const providerId = createProviderId('username', fields.username)
const authIdentity = await findAuthIdentity(providerId)
if (!authIdentity) {
throwInvalidCredentialsError()
throw createInvalidCredentialsError()
}

try {
const providerData = deserializeAndSanitizeProviderData<'username'>(authIdentity.providerData)

await verifyPassword(providerData.hashedPassword, fields.password)
} catch(e) {
throwInvalidCredentialsError()
throw createInvalidCredentialsError()
}

const auth = await findAuthWithUserBy({
Expand Down
4 changes: 2 additions & 2 deletions waspc/data/Generator/templates/server/src/crud/_operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { prisma } from 'wasp/server'

{=# isAuthEnabled =}
import { throwInvalidCredentialsError } from 'wasp/auth/utils'
import { createInvalidCredentialsError } from 'wasp/auth/utils'
{=/ isAuthEnabled =}
import type {
{=# crud.operations.GetAll =}
Expand Down Expand Up @@ -175,7 +175,7 @@ export async function deleteFn(args, context) {
function throwIfNotAuthenticated (context) {
{=# isAuthEnabled =}
if (!context.user) {
throwInvalidCredentialsError()
throw createInvalidCredentialsError()
}
{=/ isAuthEnabled =}
{=^ isAuthEnabled =}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { handleRejection } from 'wasp/server/utils'
import { throwInvalidCredentialsError } from 'wasp/auth/utils'
import { createInvalidCredentialsError } from 'wasp/auth/utils'
import { invalidateSession } from 'wasp/auth/session'

export default handleRejection(async (req, res) => {
if (req.sessionId) {
await invalidateSession(req.sessionId)
return res.json({ success: true })
} else {
throwInvalidCredentialsError()
throw createInvalidCredentialsError()
}
})
4 changes: 2 additions & 2 deletions waspc/data/Generator/templates/server/src/routes/auth/me.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { serialize as superjsonSerialize } from 'superjson'
import { handleRejection } from 'wasp/server/utils'
import { throwInvalidCredentialsError } from 'wasp/auth/utils'
import { createInvalidCredentialsError } from 'wasp/auth/utils'

export default handleRejection(async (req, res) => {
if (req.user) {
return res.json(superjsonSerialize(req.user))
} else {
throwInvalidCredentialsError()
throw createInvalidCredentialsError()
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function addUserToSocketDataIfAuthenticated(socket: Socket, next: (err?: E
const sessionId = socket.handshake.auth.sessionId
if (sessionId) {
try {
const sessionAndUser = await getSessionAndUserFromSessionId(sessionId)
const sessionAndUser = await getSessionAndUserFromSessionId(sessionId)
const user = sessionAndUser ? makeAuthUserIfPossible(sessionAndUser.user) : null
socket.data = {
...socket.data,
Expand Down

0 comments on commit 808acbb

Please sign in to comment.