add signup resolver
This commit is contained in:
53
server/db/db.go
Normal file
53
server/db/db.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/yauthdev/yauth/server/constants"
|
||||
"github.com/yauthdev/yauth/server/enum"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
type Manager interface {
|
||||
AddUser(user User) (User, error)
|
||||
GetUsers() ([]User, error)
|
||||
GetUserByEmail(email string) (User, error)
|
||||
AddVerification(verification Verification) (Verification, error)
|
||||
}
|
||||
|
||||
type manager struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
var Mgr Manager
|
||||
|
||||
func init() {
|
||||
var db *gorm.DB
|
||||
var err error
|
||||
ormConfig := &gorm.Config{
|
||||
NamingStrategy: schema.NamingStrategy{
|
||||
TablePrefix: "yauth_",
|
||||
},
|
||||
}
|
||||
if constants.DB_TYPE == enum.Postgres {
|
||||
db, err = gorm.Open(postgres.Open(constants.DB_URL), ormConfig)
|
||||
}
|
||||
if constants.DB_TYPE == enum.Mysql {
|
||||
db, err = gorm.Open(mysql.Open(constants.DB_URL), ormConfig)
|
||||
}
|
||||
if constants.DB_TYPE == enum.Sqlite {
|
||||
db, err = gorm.Open(sqlite.Open(constants.DB_URL), ormConfig)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Fatal("Failed to init db:", err)
|
||||
} else {
|
||||
db.AutoMigrate(&User{}, &Verification{})
|
||||
}
|
||||
|
||||
Mgr = &manager{db: db}
|
||||
}
|
62
server/db/user.go
Normal file
62
server/db/user.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
FirstName string
|
||||
LastName string
|
||||
Email string `gorm:"unique"`
|
||||
Password string
|
||||
SignupMethod string
|
||||
EmailVerifiedAt int64
|
||||
CreatedAt int64 `gorm:"autoCreateTime"`
|
||||
UpdatedAt int64 `gorm:"autoUpdateTime"`
|
||||
}
|
||||
|
||||
func (user *User) BeforeSave(tx *gorm.DB) error {
|
||||
// Modify current operation through tx.Statement, e.g:
|
||||
if pw, err := bcrypt.GenerateFromPassword([]byte(user.Password), 0); err == nil {
|
||||
tx.Statement.SetColumn("Password", pw)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddUser function to add user
|
||||
func (mgr *manager) AddUser(user User) (User, error) {
|
||||
result := mgr.db.Clauses(clause.OnConflict{DoNothing: true}).Create(&user)
|
||||
if result.Error != nil {
|
||||
log.Println(result.Error)
|
||||
return user, result.Error
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// GetUsers function to get all users
|
||||
func (mgr *manager) GetUsers() ([]User, error) {
|
||||
var users []User
|
||||
result := mgr.db.Find(&users)
|
||||
if result.Error != nil {
|
||||
log.Println(result.Error)
|
||||
return users, result.Error
|
||||
}
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (mgr *manager) GetUserByEmail(email string) (User, error) {
|
||||
var user User
|
||||
result := mgr.db.Where("email = ?", email).First(&user)
|
||||
|
||||
if result.Error != nil {
|
||||
return user, result.Error
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
30
server/db/verification.go
Normal file
30
server/db/verification.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type Verification struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Token string
|
||||
Identifier string
|
||||
ExpiresAt int64
|
||||
CreatedAt int64 `gorm:"autoCreateTime"`
|
||||
UpdatedAt int64 `gorm:"autoUpdateTime"`
|
||||
Email string `gorm:"unique"`
|
||||
}
|
||||
|
||||
// AddVerification function to add verification record
|
||||
func (mgr *manager) AddVerification(verification Verification) (Verification, error) {
|
||||
result := mgr.db.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "email"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{"token", "identifier", "expires_at"}),
|
||||
}).Create(&verification)
|
||||
if result.Error != nil {
|
||||
log.Println(`Error saving verification record`, result.Error)
|
||||
return verification, result.Error
|
||||
}
|
||||
return verification, nil
|
||||
}
|
Reference in New Issue
Block a user