Skip to content

Commit c0d440c

Browse files
committed
feat(serverHandler): render 401 unauthorized state as regular page
1 parent dce75e0 commit c0d440c

File tree

4 files changed

+34
-38
lines changed

4 files changed

+34
-38
lines changed

src/i18n/translation.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Translation struct {
3333
DeleteLabel string
3434
DeleteConfirm string
3535

36+
Error401 string
3637
Error403 string
3738
Error404 string
3839
Error500 string
@@ -71,6 +72,7 @@ var translationEnUs = Translation{
7172
DeleteLabel: "Delete",
7273
DeleteConfirm: "Confirm delete?",
7374

75+
Error401: "401 unauthorized",
7476
Error403: "403 resource is forbidden",
7577
Error404: "404 resource not found",
7678
Error500: "500 potential issue occurred",
@@ -109,6 +111,7 @@ var translationZhSimp = Translation{
109111
DeleteLabel: "删除",
110112
DeleteConfirm: "确认删除吗?",
111113

114+
Error401: "401 未授权",
112115
Error403: "403 禁止访问资源",
113116
Error404: "404 资源不存在",
114117
Error500: "500 发生潜在错误",
@@ -147,6 +150,7 @@ var translationZhTrad = Translation{
147150
DeleteLabel: "刪除",
148151
DeleteConfirm: "確認刪除嗎?",
149152

153+
Error401: "401 未授權",
150154
Error403: "403 禁止訪問資源",
151155
Error404: "404 資源不存在",
152156
Error500: "500 發生潛在錯誤",

src/serverHandler/aliasHandler.go

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -118,49 +118,44 @@ func (h *aliasHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
118118

119119
if data.NeedAuth {
120120
h.notifyAuth(w, r)
121+
}
121122

122-
if !data.AuthSuccess {
123-
if !h.postMiddleware(w, r, data, fsPath) {
124-
h.authFailed(w, data.Status)
125-
}
126-
return
127-
}
128-
123+
if data.AuthSuccess {
129124
if data.forceAuth {
130125
h.redirectWithoutForceAuth(w, r, data)
131126
return
132127
}
133-
}
134128

135-
if data.NeedDirSlashRedirect {
136-
h.redirectWithSlashSuffix(w, r, data.prefixReqPath)
137-
return
138-
}
139-
140-
header(w, data.Headers)
129+
if data.NeedDirSlashRedirect {
130+
h.redirectWithSlashSuffix(w, r, data.prefixReqPath)
131+
return
132+
}
141133

142-
if data.CanCors {
143-
cors(w)
144-
}
134+
header(w, data.Headers)
145135

146-
if data.IsMutate {
147-
h.mutate(w, r, data)
148-
return
149-
}
136+
if data.CanCors {
137+
cors(w)
138+
}
150139

151-
// archive
152-
if len(r.URL.RawQuery) >= 3 {
153-
switch r.URL.RawQuery[:3] {
154-
case "tar":
155-
h.tar(w, r, data)
156-
return
157-
case "tgz":
158-
h.tgz(w, r, data)
159-
return
160-
case "zip":
161-
h.zip(w, r, data)
140+
if data.IsMutate {
141+
h.mutate(w, r, data)
162142
return
163143
}
144+
145+
// archive
146+
if len(r.URL.RawQuery) >= 3 {
147+
switch r.URL.RawQuery[:3] {
148+
case "tar":
149+
h.tar(w, r, data)
150+
return
151+
case "tgz":
152+
h.tgz(w, r, data)
153+
return
154+
case "zip":
155+
h.zip(w, r, data)
156+
return
157+
}
158+
}
164159
}
165160

166161
if h.postMiddleware(w, r, data, fsPath) {

src/serverHandler/auth.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,3 @@ func (h *aliasHandler) verifyAuth(r *http.Request, needAuth bool) (username stri
4343

4444
return
4545
}
46-
47-
func (h *aliasHandler) authFailed(w http.ResponseWriter, status int) {
48-
w.WriteHeader(status)
49-
w.Write([]byte("Unauthorized"))
50-
}

src/tpl/defaultTheme/frontend/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@
125125
{{end}}
126126
</ul>
127127

128-
{{if eq .Status 403}}
128+
{{if eq .Status 401}}
129+
<div class="error">{{.Trans.Error401}}</div>
130+
{{else if eq .Status 403}}
129131
<div class="error">{{.Trans.Error403}}</div>
130132
{{else if eq .Status 404}}
131133
<div class="error">{{.Trans.Error404}}</div>

0 commit comments

Comments
 (0)