Hashing password
Using postgres' pgcrypt
This commit is contained in:
@@ -36,8 +36,8 @@ const (
|
||||
type Status string
|
||||
|
||||
const (
|
||||
Active Status = "Active"
|
||||
Inactive Status = "Inactive"
|
||||
Active Status = "Active"
|
||||
Inactive Status = "Inactive"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
@@ -56,6 +56,43 @@ type User struct {
|
||||
Articles []*Blog `bun:"rel:has-many,join:user_id=blog_id" json:"articles,omitempty"`
|
||||
}
|
||||
|
||||
func (u *User) Insert(ctx context.Context) (sql.Result, error) {
|
||||
u.Password = fmt.Sprintf("crypt('%s', gen_salt('bf'))", u.Password)
|
||||
return DB.NewInsert().
|
||||
Model(u).
|
||||
Value("password", u.Password).
|
||||
Exec(ctx)
|
||||
}
|
||||
|
||||
func Verify(ctx context.Context, email, password string) (*User, error) {
|
||||
// var user User
|
||||
// query := `
|
||||
// SELECT *
|
||||
// FROM users
|
||||
// WHERE email = ? AND password = crypt(?, password)
|
||||
// `
|
||||
//
|
||||
// err := DB.NewRaw(query, email, password).Scan(ctx, user)
|
||||
|
||||
var user User
|
||||
count, err := DB.NewSelect().
|
||||
Model(&user).
|
||||
Where("email = ? AND password = crypt(?, password)", email, password).
|
||||
Limit(1).
|
||||
ScanAndCount(context.Background())
|
||||
if count == 0 {
|
||||
return nil, fmt.Errorf("invalid email or password")
|
||||
}
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("invalid email or password")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
type Event struct {
|
||||
bun.BaseModel `bun:"table:events"`
|
||||
|
||||
@@ -69,7 +106,7 @@ type Event struct {
|
||||
type EventToUser struct {
|
||||
bun.BaseModel `bun:"table:events_to_users"`
|
||||
|
||||
EventID uuid.UUID `bun:"type:uuid,pk"`
|
||||
EventID uuid.UUID `bun:"type:uuid,pk"`
|
||||
UserID uuid.UUID `bun:"type:uuid,pk"`
|
||||
|
||||
Event *Event `bun:"rel:belongs-to,join:event_id=event_id"`
|
||||
@@ -103,12 +140,18 @@ func InitDatabase(dsn DSN) (*bun.DB, error) {
|
||||
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn.ToString())))
|
||||
db := bun.NewDB(sqldb, pgdialect.New())
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := db.ExecContext(ctx, "CREATE EXTENSION IF NOT EXISTS pgcrypto;")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db.RegisterModel((*EventToUser)(nil))
|
||||
_, err := db.NewCreateTable().Model((*User)(nil)).IfNotExists().Exec(context.Background())
|
||||
_, err = db.NewCreateTable().Model((*Event)(nil)).IfNotExists().Exec(context.Background())
|
||||
_, err = db.NewCreateTable().Model((*EventToUser)(nil)).IfNotExists().Exec(context.Background())
|
||||
_, err = db.NewCreateTable().Model((*Blog)(nil)).IfNotExists().Exec(context.Background())
|
||||
_, err = db.NewCreateTable().Model((*WebsiteSettings)(nil)).IfNotExists().Exec(context.Background())
|
||||
_, err = db.NewCreateTable().Model((*User)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Event)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*EventToUser)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Blog)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*WebsiteSettings)(nil)).IfNotExists().Exec(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user