Skip to content
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

fix(auth/error): improve error handling of auth issues #3950

Merged
merged 1 commit into from
Feb 11, 2025
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
5 changes: 3 additions & 2 deletions packages/server/modules/auth/strategies/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,12 @@ const githubStrategyBuilderFactory =
case InviteNotFoundError:
case UnverifiedEmailSSOLoginError:
logger.info(err)
break
return done(null, false, { message: e.message })
default:
logger.error(err)
// Only when the server is operating abnormally should err be set, to indicate an internal error.
return done(err, false, { message: e.message })
}
return done(err, false, { message: e.message })
}
}
)
Expand Down
5 changes: 3 additions & 2 deletions packages/server/modules/auth/strategies/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,12 @@ const googleStrategyBuilderFactory =
case UserInputError:
case InviteNotFoundError:
logger.info({ err: e })
break
return done(null, false, { message: e.message })
default:
logger.error({ err: e })
// Only when the server is operating abnormally should err be set, to indicate an internal error.
return done(e, false, { message: e.message })
}
return done(e, false, { message: e.message })
}
}
)
Expand Down
12 changes: 11 additions & 1 deletion packages/server/modules/auth/strategies/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
ValidateUserPassword
} from '@/modules/core/domain/users/operations'
import { GetServerInfo } from '@/modules/core/domain/server/operations'
import { UserValidationError } from '@/modules/core/errors/user'

const localStrategyBuilderFactory =
(deps: {
Expand Down Expand Up @@ -69,7 +70,16 @@ const localStrategyBuilderFactory =

return next()
} catch (err) {
req.log.info({ err }, 'Error while logging in.')
const e = ensureError(err, 'Unexpected issue occured while logging in')
switch (e.constructor) {
case UserInputError:
case UserValidationError:
req.log.info({ err }, 'Error while logging in.')
break
default:
req.log.error({ err }, 'Error while logging in.')
Copy link
Contributor Author

@iainsproat iainsproat Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to alert on some types of issues when users are logging in, so this adds error level logging for unexpected errors.

break
}
return res.status(401).send({ err: true, message: 'Invalid credentials.' })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question related to this, should we instead be using next(e) and let the error handling middleware then handle how the error is returned to the user?
https://expressjs.com/en/guide/error-handling.html

}
},
Expand Down
5 changes: 3 additions & 2 deletions packages/server/modules/auth/strategies/oidc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,12 @@ const oidcStrategyBuilderFactory =
case UserInputError:
case InviteNotFoundError:
logger.info({ err: e })
break
return done(null, undefined)
default:
logger.error({ err: e })
// Only when the server is operating abnormally should err be set, to indicate an internal error.
return done(e, undefined)
}
return done(e, undefined)
}
}
)
Expand Down