Skip to content

Login - #3

Open
hamid-JTG wants to merge 13 commits into
sign-upfrom
login
Open

Login#3
hamid-JTG wants to merge 13 commits into
sign-upfrom
login

Conversation

@hamid-JTG

Copy link
Copy Markdown
Owner

No description provided.

Comment thread controllers/auth.go Outdated
Comment on lines +44 to +62
user, err := services.VerifyUserCredentials(req.Email, req.Password)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": constants.ErrInvalidCredentials})
return
}

token, expiryTime, err := utils.GenerateJWT(user.ID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": constants.ErrFailedToGenerateToken})
return
}

c.SetCookie("token", token, int(expiryTime-time.Now().Unix()), "/", "localhost", false, true)

userResponse := serializers.SerializeLoginResponse(*user)
tokenResponse := serializers.SerializeToken(token, expiryTime)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this all will be part of service

Comment thread controllers/auth.go Outdated
Comment on lines +61 to +65
"user_details": userResponse,
"token": tokenResponse,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create a serializer for it

Comment thread controllers/auth.go Outdated
return
}

c.SetCookie("token", token, int(expiryTime-time.Now().Unix()), "/", "localhost", false, true)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this?

Comment thread serializers/login.go

type LoginRequest struct {
Email string `json:"email" binding:"required,email"`
Password string `json:"password" binding:"required,min=6"`

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you only making len 6 validation. you can also add a custom validator and make validator for a special character and max len too

Comment thread services/authServices.go Outdated
func VerifyUserCredentials(email, password string) (*models.User, error) {
var user models.User
if err := db.DB.Where("email = ?", email).First(&user).Error; err != nil {
if err.Error() == "record not found" {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gorm also give error message constant for record not found use that here

Comment thread services/authServices.go Outdated
var user models.User
if err := db.DB.Where("email = ?", email).First(&user).Error; err != nil {
if err.Error() == "record not found" {
return nil, errors.New("user not found")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create a constant for this check below too

Comment thread utils/utils.go
return signedToken, expiryTime, nil
}

func ValidateJWT(tokenString string) (uint, error) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be part of middleware

Comment thread controllers/auth.go Outdated
c.JSON(http.StatusCreated, response)
}

func Login(c *gin.Context) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create method instead of functions check for service too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants