-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
62 lines (49 loc) · 1.55 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
58
59
60
61
62
package auth
import (
"net/http"
"github.com/dense-analysis/pricewarp/internal/database"
"github.com/dense-analysis/pricewarp/internal/model"
"github.com/dense-analysis/pricewarp/internal/session"
"github.com/dense-analysis/pricewarp/internal/template"
"golang.org/x/crypto/bcrypt"
)
type LoginFormData struct {
User *model.User
ErrorMessage string
}
func HandleViewLoginForm(writer http.ResponseWriter, request *http.Request) {
data := LoginFormData{}
if request.Method == "POST" {
data.ErrorMessage = "Invalid login!"
}
template.Render(template.Login, writer, data)
}
func HandleLogin(conn *database.Conn, writer http.ResponseWriter, request *http.Request) {
request.ParseForm()
username := request.Form.Get("username")
password := request.Form.Get("password")
var userID int
loginValid := false
if len(username) > 0 && len(password) > 0 {
row := conn.QueryRow(
"select id, password from crypto_user where username = $1",
username,
)
var passwordHash string
if err := row.Scan(&userID, &passwordHash); err == nil {
if bcrypt.CompareHashAndPassword([]byte(passwordHash), []byte(password)) == nil {
loginValid = true
}
}
}
if loginValid {
session.SaveUserInSession(writer, request, &model.User{ID: userID, Username: username})
http.Redirect(writer, request, "/alert", http.StatusFound)
} else {
HandleViewLoginForm(writer, request)
}
}
func HandleLogout(writer http.ResponseWriter, request *http.Request) {
session.ClearSession(writer, request)
http.Redirect(writer, request, "/login", http.StatusFound)
}