Added JSON ERROR
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -27,21 +26,37 @@ type Claims struct {
|
|||||||
|
|
||||||
func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
log.Fatal("Not post method")
|
JSONError{
|
||||||
|
Status: Error,
|
||||||
|
Message: "Method is not allowed",
|
||||||
|
}.Respond(w, http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Body == nil {
|
if r.Body == nil {
|
||||||
log.Fatal("No body")
|
JSONError{
|
||||||
|
Status: Error,
|
||||||
|
Message: "No body has been provided.",
|
||||||
|
}.Respond(w, http.StatusNoContent)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
JSONError{
|
||||||
|
Status: Error,
|
||||||
|
Message: err.Error(),
|
||||||
|
}.Respond(w, http.StatusNoContent)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
var login LoginInformation
|
var login LoginInformation
|
||||||
err = json.Unmarshal(body, &login)
|
err = json.Unmarshal(body, &login)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
JSONError{
|
||||||
|
Status: Error,
|
||||||
|
Message: err.Error(),
|
||||||
|
}.Respond(w, http.StatusNoContent)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var user User
|
var user User
|
||||||
@@ -52,7 +67,11 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|||||||
Scan(context.Background())
|
Scan(context.Background())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
JSONError{
|
||||||
|
Status: Error,
|
||||||
|
Message: err.Error(),
|
||||||
|
}.Respond(w, http.StatusNoContent)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
claims := Claims{
|
claims := Claims{
|
||||||
@@ -68,10 +87,18 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
signed, err := token.SignedString([]byte("hello"))
|
signed, err := token.SignedString([]byte("hello"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
JSONError{
|
||||||
|
Status: Error,
|
||||||
|
Message: err.Error(),
|
||||||
|
}.Respond(w, http.StatusNoContent)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(signed)
|
JSONSuccess{
|
||||||
|
Status: Success,
|
||||||
|
Message: "JWT Created",
|
||||||
|
Data: map[string]string{"jwt": signed},
|
||||||
|
}.Respond(w, http.StatusCreated)
|
||||||
}
|
}
|
||||||
|
|
||||||
func AuthJWT(next http.Handler) http.Handler {
|
func AuthJWT(next http.Handler) http.Handler {
|
||||||
|
|||||||
@@ -32,14 +32,6 @@ func (dsn *DSN) ToString() string {
|
|||||||
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", dsn.User, dsn.Password, dsn.Hostname, dsn.Port, dsn.DBName)
|
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", dsn.User, dsn.Password, dsn.Hostname, dsn.Port, dsn.DBName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func er(w http.ResponseWriter, r *http.Request) {
|
|
||||||
_ = r
|
|
||||||
JSONError{
|
|
||||||
Status: Error,
|
|
||||||
Message: "Nope",
|
|
||||||
}.Respond(w, http.StatusUnauthorized)
|
|
||||||
}
|
|
||||||
|
|
||||||
func handlerCreateUser(w http.ResponseWriter, r *http.Request) {
|
func handlerCreateUser(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
@@ -91,7 +83,6 @@ func main() {
|
|||||||
"/": {handler, nil},
|
"/": {handler, nil},
|
||||||
"/users/login": {HandleLogin, nil},
|
"/users/login": {HandleLogin, nil},
|
||||||
"/users/new": {handlerCreateUser, AuthJWT},
|
"/users/new": {handlerCreateUser, AuthJWT},
|
||||||
"/error": {er, nil},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Printf("Serving on port %s\n", port)
|
fmt.Printf("Serving on port %s\n", port)
|
||||||
|
|||||||
Reference in New Issue
Block a user