Skip to content

Commit bf4029a

Browse files
Added initial parts of the login system. Currently just takes you to a page that says you are logged in.
1 parent 9f61f77 commit bf4029a

File tree

17 files changed

+277
-33
lines changed

17 files changed

+277
-33
lines changed

controllers/about.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package controllers
2+
3+
import (
4+
"net/http"
5+
)
6+
7+
/* ContactHandler serves the contact page */
8+
func AboutHandler(w http.ResponseWriter, r *http.Request) {
9+
err := tpl.Get("about").ExecuteTemplate(w, "base-tpl", nil)
10+
checkErr(err)
11+
}

controllers/auth.go

+58-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,65 @@
11
package controllers
22

33
import (
4+
"fmt"
45
"net/http"
6+
7+
"github.com/blastbeatsandcode/blastbeatsandcode-website/utils"
58
)
69

7-
/* LoginHandler serves the login page */
8-
func LoginHandler(w http.ResponseWriter, r *http.Request) {
9-
err := tpl.Get("login").ExecuteTemplate(w, "base-tpl", nil)
10-
checkErr(err)
10+
// /* LoginHandler serves the login page */
11+
// func LoginHandler(w http.ResponseWriter, r *http.Request) {
12+
// err := tpl.Get("login").ExecuteTemplate(w, "base-tpl", nil)
13+
// checkErr(err)
14+
// }
15+
16+
/* AuthGetHandler handles login GET requests */
17+
func AuthGetHandler(w http.ResponseWriter, r *http.Request) {
18+
isAuth := utils.HandleAccess(r)
19+
20+
// Check if we have a username
21+
// If we do, tell the user they are logged in
22+
if isAuth {
23+
redirURL := "/edit"
24+
http.Redirect(w, r, redirURL, http.StatusSeeOther)
25+
} else {
26+
err := tpl.Get("login").ExecuteTemplate(w, "base-tpl", nil)
27+
checkErr(err)
28+
}
29+
}
30+
31+
/* Takes information from login POST requests and logs user in or shows error */
32+
func AuthPostHandler(w http.ResponseWriter, r *http.Request) {
33+
r.ParseForm()
34+
username := r.PostForm.Get("username")
35+
password := r.PostForm.Get("password")
36+
37+
err := utils.CheckLogin(username, password)
38+
39+
if err != nil { // If error is not nil, then the login does not match
40+
username = ""
41+
fmt.Println("THERE WAS AN ERROR LOGGING IN")
42+
}
43+
44+
// Save the session
45+
store := utils.GetStore()
46+
session, _ := store.Get(r, "session")
47+
48+
session.Values["username"] = username
49+
session.Save(r, w)
50+
51+
// Check if we have a matching project ID
52+
// If we do, load edit page, otherwise prompt for login
53+
isAuth := utils.HandleAccess(r)
54+
55+
// If user is authorized to edit and the request matches, load edit page
56+
if isAuth {
57+
redirURL := "/edit"
58+
http.Redirect(w, r, redirURL, http.StatusSeeOther)
59+
} else { // Otherwise load the login-failed template
60+
session.Values["username"] = ""
61+
session.Save(r, w)
62+
err := tpl.Get("login").ExecuteTemplate(w, "base-tpl", nil)
63+
checkErr(err)
64+
}
1165
}

controllers/blog.go

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
/* BlogHandler serves the blog page */
1212
func BlogHandler(w http.ResponseWriter, r *http.Request) {
1313
db := utils.GetDB()
14+
defer db.Close()
15+
1416
m := getBlogPosts(db)
1517

1618
err := tpl.Get("blog").ExecuteTemplate(w, "base-tpl", m)

controllers/contact.go

-11
This file was deleted.

controllers/edit.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package controllers
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/blastbeatsandcode/blastbeatsandcode-website/utils"
7+
)
8+
9+
/* ContactHandler serves the contact page */
10+
func EditHandler(w http.ResponseWriter, r *http.Request) {
11+
isAuth := utils.HandleAccess(r)
12+
13+
// Check if we have a username
14+
// If we do, tell the user they are logged in
15+
if isAuth {
16+
err := tpl.Get("edit").ExecuteTemplate(w, "base-tpl", nil)
17+
checkErr(err)
18+
} else {
19+
redirURL := "/"
20+
http.Redirect(w, r, redirURL, http.StatusSeeOther)
21+
}
22+
}

controllers/home.go

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
/* HomeHandler serves the index page */
1212
func HomeHandler(w http.ResponseWriter, r *http.Request) {
1313
db := utils.GetDB()
14+
defer db.Close()
1415
m := getLatestPost(db)
1516

1617
err := tpl.Get("index").ExecuteTemplate(w, "base-tpl", m)

models/user.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package models
2+
3+
// User struct that contains information about users
4+
// Sets column definitions for GORM migration to database
5+
type User struct {
6+
UserID int `gorm:"primary_key"`
7+
Username string
8+
Password []byte
9+
IsAdmin bool
10+
}

public/css/base.css

+13-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ a:hover {
5252
}
5353

5454
body {
55-
font-family: 'Fira Sans', sans-serif;
55+
font-family: 'Lato', Helvetica, sans-serif;
56+
font-size: 1.2rem;
5657
}
5758

5859
footer {
@@ -70,6 +71,17 @@ h1, h2, h3 {
7071
font-family: 'Abel', serif;
7172
}
7273

74+
.login {
75+
max-width: 600px;
76+
padding: 2rem;
77+
}
78+
79+
.login input {
80+
height: 3rem;
81+
font-size: 1.2em;
82+
margin-bottom: 1rem;
83+
}
84+
7385
.navbar, .navbar a {
7486
color: black;
7587
}

routes/routes.go

+18-8
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,30 @@ func Routes() *mux.Router {
3939
projectRoute.PathPrefix("/public/img/").Handler(http.StripPrefix("/public/img/", http.FileServer(http.Dir("./public/img/"))))
4040
projectRoute.PathPrefix("/public/js/").Handler(http.StripPrefix("/public/js/", http.FileServer(http.Dir("./public/js/"))))
4141

42-
// Register Contact
43-
contactRoute := r.PathPrefix("/contact").Subrouter()
44-
contactRoute.HandleFunc("", controllers.ContactHandler)
45-
contactRoute.PathPrefix("/public/css/").Handler(http.StripPrefix("/public/css/", http.FileServer(http.Dir("./public/css/"))))
46-
contactRoute.PathPrefix("/public/img/").Handler(http.StripPrefix("/public/img/", http.FileServer(http.Dir("./public/img/"))))
47-
contactRoute.PathPrefix("/public/js/").Handler(http.StripPrefix("/public/js/", http.FileServer(http.Dir("./public/js/"))))
42+
// Register About
43+
aboutRoute := r.PathPrefix("/about").Subrouter()
44+
aboutRoute.HandleFunc("", controllers.AboutHandler)
45+
aboutRoute.PathPrefix("/public/css/").Handler(http.StripPrefix("/public/css/", http.FileServer(http.Dir("./public/css/"))))
46+
aboutRoute.PathPrefix("/public/img/").Handler(http.StripPrefix("/public/img/", http.FileServer(http.Dir("./public/img/"))))
47+
aboutRoute.PathPrefix("/public/js/").Handler(http.StripPrefix("/public/js/", http.FileServer(http.Dir("./public/js/"))))
4848

49-
// Register Login
49+
// Register Edit
50+
editRoute := r.PathPrefix("/edit").Subrouter()
51+
editRoute.HandleFunc("", controllers.EditHandler)
52+
editRoute.PathPrefix("/public/css/").Handler(http.StripPrefix("/public/css/", http.FileServer(http.Dir("./public/css/"))))
53+
editRoute.PathPrefix("/public/img/").Handler(http.StripPrefix("/public/img/", http.FileServer(http.Dir("./public/img/"))))
54+
editRoute.PathPrefix("/public/js/").Handler(http.StripPrefix("/public/js/", http.FileServer(http.Dir("./public/js/"))))
55+
56+
// Register Login, this one has a POST and a GET because we use a form
5057
loginRoute := r.PathPrefix("/login").Subrouter()
51-
loginRoute.HandleFunc("", controllers.LoginHandler)
58+
loginRoute.HandleFunc("", controllers.AuthGetHandler).Methods("GET")
59+
loginRoute.HandleFunc("", controllers.AuthPostHandler).Methods("POST")
5260
loginRoute.PathPrefix("/public/css/").Handler(http.StripPrefix("/public/css/", http.FileServer(http.Dir("./public/css/"))))
5361
loginRoute.PathPrefix("/public/img/").Handler(http.StripPrefix("/public/img/", http.FileServer(http.Dir("./public/img/"))))
5462
loginRoute.PathPrefix("/public/js/").Handler(http.StripPrefix("/public/js/", http.FileServer(http.Dir("./public/js/"))))
5563

64+
// TODO: Add a logout route
65+
5666
return r
5767
}
5868

Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{{define "title-tpl"}}
2-
Contact - Blast Beats and Code | Software and Game Developer Alex Silcott
2+
About - Blast Beats and Code | Software and Game Developer Alex Silcott
33
{{end}}
44

55

66
{{define "banner-tpl"}}
77
<div class="banner-other">
88
<div class="container">
9-
<span id="title">Contact</span>
9+
<span id="title">About Me</span>
1010
</div>
1111
</div>
1212
{{end}}
1313

1414
{{define "content-tpl"}}
15-
Contact content
15+
About Me content
1616
{{end}}

templates/base.gohtml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
1111
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">
1212
<link rel="stylesheet" type="text/css" href="/public/css/base.css">
13-
<link href="https://fonts.googleapis.com/css?family=Fira+Sans|Abel|Arvo" rel="stylesheet">
13+
<link href="https://fonts.googleapis.com/css?family=Fira+Sans|Abel|Arvo|Lato" rel="stylesheet">
1414
</head>
1515
<body>
1616
{{template "nav-tpl" .}}

templates/edit.gohtml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{{define "title-tpl"}}
2+
Edit - Blast Beats and Code | Software and Game Developer Alex Silcott
3+
{{end}}
4+
5+
6+
{{define "banner-tpl"}}
7+
<div class="banner-other">
8+
<div class="container">
9+
<span id="title">Edit</span>
10+
</div>
11+
</div>
12+
{{end}}
13+
14+
{{define "content-tpl"}}
15+
<p>You are logged in!</p>
16+
17+
<p><a href="#"> Log out</a></p>
18+
{{end}}

templates/login.gohtml

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1+
{{define "title-tpl"}}
2+
Login - Blast Beats and Code | Software and Game Developer Alex Silcott
3+
{{end}}
4+
15
{{define "banner-tpl"}}
2-
<div class="banner">
6+
<!--<div class="banner-other">
37
<div class="container">
48
<span id="title">Login</span>
59
</div>
6-
</div>
10+
</div>-->
711
{{end}}
812

913
{{define "content-tpl"}}
10-
Login content
14+
<div class="card card-container login mx-auto">
15+
<h1 class="text-center white">Login</h1>
16+
<form class="form-signin" method="POST">
17+
<span id="reauth-email" class="reauth-email"></span>
18+
<input type="text" id="username" name="username" class="form-control" placeholder="Username" required autofocus>
19+
<input type="password" id="password" name="password" class="form-control" placeholder="Password" required>
20+
<button class="btn btn-lg btn-primary btn-block btn-signin" type="submit">Sign in</button>
21+
</form>
22+
</div>
1123
{{end}}

templates/nav.gohtml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<a class="nav-link" href="/projects">Projects</a>
2323
</li>
2424
<li class="nav-item">
25-
<a class="nav-link" href="/contact">Contact</a>
25+
<a class="nav-link" href="/about">About</a>
2626
</li>
2727
</ul>
2828
</div>

templates/templates.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ func GetTemplates() Reloader {
3838
"index": parsedTemplates("index"),
3939
"projects": parsedTemplates("projects"),
4040
"blog": parsedTemplates("blog"),
41-
"contact": parsedTemplates("contact"),
41+
"about": parsedTemplates("about"),
4242
"login": parsedTemplates("login"),
43+
"edit": parsedTemplates("edit"),
4344
}
4445

4546
return *r

utils/db.go

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func GetDB() *gorm.DB {
2727
/* Automigrate database based on the structs in /models */
2828
func dbSetup(db *gorm.DB) gorm.DB {
2929
db.AutoMigrate(&models.BlogPost{})
30+
db.AutoMigrate(&models.User{})
3031

3132
return *db
3233
}

0 commit comments

Comments
 (0)