我正在調整這篇博文中的登錄功能。User 結構體(見下文)有四個字段,id、name、email 和 password。您可以在下面的數據庫中看到一行。將fmt.Println在登錄功能向用戶顯示是這樣的數據庫查詢后 &{3 testuser $2a$10$hS7sth8jIBN2/IXFTWBibu3Ko5BXm9zHO5AJZRAbAOQ04uv.Gs5Ym [116 101 115 116 117 115 101 114 64 103 109 97 105 108 46 99 111 109]}換句話說,它有id(3)、name(testuser)、散列密碼,但還有一組數字,這讓我有點驚訝,因為它不在數據庫的行中(見下文)。您還會注意到fmt.Println沒有顯示電子郵件,即使它在數據庫的行中可見,所以這里似乎有問題。當 bcrypt 比較Login函數中的哈希值和密碼時,它給了我這個錯誤hashedSecret too short to be a bcrypted password not auth你能解釋為什么會拋出這個錯誤嗎?func Login(password, email string) (u *User, err error) { u = &User{} err = db.QueryRow("select * from users where email=$1 ", email).Scan(&u.Id, &u.Name, &u.Password, &u.Email) fmt.Println("u", u) if err != nil { fmt.Println("err", err) } err = bcrypt.CompareHashAndPassword(u.Password, []byte(password)) if err != nil { u = nil } return}我有一個包含以下字段的用戶結構type User struct { Id int Name string Email string Password []byte}我像這樣在 postgres 中為它創建了一個表CREATE TABLE "public"."users" ( "id" int4 NOT NULL DEFAULT nextval('users_id_seq'::regclass), "username" varchar(255) NOT NULL COLLATE "default", "email" varchar(255) NOT NULL COLLATE "default", "password" bytea )WITH (OIDS=FALSE);這是數據庫中的一行id | username | email | password ----+------------+----------------------+---------------------------------------------------------------------------------------------------------------------------- 3 | testuser | [email protected] | \x24326124313024685337737468386a49424e322f495846545742696275334b6f3542586d397a484f35414a5a524162414f51303475762e477335596d
- 1 回答
- 0 關注
- 756 瀏覽
添加回答
舉報
0/150
提交
取消