Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reset password api #37

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions account/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func Create(rollNo string, hashedPasssword string, name string) error {
return nil
}

func UpdatePassword(rollNo string, hashedPasssword string) error {
_, err := database.DB.Exec("UPDATE ACCOUNT SET password = $1 WHERE rollNo = $2", hashedPasssword, rollNo)
return err
}

func UserExists(rollNo string) (bool, error) {
row := database.DB.QueryRow("SELECT rollNo FROM ACCOUNT WHERE rollNo=$1", rollNo)
scannedRow := ""
Expand Down
41 changes: 41 additions & 0 deletions auth/resetPass.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package auth

import (
"net/http"

"github.com/bhuvansingla/iitk-coin/account"
"github.com/bhuvansingla/iitk-coin/errors"
"github.com/bhuvansingla/iitk-coin/util"
)

func ResetPassword(rollNo string, newPassword string, otp string) error {

userExists, err := account.UserExists(rollNo)
if err != nil {
return errors.NewHTTPError(err, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}

if !userExists {
return errors.NewHTTPError(nil, http.StatusBadRequest, "account doesnot exists")
}

if err := account.ValidatePassword(newPassword); err != nil {
return err
}

if err := VerifyOTP(rollNo, otp); err != nil {
return err
}

hashedPwd, err := util.HashAndSalt(newPassword)
if err != nil {
return errors.NewHTTPError(err, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}

err = account.UpdatePassword(rollNo, hashedPwd)
if err != nil {
return errors.NewHTTPError(err, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}

return nil
}
36 changes: 36 additions & 0 deletions handlers/resetPass.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package handlers

import (
"encoding/json"
"net/http"

"github.com/bhuvansingla/iitk-coin/auth"
"github.com/bhuvansingla/iitk-coin/errors"
)

type ResetPasswordRequest struct {
RollNo string `json:"rollNo"`
NewPassword string `json:"newPassword"`
Otp string `json:"otp"`
}

func ResetPassword(w http.ResponseWriter, r *http.Request) error {

if r.Method != "POST" {
return errors.NewHTTPError(nil, http.StatusMethodNotAllowed, http.StatusText(http.StatusMethodNotAllowed))
}

var resetPasswordRequest ResetPasswordRequest

if err := json.NewDecoder(r.Body).Decode(&resetPasswordRequest); err != nil {
return errors.NewHTTPError(err, http.StatusBadRequest, "error decoding request body")
}

err := auth.ResetPassword(resetPasswordRequest.RollNo, resetPasswordRequest.NewPassword, resetPasswordRequest.Otp)

if err != nil {
return err
}

return nil
}
1 change: 1 addition & 0 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func setRoutes() {

http.HandleFunc("/auth/login", CORS(errors.Handler(handlers.Login)))
http.HandleFunc("/auth/signup", CORS(errors.Handler(handlers.Signup)))
http.HandleFunc("/auth/reset-password", CORS(errors.Handler(handlers.ResetPassword)))
http.HandleFunc("/auth/check", CORS(auth.IsAuthorized((errors.Handler(handlers.CheckLogin)))))
http.HandleFunc("/auth/otp", CORS(errors.Handler(handlers.GenerateOtp)))
http.HandleFunc("/auth/logout", CORS(errors.Handler(handlers.Logout)))
Expand Down