team-alembic / ash_authentication

The Ash Authentication framework
MIT License
95 stars 50 forks source link

Improve error when token storage fails during sign-in #742

Open mbuhot opened 3 months ago

mbuhot commented 3 months ago

Somewhat similar to #370, we recently experienced a situation where the database was stuck in a read-only mode after a failover.

Sign-in was failing with an unhelpful message:

** (MatchError) no match of right hand side value: :error

lib/ash_authentication/strategies/password/sign_in_preparation.ex:132 AshAuthentication.Strategy.Password.SignInPreparation.generate_token/3

The SignInPreparation.generate_token fails with MatchError if Jwt.token_for_user returns :error due to being unable to store the token.

Ideally these functions that we expect to rarely fail (like generating a token) would use ! variants of any actions they call rather than failing to match on :error returns, so that a more detailed exception is recorded.

The reason is lost in the else clause of the with expression at: https://github.com/team-alembic/ash_authentication/blob/main/lib/ash_authentication/jwt.ex#L116

jimsynz commented 2 weeks ago

Looks like @simpers slipped this in with #749. It now emits an error into the log.