diff --git a/user.go b/user.go index 4b202ef..1a4ff66 100644 --- a/user.go +++ b/user.go @@ -245,3 +245,10 @@ func (user User) ResetPassword(password string) error { config.PassworHash = string(bytes) return user.SetConfig(config) } + +func (user User) VerifyPassword(password string) bool { + err := bcrypt.CompareHashAndPassword( + []byte(user.Config().PassworHash), []byte(password), + ) + return err == nil +} diff --git a/user_test.go b/user_test.go index d1ca83c..1292acc 100644 --- a/user_test.go +++ b/user_test.go @@ -302,3 +302,15 @@ func TestResetUserPassword(t *testing.T) { assertions.Assert(t, user.Config().PassworHash != "", "Password Hash should not be empty") assertions.Assert(t, user.Config().PassworHash != "test", "Password Hash should not be test") } + +func TestVerifyPassword(t *testing.T) { + user := getTestUser() + user.ResetPassword("test") + assertions.Assert(t, user.VerifyPassword("test"), "Password should be correct") + assertions.Assert(t, !user.VerifyPassword("test2"), "Password should be incorrect") + assertions.Assert(t, !user.VerifyPassword(""), "Password should be incorrect") + assertions.Assert(t, !user.VerifyPassword("Test"), "Password should be incorrect") + assertions.Assert(t, !user.VerifyPassword("TEST"), "Password should be incorrect") + assertions.Assert(t, !user.VerifyPassword("0000000"), "Password should be incorrect") + +}