feat: login service and implement database storage
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package response
|
||||
|
||||
import "net/http"
|
||||
|
||||
type ErrorCode struct {
|
||||
Code string
|
||||
Message string
|
||||
HttpCode int
|
||||
}
|
||||
|
||||
var (
|
||||
// 4xx
|
||||
ErrBadRequest = ErrorCode{Code: "BAD_REQUEST", Message: "BAD_REQUEST", HttpCode: http.StatusBadRequest}
|
||||
ErrDBRequest = ErrorCode{Code: "BAD_DB_REQUEST", Message: "DB_ERROR", HttpCode: http.StatusBadRequest}
|
||||
)
|
||||
@@ -0,0 +1,23 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func ResponseWithErrorCode(ctx context.Context, w http.ResponseWriter,
|
||||
err error, code string, statusCode int, message string) {
|
||||
setDefaultHeaders(ctx, w.Header())
|
||||
|
||||
w.WriteHeader(statusCode)
|
||||
|
||||
b, _ := json.Marshal(ErrorResponse{
|
||||
Error: ErrorResponseData{
|
||||
Code: code,
|
||||
Message: message,
|
||||
},
|
||||
})
|
||||
|
||||
w.Write(b)
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ErrorResponseData struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type ErrorResponse struct {
|
||||
Error ErrorResponseData `json:"error"`
|
||||
}
|
||||
|
||||
func setDefaultHeaders(ctx context.Context, headers http.Header) {
|
||||
headers.Set("Content-Type", "application/json")
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Response[T any] struct {
|
||||
Data T `json:"data"`
|
||||
}
|
||||
|
||||
func RespondJsonSuccess[T any](ctx context.Context, w http.ResponseWriter, data T) {
|
||||
setDefaultHeaders(ctx, w.Header())
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
b, _ := json.Marshal(Response[T]{
|
||||
Data: data,
|
||||
})
|
||||
|
||||
w.Write(b)
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func UnmarshalBody(r *http.Request, v any) error {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -28,11 +28,11 @@ func GenerateToken(options ...ClaimOption) (string, error) {
|
||||
return token.SignedString(jwtSecret)
|
||||
}
|
||||
|
||||
func GenerateToken2(username string) (string, error) {
|
||||
func GenerateToken2(email string) (string, error) {
|
||||
now := timeutils.Now()
|
||||
token := jwt.New(jwt.SigningMethodES256)
|
||||
token := jwt.New(jwt.SigningMethodHS256)
|
||||
claims := token.Claims.(jwt.MapClaims)
|
||||
claims["username"] = username
|
||||
claims["email"] = email
|
||||
claims["exp"] = now.Add(time.Hour).Unix()
|
||||
|
||||
return token.SignedString(jwtSecret)
|
||||
|
||||
Reference in New Issue
Block a user