-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
85 lines (70 loc) · 2.09 KB
/
app.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package controllers
import (
"fmt"
"os"
"strconv"
"github.com/pdmp/amai/app/models"
gormc "github.com/revel/modules/orm/gorm/app/controllers"
"github.com/revel/revel"
"golang.org/x/crypto/bcrypt"
)
type App struct {
gormc.Controller
}
var (
url = "bento/dist/index.html"
)
func (c App) Index() revel.Result {
return c.RenderFileName(url, revel.NoDisposition)
}
func (c App) Login() revel.Result {
username := c.Params.Form.Get("username")
pass := c.Params.Form.Get("password")
var user models.UserT
//return c.RenderJSON(c.DB.Where("user_username = ?", username).Find(&user))
c.DB.Where("user_name = ?", username).Find(&user)
//return c.RenderJSON(&user)
fmt.Println("hash", user.UserPassword, "\npass -> "+pass, "\nbyte", []byte(pass))
if user.UserName != "" {
err := bcrypt.CompareHashAndPassword([]byte(user.UserPassword), []byte(pass))
if err == nil {
if user.IdTipo == 1 {
c.Session["user"] = user.UserName
c.Session.SetDefaultExpiration()
}
token := encodeToken(username)
return c.RenderJSON(token)
}
}
return c.RenderJSON("auth failed")
//return c.RenderFileName("bento/dist/index.html", revel.NoDisposition)
}
func (c App) Register() revel.Result {
username := c.Params.Form.Get("username")
pass := c.Params.Form.Get("password")
idGoogle := c.Params.Form.Get("idGoogle")
hashPass, _ := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost)
var user models.UserT
user.UserName = username
user.UserPassword = hashPass
if pass == "" {
value, _ := strconv.Atoi(idGoogle)
user.IdGoogle = uint(value)
}
user.IdTipo = 3
c.DB.Create(&user)
return c.RenderJSON(user)
//return c.Redirect(routes.App.Index())
}
func (c App) Insert() revel.Result {
username := c.Params.Form.Get("username")
pass := c.Params.Form.Get("password")
password, _ := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost)
user := models.UserT{UserName: username, UserPassword: password}
c.DB.Create(&user)
return c.RenderJSON(user)
}
func (c App) Image(name string) revel.Result {
f, _ := os.Open("public/img/" + name)
return c.RenderFile(f, revel.Attachment)
}