-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
48 lines (40 loc) · 1.04 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package routes
import (
"path"
"strings"
"github.com/rocwong/neko"
"github.com/go-ant/ant/core/server/models"
"github.com/go-ant/ant/core/server/modules/middleware"
"github.com/go-ant/ant/core/server/modules/setting"
)
type authOptions struct {
IsApi bool
SignInRequired bool
SignOutRequired bool
}
func auth(options *authOptions) neko.HandlerFunc {
return func(ctx *neko.Context) {
// cannot view any page before installation.
if !setting.InstallLock {
ctx.Redirect(setting.INSTALLER_URL)
ctx.Abort()
return
}
if options.SignOutRequired && middleware.Context.IsSigned && ctx.Req.RequestURI != "/" {
ctx.Redirect(setting.Host.Path)
return
}
if options.SignInRequired && !middleware.Context.IsSigned {
if isApiPath(ctx.Req.URL.Path) {
ctx.Json(models.RestApi{Error: models.ApiMsg.NeedToSignIn})
} else {
ctx.Redirect(setting.Host.Path + "/goant/login")
}
ctx.Abort()
return
}
}
}
func isApiPath(url string) bool {
return strings.HasPrefix(url, path.Join(setting.Host.Path, "/api/"))
}