Skip to content

Commit

Permalink
fix: error handling based on #1
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhantloya committed Sep 20, 2022
1 parent 801fca1 commit c061141
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
41 changes: 23 additions & 18 deletions server/controllers/authController.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"fmt"
"log"

"github.com/formatkaka/youtube-clone/db"
Expand All @@ -13,44 +14,45 @@ func SignUp(ctx *gin.Context) {
var req models.SignUpRequest

if err := ctx.BindJSON(&req); err != nil {
log.Fatal("bind issue signup request obj")
log.Println("bind issue signup request obj : ", err)
ctx.JSON(500, gin.H{
"error": "bind issue signup request obj",
"error": fmt.Sprintf("bind issue signup request obj : %s", err.Error()),
})
return
}

hash, err := helpers.GenerateHash(req.Password)

if err != nil {
log.Println(err)
log.Println("generate hash error in signup func ", err)
ctx.JSON(200, gin.H{
"error": err,
"error": fmt.Sprintf("generate hash error in signup func %s ", err),
})
return
}

log.Println("hash : ", hash)

client := db.GetClient()
var id int64
query := "INSERT INTO USERS (name, email, password) VALUES ($1, $2, $3) RETURNING id"
err = client.QueryRow(query, req.Name, req.Email, hash).Scan(&id)

if err != nil {
log.Println(err)
// how to return error that email already exists in db ??
// should I check if email exists and then put or try to analyse the error message
log.Println("unable to insert ", err)
ctx.JSON(500, gin.H{
"err": err,
"error": fmt.Sprintf("unable to insert %s ", err),
})
return
}

token, refreshToken, err := helpers.GenerateToken(req.Email, req.Name, id)

if err != nil {
log.Println(err)
log.Println("signup request : ", err)
ctx.JSON(500, gin.H{
"err": err,
"error": fmt.Sprintf("signup request : %s", err),
"status": helpers.TOKEN_ERROR,
})
return
}
Expand All @@ -66,9 +68,9 @@ func Login(ctx *gin.Context) {
var req models.LoginRequest

if err := ctx.BindJSON(&req); err != nil {
log.Fatal("bind issue signup request obj")
log.Println("bind issue login request obj : ", err)
ctx.JSON(500, gin.H{
"error": "bind issue signup request obj",
"error": fmt.Sprintf("bind issue login request obj : %s", err.Error()),
})
return
}
Expand All @@ -89,11 +91,12 @@ func Login(ctx *gin.Context) {

if rows.Next() {
if err := rows.Scan(&id, &name, &hash); err != nil {
panic(err)
log.Println("ERROR: login rows Scan : ", err)
}
} else {
ctx.JSON(500, gin.H{
"error": "Email does not exist",
"error": "Email does not exist",
"status": helpers.EMAIL_NOT_EXIST,
})
return
}
Expand All @@ -102,9 +105,10 @@ func Login(ctx *gin.Context) {
token, refreshToken, err := helpers.GenerateToken(req.Email, name, id)

if err != nil {
log.Println(err)
log.Println("login func : ", err)
ctx.JSON(500, gin.H{
"err": err,
"error": fmt.Sprintf("login function token : %s", err.Error()),
"status": helpers.TOKEN_ERROR,
})
return
}
Expand All @@ -119,8 +123,9 @@ func Login(ctx *gin.Context) {
return
}

ctx.JSON(500, gin.H{
"error": "Invalid email or password",
ctx.JSON(200, gin.H{
"error": "Invalid email or password",
"errorcode": helpers.INVALID_LOGIN_CREDS,
})

}
2 changes: 2 additions & 0 deletions server/db/dbConn.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func Init() {
client, err = sql.Open("postgres", url)

if err != nil {
// unable to connect to DB
// critical error
log.Fatal(err)
}

Expand Down
5 changes: 5 additions & 0 deletions server/helpers/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package helpers

const INVALID_LOGIN_CREDS = "INVALID_LOGIN_CREDS"
const EMAIL_NOT_EXIST = "EMAIL_NOT_EXIST"
const TOKEN_ERROR = "TOKEN_ERROR"
5 changes: 3 additions & 2 deletions server/helpers/tokenHelper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package helpers

import (
"fmt"
"log"
"os"
"time"
Expand Down Expand Up @@ -37,14 +38,14 @@ func GenerateToken(email string, name string, uid int64) (string, string, error)

if err != nil {
log.Println(err)
return "", "", err
return "", "", fmt.Errorf("token generation error in GenerateToken : %w", err)
}

refreshToken, err := jwt.NewWithClaims(jwt.SigningMethodHS256, refreshClaims).SignedString([]byte(SECRET_KEY))

if err != nil {
log.Println(err)
return "", "", err
return "", "", fmt.Errorf("refresh token generation error in GenerateToken : %w", err)
}

return token, refreshToken, nil
Expand Down

0 comments on commit c061141

Please sign in to comment.