This commit is contained in:
Lakhan Samani 2021-07-17 22:39:50 +05:30
parent 9b8658f666
commit 245a5b5e1b
4 changed files with 18 additions and 22 deletions

View File

@ -3,8 +3,6 @@ package db
import (
"log"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
@ -21,17 +19,6 @@ type User struct {
Image string
}
func (user *User) BeforeSave(tx *gorm.DB) error {
// Modify current operation through tx.Statement, e.g:
if user.Password != "" {
if pw, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost); err == nil {
tx.Statement.SetColumn("Password", string(pw))
}
}
return nil
}
// SaveUser function to add user
func (mgr *manager) SaveUser(user User) (User, error) {
result := mgr.db.Clauses(clause.OnConflict{UpdateAll: true, Columns: []clause.Column{{Name: "email"}}}).Create(&user)
@ -40,8 +27,6 @@ func (mgr *manager) SaveUser(user User) (User, error) {
log.Println(result.Error)
return user, result.Error
}
log.Println("===== USER ID =====")
log.Println(user.ID)
return user, nil
}

View File

@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
@ -73,16 +72,15 @@ func processGoogleUserInfo(state string, code string, c *gin.Context) error {
Email: userRawData["email"],
EmailVerifiedAt: time.Now().Unix(),
SignupMethod: signupMethod,
Password: existingUser.Password,
}
user, _ = db.Mgr.SaveUser(user)
}
log.Println("====== USER FROM OAUTH HANDLER =====")
log.Println(user.ID)
userIdStr := fmt.Sprintf("%d", user.ID)
log.Println("str id: ", userIdStr)
refreshToken, _, _ := utils.CreateAuthToken(utils.UserAuthInfo{
ID: userIdStr,
Email: user.Email,
@ -98,7 +96,6 @@ func processGoogleUserInfo(state string, code string, c *gin.Context) error {
}
func HandleOAuthCallback(provider enum.OAuthProvider) gin.HandlerFunc {
log.Println("here...")
return func(c *gin.Context) {
if provider == enum.GoogleProvider {
err := processGoogleUserInfo(c.Request.FormValue("state"), c.Request.FormValue("code"), c)

View File

@ -37,9 +37,11 @@ func Signup(ctx context.Context, params model.SignUpInput) (*model.SignUpRespons
}
user := db.User{
Email: params.Email,
Password: params.Password,
}
password, _ := utils.HashPassword(params.Password)
user.Password = password
if params.FirstName != nil {
user.FirstName = *params.FirstName
}

View File

@ -0,0 +1,12 @@
package utils
import "golang.org/x/crypto/bcrypt"
func HashPassword(password string) (string, error) {
pw, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return "", err
}
return string(pw), nil
}