Add error for already used token
Signed-off-by: Bryan Frimin <bryan@probo.com>
This commit is contained in:
@@ -680,7 +680,7 @@ func (s AuthService) OpenSessionWithMagicLink(ctx context.Context, tokenString s
|
||||
|
||||
if err := token.LoadByHashedValueForUpdate(ctx, tx, hashedValue); err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return NewInvalidTokenError()
|
||||
return NewTokenAlreadyUsedError()
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot load token by hashed value: %w", err)
|
||||
|
||||
@@ -31,6 +31,16 @@ func (e ErrInvalidToken) Error() string {
|
||||
return e.message
|
||||
}
|
||||
|
||||
type ErrTokenAlreadyUsed struct{ message string }
|
||||
|
||||
func NewTokenAlreadyUsedError() error {
|
||||
return &ErrTokenAlreadyUsed{"this magic link has already been used"}
|
||||
}
|
||||
|
||||
func (e ErrTokenAlreadyUsed) Error() string {
|
||||
return e.message
|
||||
}
|
||||
|
||||
type ErrExpiredToken struct{ message string }
|
||||
|
||||
func NewExpiredTokenError() error {
|
||||
|
||||
@@ -83,6 +83,10 @@ func (r *mutationResolver) VerifyMagicLink(ctx context.Context, input types.Veri
|
||||
return nil, gqlutils.TokenExpired(ctx, err)
|
||||
}
|
||||
|
||||
if _, ok := errors.AsType[*iam.ErrTokenAlreadyUsed](err); ok {
|
||||
return nil, gqlutils.TokenAlreadyUsed(ctx, err)
|
||||
}
|
||||
|
||||
if _, ok := errors.AsType[*iam.ErrInvalidToken](err); ok {
|
||||
return nil, gqlutils.Invalid(ctx, err)
|
||||
}
|
||||
@@ -105,6 +109,10 @@ func (r *mutationResolver) VerifyMagicLink(ctx context.Context, input types.Veri
|
||||
return nil, gqlutils.TokenExpired(ctx, err)
|
||||
}
|
||||
|
||||
if _, ok := errors.AsType[*iam.ErrTokenAlreadyUsed](err); ok {
|
||||
return nil, gqlutils.TokenAlreadyUsed(ctx, err)
|
||||
}
|
||||
|
||||
if _, ok := errors.AsType[*iam.ErrInvalidToken](err); ok {
|
||||
return nil, gqlutils.Invalid(ctx, err)
|
||||
}
|
||||
|
||||
@@ -150,6 +150,14 @@ func TokenExpired(ctx context.Context, err error) *gqlerror.Error {
|
||||
}
|
||||
}
|
||||
|
||||
func TokenAlreadyUsed(ctx context.Context, err error) *gqlerror.Error {
|
||||
return &gqlerror.Error{
|
||||
Message: err.Error(),
|
||||
Path: graphql.GetPath(ctx),
|
||||
Extensions: map[string]any{"code": "TOKEN_ALREADY_USED"},
|
||||
}
|
||||
}
|
||||
|
||||
func Invalid(ctx context.Context, err error) *gqlerror.Error {
|
||||
var details map[string]any
|
||||
|
||||
|
||||
Reference in New Issue
Block a user