-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
225 lines (216 loc) · 5.57 KB
/
main.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"text/template"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"golang.org/x/crypto/bcrypt"
)
func dbConn() (db *sql.DB) {
dbDriver := "mysql"
dbUser := "root"
dbPass := "root@123"
dbName := "test"
db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"@/"+dbName)
if err != nil {
panic(err.Error())
}
return db
}
type Employee struct {
Id int
Username string
Password string
Created_at string
}
var store = sessions.NewCookieStore([]byte("t0p-s3cr3t"))
var tmpl = template.Must(template.ParseGlob("form/*"))
func dashboard(w http.ResponseWriter, r *http.Request) {
db := dbConn()
session, _ := store.Get(r, "session")
untyped, ok := session.Values["username"]
if !ok {
return
}
Susername, ok := untyped.(string)
if !ok {
return
}
untyped1, ok := session.Values["authenticated"]
if !ok {
return
}
SDauth, ok := untyped1.(bool)
if !ok {
return
}
selDB, err := db.Query("SELECT * FROM users WHERE username=?", Susername)
if err != nil {
panic(err.Error())
}
emp := Employee{}
for selDB.Next() {
var id int
var username, password, created_at string
err = selDB.Scan(&id, &username, &password, &created_at)
if err != nil {
panic(err.Error())
}
emp.Id = id
emp.Username = username
emp.Password = password
emp.Created_at = created_at
}
DMessage := "Either your session expired or you are logged out !!"
if SDauth == true {
//w.Write([]byte(username))
tmpl.ExecuteTemplate(w, "Dashboard", emp)
return
} else {
w.Write([]byte(DMessage))
}
defer db.Close()
}
func Index(w http.ResponseWriter, r *http.Request) {
db := dbConn()
selDB, err := db.Query("SELECT * FROM users ORDER BY id DESC")
if err != nil {
panic(err.Error())
}
emp := Employee{}
res := []Employee{}
for selDB.Next() {
var id int
var username, password, created_at string
err = selDB.Scan(&id, &username, &password, &created_at)
if err != nil {
panic(err.Error())
}
emp.Id = id
emp.Username = username
emp.Password = password
emp.Created_at = created_at
res = append(res, emp)
}
tmpl.ExecuteTemplate(w, "Index", res)
defer db.Close()
}
func Show(w http.ResponseWriter, r *http.Request) {
db := dbConn()
nId := r.URL.Query().Get("id")
selDB, err := db.Query("SELECT * FROM users WHERE id=?", nId)
if err != nil {
panic(err.Error())
}
emp := Employee{}
for selDB.Next() {
var id int
var username, password, created_at string
err = selDB.Scan(&id, &username, &password, &created_at)
if err != nil {
panic(err.Error())
}
emp.Id = id
emp.Username = username
emp.Password = password
emp.Created_at = created_at
}
tmpl.ExecuteTemplate(w, "Show", emp)
defer db.Close()
}
func Registration(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "Registration", nil)
}
func LoginPage(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "Login", nil)
}
func Register(w http.ResponseWriter, r *http.Request) {
db := dbConn()
if r.Method == "POST" {
username := r.FormValue("username")
password := r.FormValue("password")
// Password changed to hash
hash, _ := HashPassword(password) // ignore error for the sake of simplicity
t := time.Now()
t.Format(time.RFC3339)
insUForm, err := db.Prepare("INSERT INTO users (username,password, created_at) values (?,?,?)")
if err != nil {
panic(err.Error())
}
insUForm.Exec(username, hash, t)
}
defer db.Close()
http.Redirect(w, r, "/", 301)
}
func HashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 15)
return string(bytes), err
}
func CheckPasswordHash(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
func login(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
db := dbConn()
if r.Method == "POST" {
usernameFlogin := r.FormValue("username")
passwordFlogin := r.FormValue("password")
session, _ := store.Get(r, "session")
session.Values["username"] = usernameFlogin
selDB, err := db.Query("SELECT * FROM users WHERE username=?", usernameFlogin)
if err != nil {
panic(err.Error())
}
for selDB.Next() {
var id int
var username, password, created_at string
err = selDB.Scan(&id, &username, &password, &created_at)
if err != nil {
panic(err.Error())
}
hash1 := password
// Password from Database
match := CheckPasswordHash(passwordFlogin, hash1) // Compare Password from Db and Login page
if match == true {
session.Values["authenticated"] = true
session.Save(r, w)
http.Redirect(w, r, "/dashboard", 301)
} else {
http.Redirect(w, r, "/registration", 301)
}
}
}
defer db.Close()
}
func end(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "session")
session.Values["authenticated"] = false
session.Save(r, w)
http.Redirect(w, r, "/dashboard", 301)
}
func main() {
r := mux.NewRouter()
log.Println("Server started on: http://localhost:3000")
r.HandleFunc("/dashboard", dashboard)
r.HandleFunc("/", Index)
r.HandleFunc("/auth", LoginPage)
r.HandleFunc("/login", login)
r.HandleFunc("/end", end)
r.HandleFunc("/books/{title}/page/{page}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
title := vars["title"]
page := vars["page"]
fmt.Fprintf(w, "You've requested the book: %s on page %s\n", title, page)
})
r.HandleFunc("/show", Show)
r.HandleFunc("/registration", Registration)
r.HandleFunc("/register", Register)
http.ListenAndServe(":3000", r)
}
// Recheck code in August 2020