-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
57 lines (48 loc) · 1.37 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
49
50
51
52
53
54
55
56
57
package main
import (
"net/http"
"strings"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
webSessions "github.com/martini-contrib/sessions"
)
func AuthFunc(req *http.Request, session webSessions.Session, r render.Render, c martini.Context) bool {
if strings.HasPrefix(req.RequestURI, "/api") || strings.HasPrefix(req.RequestURI, "/login") {
return true
}
user := session.Get("auth_user")
if user == nil {
r.Redirect("/login", 302)
return true
}
session.Set("auth_user", user)
c.Map(user.(string))
return true
}
func Login(params martini.Params, r render.Render) {
data := map[string]interface{}{"username": "", "msg": ""}
r.HTML(200, "login", data)
}
func Signin(req *http.Request, session webSessions.Session, r render.Render) {
req.ParseForm()
username := req.PostForm.Get("name")
password := req.PostForm.Get("password")
var user User
db.First(&user, User{Name: username})
if user.Id <= 0 {
data := map[string]interface{}{"username": "", "msg": "User not found!"}
r.HTML(200, "login", data)
return
}
if password == user.Password {
session.Set("auth_user", username)
r.Redirect("/", 302)
} else {
data := map[string]interface{}{"username": "", "msg": "Incorrent password."}
r.HTML(200, "login", data)
}
}
func Signout(session webSessions.Session, r render.Render) {
session.Delete("auth_user")
r.Redirect("/login", 302)
}