mirror of
				https://github.com/khairul169/garage-webui.git
				synced 2025-10-30 22:59:31 +07:00 
			
		
		
		
	
		
			
				
	
	
		
			28 lines
		
	
	
		
			536 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			28 lines
		
	
	
		
			536 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| package middleware
 | |
| 
 | |
| import (
 | |
| 	"errors"
 | |
| 	"khairul169/garage-webui/utils"
 | |
| 	"net/http"
 | |
| )
 | |
| 
 | |
| func AuthMiddleware(next http.Handler) http.Handler {
 | |
| 	authData := utils.GetEnv("AUTH_USER_PASS", "")
 | |
| 
 | |
| 	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 | |
| 		auth := utils.Session.Get(r, "authenticated")
 | |
| 
 | |
| 		if authData == "" {
 | |
| 			next.ServeHTTP(w, r)
 | |
| 			return
 | |
| 		}
 | |
| 
 | |
| 		if auth == nil || !auth.(bool) {
 | |
| 			utils.ResponseErrorStatus(w, errors.New("unauthorized"), http.StatusUnauthorized)
 | |
| 			return
 | |
| 		}
 | |
| 
 | |
| 		next.ServeHTTP(w, r)
 | |
| 	})
 | |
| }
 |