Add auth protection sample and standardize json.
This commit is contained in:
parent
bcb820ea9b
commit
418a30f062
@ -7,6 +7,7 @@ This is to kick start a web project with nextjs as frontend and go server as bac
|
|||||||
```
|
```
|
||||||
starter/
|
starter/
|
||||||
├── .env
|
├── .env
|
||||||
|
├── .env.dev
|
||||||
├── sh/ # Shell scripts for development and deployment automation
|
├── sh/ # Shell scripts for development and deployment automation
|
||||||
│ ├── dev.sh
|
│ ├── dev.sh
|
||||||
│ └── prod.sh
|
│ └── prod.sh
|
||||||
@ -42,3 +43,7 @@ All environmental variables should be managed in a centralized `.env` in the roo
|
|||||||
- API prefix: `/service/`
|
- API prefix: `/service/`
|
||||||
- Next.js 15.3.2 (App Router, Turbopack enabled, Import alias: `@/*`)
|
- Next.js 15.3.2 (App Router, Turbopack enabled, Import alias: `@/*`)
|
||||||
- Logto 1.27.0
|
- Logto 1.27.0
|
||||||
|
|
||||||
|
## Rules
|
||||||
|
|
||||||
|
- Frontend and backend should communicate with standard JSON.
|
||||||
|
|||||||
@ -15,11 +15,11 @@ import (
|
|||||||
// In production, use Redis/MongoDB
|
// In production, use Redis/MongoDB
|
||||||
|
|
||||||
type SessionStorage struct {
|
type SessionStorage struct {
|
||||||
session sessions.Session
|
Session sessions.Session
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SessionStorage) GetItem(key string) string {
|
func (s *SessionStorage) GetItem(key string) string {
|
||||||
value := s.session.Get(key)
|
value := s.Session.Get(key)
|
||||||
if value == nil {
|
if value == nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@ -31,12 +31,12 @@ func (s *SessionStorage) GetItem(key string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SessionStorage) SetItem(key, value string) {
|
func (s *SessionStorage) SetItem(key, value string) {
|
||||||
s.session.Set(key, value)
|
s.Session.Set(key, value)
|
||||||
s.session.Save()
|
s.Session.Save()
|
||||||
}
|
}
|
||||||
|
|
||||||
// getLogtoConfig returns Logto config from environment variables
|
// GetLogtoConfig returns Logto config from environment variables
|
||||||
func getLogtoConfig() *client.LogtoConfig {
|
func GetLogtoConfig() *client.LogtoConfig {
|
||||||
return &client.LogtoConfig{
|
return &client.LogtoConfig{
|
||||||
Endpoint: os.Getenv("LOGTO_ENDPOINT"),
|
Endpoint: os.Getenv("LOGTO_ENDPOINT"),
|
||||||
AppId: os.Getenv("LOGTO_APP_ID"),
|
AppId: os.Getenv("LOGTO_APP_ID"),
|
||||||
@ -48,7 +48,7 @@ func getLogtoConfig() *client.LogtoConfig {
|
|||||||
// Note: The /service/auth endpoint is for debugging purposes only
|
// Note: The /service/auth endpoint is for debugging purposes only
|
||||||
func HomeHandler(ctx *gin.Context) {
|
func HomeHandler(ctx *gin.Context) {
|
||||||
session := sessions.Default(ctx)
|
session := sessions.Default(ctx)
|
||||||
logtoClient := client.NewLogtoClient(getLogtoConfig(), &SessionStorage{session: session})
|
logtoClient := client.NewLogtoClient(GetLogtoConfig(), &SessionStorage{Session: session})
|
||||||
|
|
||||||
var debugInfo string
|
var debugInfo string
|
||||||
debugInfo = "<h1>Logto Auth Debugging Page</h1>"
|
debugInfo = "<h1>Logto Auth Debugging Page</h1>"
|
||||||
@ -141,11 +141,11 @@ func HomeHandler(ctx *gin.Context) {
|
|||||||
// SignInHandler starts the Logto sign-in flow
|
// SignInHandler starts the Logto sign-in flow
|
||||||
func SignInHandler(ctx *gin.Context) {
|
func SignInHandler(ctx *gin.Context) {
|
||||||
session := sessions.Default(ctx)
|
session := sessions.Default(ctx)
|
||||||
logtoClient := client.NewLogtoClient(getLogtoConfig(), &SessionStorage{session: session})
|
logtoClient := client.NewLogtoClient(GetLogtoConfig(), &SessionStorage{Session: session})
|
||||||
redirectUri := os.Getenv("LOGTO_REDIRECT_URI")
|
redirectUri := os.Getenv("LOGTO_REDIRECT_URI")
|
||||||
signInUri, err := logtoClient.SignIn(redirectUri)
|
signInUri, err := logtoClient.SignIn(redirectUri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.String(http.StatusInternalServerError, err.Error())
|
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.Redirect(http.StatusTemporaryRedirect, signInUri)
|
ctx.Redirect(http.StatusTemporaryRedirect, signInUri)
|
||||||
@ -154,10 +154,10 @@ func SignInHandler(ctx *gin.Context) {
|
|||||||
// CallbackHandler handles the Logto sign-in callback
|
// CallbackHandler handles the Logto sign-in callback
|
||||||
func CallbackHandler(ctx *gin.Context) {
|
func CallbackHandler(ctx *gin.Context) {
|
||||||
session := sessions.Default(ctx)
|
session := sessions.Default(ctx)
|
||||||
logtoClient := client.NewLogtoClient(getLogtoConfig(), &SessionStorage{session: session})
|
logtoClient := client.NewLogtoClient(GetLogtoConfig(), &SessionStorage{Session: session})
|
||||||
err := logtoClient.HandleSignInCallback(ctx.Request)
|
err := logtoClient.HandleSignInCallback(ctx.Request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.String(http.StatusInternalServerError, err.Error())
|
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Redirect to the frontend page instead of the backend auth page
|
// Redirect to the frontend page instead of the backend auth page
|
||||||
@ -167,11 +167,11 @@ func CallbackHandler(ctx *gin.Context) {
|
|||||||
// SignOutHandler starts the Logto sign-out flow
|
// SignOutHandler starts the Logto sign-out flow
|
||||||
func SignOutHandler(ctx *gin.Context) {
|
func SignOutHandler(ctx *gin.Context) {
|
||||||
session := sessions.Default(ctx)
|
session := sessions.Default(ctx)
|
||||||
logtoClient := client.NewLogtoClient(getLogtoConfig(), &SessionStorage{session: session})
|
logtoClient := client.NewLogtoClient(GetLogtoConfig(), &SessionStorage{Session: session})
|
||||||
postSignOutRedirectUri := os.Getenv("LOGTO_POST_SIGN_OUT_REDIRECT_URI")
|
postSignOutRedirectUri := os.Getenv("LOGTO_POST_SIGN_OUT_REDIRECT_URI")
|
||||||
signOutUri, err := logtoClient.SignOut(postSignOutRedirectUri)
|
signOutUri, err := logtoClient.SignOut(postSignOutRedirectUri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.String(http.StatusOK, err.Error())
|
ctx.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.Redirect(http.StatusTemporaryRedirect, signOutUri)
|
ctx.Redirect(http.StatusTemporaryRedirect, signOutUri)
|
||||||
@ -180,10 +180,10 @@ func SignOutHandler(ctx *gin.Context) {
|
|||||||
// UserIdTokenClaimsHandler returns the user's ID token claims as JSON
|
// UserIdTokenClaimsHandler returns the user's ID token claims as JSON
|
||||||
func UserIdTokenClaimsHandler(ctx *gin.Context) {
|
func UserIdTokenClaimsHandler(ctx *gin.Context) {
|
||||||
session := sessions.Default(ctx)
|
session := sessions.Default(ctx)
|
||||||
logtoClient := client.NewLogtoClient(getLogtoConfig(), &SessionStorage{session: session})
|
logtoClient := client.NewLogtoClient(GetLogtoConfig(), &SessionStorage{Session: session})
|
||||||
idTokenClaims, err := logtoClient.GetIdTokenClaims()
|
idTokenClaims, err := logtoClient.GetIdTokenClaims()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.String(http.StatusOK, err.Error())
|
ctx.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.JSON(http.StatusOK, idTokenClaims)
|
ctx.JSON(http.StatusOK, idTokenClaims)
|
||||||
|
|||||||
24
backend/internal/middleware/auth.go
Normal file
24
backend/internal/middleware/auth.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"starter/backend/internal/handlers"
|
||||||
|
|
||||||
|
"github.com/gin-contrib/sessions"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/logto-io/go/client"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AuthRequired 是 Logto 认证中间件,未认证用户返回 401
|
||||||
|
func AuthRequired() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
session := sessions.Default(c)
|
||||||
|
logtoClient := client.NewLogtoClient(handlers.GetLogtoConfig(), &handlers.SessionStorage{Session: session})
|
||||||
|
if !logtoClient.IsAuthenticated() {
|
||||||
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -16,7 +16,7 @@ func RegisterRoutes(rg *gin.RouterGroup) {
|
|||||||
rg.Use(middleware.Logger())
|
rg.Use(middleware.Logger())
|
||||||
|
|
||||||
// Define routes
|
// Define routes
|
||||||
rg.GET("/health", handlers.HealthCheck)
|
rg.GET("/health", middleware.AuthRequired(), handlers.HealthCheck)
|
||||||
|
|
||||||
// Logto authentication routes
|
// Logto authentication routes
|
||||||
rg.GET("/auth/", handlers.HomeHandler)
|
rg.GET("/auth/", handlers.HomeHandler)
|
||||||
|
|||||||
@ -16,7 +16,14 @@ export default function HealthStatus() {
|
|||||||
throw new Error(`Server responded with status: ${response.status}`);
|
throw new Error(`Server responded with status: ${response.status}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
let data;
|
||||||
|
try {
|
||||||
|
data = await response.json();
|
||||||
|
} catch (jsonErr) {
|
||||||
|
setError("Response is not valid JSON");
|
||||||
|
setHealth({});
|
||||||
|
return;
|
||||||
|
}
|
||||||
setHealth(data);
|
setHealth(data);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(
|
setError(
|
||||||
|
|||||||
@ -43,9 +43,24 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const userData = await response.json();
|
let userData;
|
||||||
setUser(userData);
|
try {
|
||||||
setIsAuthenticated(true);
|
userData = await response.json();
|
||||||
|
} catch (jsonErr) {
|
||||||
|
// 不是 JSON,降级处理
|
||||||
|
setUser(null);
|
||||||
|
setIsAuthenticated(false);
|
||||||
|
setLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 处理后端返回 { error: ... }
|
||||||
|
if (userData && userData.error) {
|
||||||
|
setUser(null);
|
||||||
|
setIsAuthenticated(false);
|
||||||
|
} else {
|
||||||
|
setUser(userData);
|
||||||
|
setIsAuthenticated(true);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
setUser(null);
|
setUser(null);
|
||||||
setIsAuthenticated(false);
|
setIsAuthenticated(false);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user