JWT proto with login & registration
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID bson.ObjectID `json:"id" bson:"_id"`
|
||||
Username string `json:"username" bson:"username"`
|
||||
Email string `json:"email" bson:"email"`
|
||||
PasswordHash string `json:"-" bson:"password_hash"`
|
||||
CreatedAt time.Time `json:"created_at" bson:"created_at"`
|
||||
}
|
||||
|
||||
type RegisterRequest struct {
|
||||
Username string `json:"username" binding:"required,min=3,max=30" example:"john"`
|
||||
Email string `json:"email" binding:"required,email" example:"john@example.com"`
|
||||
Password string `json:"password" binding:"required,min=6" example:"secret123"`
|
||||
}
|
||||
|
||||
type LoginRequest struct {
|
||||
Email string `json:"email" binding:"required,email" example:"john@example.com"`
|
||||
Password string `json:"password" binding:"required" example:"secret123"`
|
||||
}
|
||||
|
||||
type AuthResponse struct {
|
||||
Token string `json:"token" example:"eyJhbGciOiJIUzI1NiIs..."`
|
||||
User UserPublic `json:"user"`
|
||||
}
|
||||
|
||||
type UserPublic struct {
|
||||
ID bson.ObjectID `json:"id" bson:"_id"`
|
||||
Username string `json:"username" bson:"username"`
|
||||
Email string `json:"email" bson:"email"`
|
||||
CreatedAt time.Time `json:"created_at" bson:"created_at"`
|
||||
}
|
||||
|
||||
func NewUserPublic(u *User) UserPublic {
|
||||
return UserPublic{
|
||||
ID: u.ID,
|
||||
Username: u.Username,
|
||||
Email: u.Email,
|
||||
CreatedAt: u.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
type UserResponse struct {
|
||||
User UserPublic `json:"user"`
|
||||
}
|
||||
|
||||
type ErrorResponse struct {
|
||||
Error string `json:"error" example:"invalid email or password"`
|
||||
}
|
||||
Reference in New Issue
Block a user