-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
65 lines (53 loc) · 1.74 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
63
64
65
package controllers
import (
"fmt"
"net/http"
"github.com/blastbeatsandcode/blastbeatsandcode-website/utils"
)
// /* LoginHandler serves the login page */
// func LoginHandler(w http.ResponseWriter, r *http.Request) {
// err := tpl.Get("login").ExecuteTemplate(w, "base-tpl", nil)
// checkErr(err)
// }
/* AuthGetHandler handles login GET requests */
func AuthGetHandler(w http.ResponseWriter, r *http.Request) {
isAuth := utils.HandleAccess(r)
// Check if we have a username
// If we do, tell the user they are logged in
if isAuth {
redirURL := "/edit"
http.Redirect(w, r, redirURL, http.StatusSeeOther)
} else {
err := tpl.Get("login").ExecuteTemplate(w, "base-tpl", nil)
checkErr(err)
}
}
/* Takes information from login POST requests and logs user in or shows error */
func AuthPostHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
username := r.PostForm.Get("username")
password := r.PostForm.Get("password")
err := utils.CheckLogin(username, password)
if err != nil { // If error is not nil, then the login does not match
username = ""
fmt.Println("THERE WAS AN ERROR LOGGING IN")
}
// Save the session
store := utils.GetStore()
session, _ := store.Get(r, "session")
session.Values["username"] = username
session.Save(r, w)
// Check if we have a matching project ID
// If we do, load edit page, otherwise prompt for login
isAuth := utils.HandleAccess(r)
// If user is authorized to edit and the request matches, load edit page
if isAuth {
redirURL := "/edit"
http.Redirect(w, r, redirURL, http.StatusSeeOther)
} else { // Otherwise load the login-failed template
session.Values["username"] = ""
session.Save(r, w)
err := tpl.Get("login").ExecuteTemplate(w, "base-tpl", nil)
checkErr(err)
}
}