This commit is contained in:
cdricms
2025-01-15 11:54:39 +01:00
parent d18245736f
commit 683a8c3133
13 changed files with 165 additions and 69 deletions

72
backend/auth.go Normal file
View File

@@ -0,0 +1,72 @@
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
"github.com/golang-jwt/jwt/v5"
)
type LoginInformation struct {
Email string `json:"email"`
Password string `json:"password"`
}
type Claims struct {
UserID string `json:"user_id"`
jwt.RegisteredClaims
}
func HandleLogin(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
log.Fatal("Not post method")
}
if r.Body == nil {
log.Fatal("No body")
}
body, err := io.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}
var login LoginInformation
err = json.Unmarshal(body, &login)
if err != nil {
log.Fatal(err)
}
var user User
err = DB.NewSelect().
Model(&user).
Where("email = ? AND password = ?", login.Email, login.Password).
Limit(1).
Scan(context.Background())
if err != nil {
log.Fatal(err)
}
claims := Claims{
UserID: user.UserID.String(),
RegisteredClaims: jwt.RegisteredClaims{
Issuer: "latosa-escrima.fr",
Subject: "authentification",
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour * 24)),
IssuedAt: jwt.NewNumericDate(time.Now()),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
signed, err := token.SignedString([]byte("hello"))
if err != nil {
log.Fatal(err)
}
fmt.Println(signed)
}

View File

@@ -3,6 +3,7 @@ module fr.latosa-escrima
go 1.23.4
require (
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0
github.com/joho/godotenv v1.5.1
github.com/lib/pq v1.10.9

View File

@@ -1,5 +1,9 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=

View File

@@ -43,6 +43,7 @@ func handlerCreateUser(w http.ResponseWriter, r *http.Request) {
LastName: "Doe",
Email: "john.doe@example.com",
Phone: "1234567890",
Password: "1234",
}
_, err := DB.NewInsert().Model(user).Exec(context.Background())
@@ -79,6 +80,7 @@ func main() {
http.HandleFunc("/", handler)
http.HandleFunc("/user/new", handlerCreateUser)
http.HandleFunc("/users/login", HandleLogin)
fmt.Printf("Serving on port %s\n", port)
err = http.ListenAndServe(fmt.Sprintf(":%s", port), nil)

View File

@@ -25,6 +25,7 @@ type User struct {
FirstName string `bun:"firstname,notnull"`
LastName string `bun:"lastname,notnull"`
Email string `bun:"email,unique,notnull"`
Password string `bun:"password,notnull"`
Phone string `bun:"phone,notnull"`
Role Role `bun:"role,notnull,default:'user'"`
CreatedAt time.Time `bun:"created_at,default:current_timestamp"`