first commit
This commit is contained in:
30
apps/gateway/internal/handler/auth/getverifycodehandler.go
Normal file
30
apps/gateway/internal/handler/auth/getverifycodehandler.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"tianyuan-api/apps/gateway/internal/logic/auth"
|
||||
"tianyuan-api/apps/gateway/internal/svc"
|
||||
"tianyuan-api/apps/gateway/internal/types"
|
||||
|
||||
xhttp "github.com/zeromicro/x/http"
|
||||
)
|
||||
|
||||
func GetVerifyCodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.GetVerifyCodeReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := auth.NewGetVerifyCodeLogic(r.Context(), svcCtx)
|
||||
err := l.GetVerifyCode(&req)
|
||||
if err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
} else {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
40
apps/gateway/internal/handler/auth/loginuserhandler.go
Normal file
40
apps/gateway/internal/handler/auth/loginuserhandler.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"tianyuan-api/apps/gateway/internal/logic/auth"
|
||||
"tianyuan-api/apps/gateway/internal/svc"
|
||||
"tianyuan-api/apps/gateway/internal/types"
|
||||
|
||||
xhttp "github.com/zeromicro/x/http"
|
||||
)
|
||||
|
||||
func LoginUserHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.LoginReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := auth.NewLoginUserLogic(r.Context(), svcCtx)
|
||||
token, err := l.LoginUser(&req)
|
||||
if err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
} else {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: "Authorization",
|
||||
Value: token, // JWT 令牌
|
||||
HttpOnly: true, // 防止 JavaScript 访问
|
||||
Secure: false, // HTTPS 使用
|
||||
SameSite: http.SameSiteLaxMode, // 防止 CSRF 攻击
|
||||
Path: "/",
|
||||
Expires: time.Now().Add(time.Duration(svcCtx.Config.AuthJWT.AccessExpire) * time.Second), // 过期时间
|
||||
})
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
23
apps/gateway/internal/handler/auth/logouthandler.go
Normal file
23
apps/gateway/internal/handler/auth/logouthandler.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
xhttp "github.com/zeromicro/x/http"
|
||||
"tianyuan-api/apps/gateway/internal/svc"
|
||||
)
|
||||
|
||||
func LogoutHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// 设置空的JWT Cookie,覆盖之前的JWT
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: "Authorization", // 你的JWT cookie名
|
||||
Value: "", // 清空cookies
|
||||
Path: "/",
|
||||
HttpOnly: true,
|
||||
Expires: time.Unix(0, 0), // 过期时间设置为过去
|
||||
})
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, nil)
|
||||
}
|
||||
}
|
||||
40
apps/gateway/internal/handler/auth/phoneloginuserhandler.go
Normal file
40
apps/gateway/internal/handler/auth/phoneloginuserhandler.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"tianyuan-api/apps/gateway/internal/logic/auth"
|
||||
"tianyuan-api/apps/gateway/internal/svc"
|
||||
"tianyuan-api/apps/gateway/internal/types"
|
||||
|
||||
xhttp "github.com/zeromicro/x/http"
|
||||
)
|
||||
|
||||
func PhoneLoginUserHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.PhoneLoginReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := auth.NewPhoneLoginUserLogic(r.Context(), svcCtx)
|
||||
token, err := l.PhoneLoginUser(&req)
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: "Authorization",
|
||||
Value: token, // JWT 令牌
|
||||
HttpOnly: true, // 防止 JavaScript 访问
|
||||
Secure: false, // HTTPS 使用
|
||||
SameSite: http.SameSiteLaxMode, // 防止 CSRF 攻击
|
||||
Path: "/",
|
||||
Expires: time.Now().Add(time.Duration(svcCtx.Config.AuthJWT.AccessExpire) * time.Second), // 过期时间
|
||||
})
|
||||
if err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
} else {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
30
apps/gateway/internal/handler/auth/registeruserhandler.go
Normal file
30
apps/gateway/internal/handler/auth/registeruserhandler.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"tianyuan-api/apps/gateway/internal/logic/auth"
|
||||
"tianyuan-api/apps/gateway/internal/svc"
|
||||
"tianyuan-api/apps/gateway/internal/types"
|
||||
|
||||
xhttp "github.com/zeromicro/x/http"
|
||||
)
|
||||
|
||||
func RegisterUserHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.RegisterReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := auth.NewRegisterUserLogic(r.Context(), svcCtx)
|
||||
err := l.RegisterUser(&req)
|
||||
if err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
} else {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user