-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5cd05c1
commit afbca50
Showing
16 changed files
with
495 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,10 @@ | ||
package admin | ||
|
||
import ( | ||
"flappy-bird-server/lib" | ||
"net/http" | ||
"github.com/gorilla/mux" | ||
) | ||
|
||
func Handler(w http.ResponseWriter, r *http.Request) { | ||
if r.Method == http.MethodGet { | ||
GetMetrics(w, r) | ||
return | ||
} | ||
if r.Method == http.MethodPost { | ||
UpdateUnderMaintenance(w, r) | ||
return | ||
} | ||
lib.ErrorJson(w, 405, "Method not allowed", "") | ||
func Handler(r *mux.Router) { | ||
r.HandleFunc("/metric", GetMetrics).Methods("GET") | ||
r.HandleFunc("/maintenance", UpdateUnderMaintenance).Methods("GET") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,10 @@ | ||
package auth | ||
|
||
import ( | ||
"flappy-bird-server/lib" | ||
"net/http" | ||
"github.com/gorilla/mux" | ||
) | ||
|
||
func Handler(w http.ResponseWriter, r *http.Request) { | ||
if r.Method == http.MethodPost { | ||
authenticate(w, r) | ||
return | ||
} | ||
lib.ErrorJson(w, 405, "Method not allowed", "") | ||
func Handler(r *mux.Router) { | ||
r.HandleFunc("/register", register).Methods("POST") | ||
r.HandleFunc("/login", login).Methods("POST") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package auth | ||
|
||
import ( | ||
gameManager "flappy-bird-server/game-manager" | ||
"flappy-bird-server/lib" | ||
"flappy-bird-server/model" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type LoginRequestBody struct { | ||
Identifier string `json:"identifier"` | ||
Password string `json:"password"` | ||
} | ||
|
||
func login(w http.ResponseWriter, r *http.Request) { | ||
log.Println("Login") | ||
var body LoginRequestBody | ||
err := lib.ReadJsonFromBody(r, w, &body) | ||
if err != nil { | ||
lib.ErrorJson(w, http.StatusBadRequest, err.Error(), "") | ||
return | ||
} | ||
if len(body.Password) < 7 { | ||
lib.ErrorJson(w, http.StatusBadRequest, "password length should be more than 7", "") | ||
return | ||
} | ||
if len(body.Password) > 15 { | ||
lib.ErrorJson(w, http.StatusBadRequest, "password length should be less then 16", "") | ||
return | ||
} | ||
|
||
var user model.User | ||
var passwordHash string | ||
|
||
getUserDetailsQuery := `SELECT id, name, email, "inrBalance", "solanaBalance", password FROM public.users WHERE email = $1` | ||
err = lib.Pool.QueryRow(r.Context(), getUserDetailsQuery, body.Identifier).Scan(&user.Id, &user.Name, &user.Email, &user.INRBalance, &user.SolanaBalance, &passwordHash) | ||
if err != nil { | ||
lib.ErrorJson(w, http.StatusBadRequest, err.Error(), "") | ||
return | ||
} | ||
currentPasswordHash := lib.HashString(body.Password) | ||
token, err := lib.GenerateToken(user.Id) | ||
if err != nil { | ||
lib.ErrorJson(w, http.StatusBadRequest, err.Error(), "") | ||
return | ||
} | ||
|
||
if passwordHash != currentPasswordHash { | ||
lib.ErrorJson(w, http.StatusBadRequest, "invalid password", "") | ||
return | ||
} | ||
|
||
gameManager.GetInstance().RedisClient.Set(r.Context(), fmt.Sprintf("mr-balance-%s", user.Id), user.SolanaBalance, 24*time.Hour) | ||
|
||
lib.WriteJson(w, http.StatusOK, map[string]interface{}{ | ||
"message": "Login successfully", | ||
"token": token, | ||
"data": user, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.