This repository has been archived by the owner on Nov 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
154 lines (133 loc) · 3.16 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
package main
import (
"fmt"
"github.com/satori/go.uuid"
"html/template"
"net/http"
)
type user struct {
Id string
First string
Last string
Email string
Password string
}
var tpl *template.Template
var muser = map[string]user{} // key is userid, value is user
var msession = map[string]string{} // key is sessionid, value is userid
func init() {
tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
}
func main() {
http.HandleFunc("/", index)
http.HandleFunc("/other", other)
http.HandleFunc("/login", login)
http.HandleFunc("/logout", logout)
http.HandleFunc("/signup", signup)
http.Handle("/favicon.ico", http.NotFoundHandler())
http.ListenAndServe(":8080", nil)
}
func index(w http.ResponseWriter, r *http.Request) {
c, err := r.Cookie("mysess")
if err != nil {
u := uuid.NewV4()
c = &http.Cookie{
Name: "mysess",
Value: u.String(),
Path: "/",
HttpOnly: true,
}
}
uid := msession[c.Value]
usr := muser[uid]
fmt.Println("SESSION ID", c.Value)
http.SetCookie(w, c)
tpl.ExecuteTemplate(w, "index.gohtml", usr)
}
func other(w http.ResponseWriter, r *http.Request) {
c, err := r.Cookie("mysess")
if err != nil {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
uid := msession[c.Value]
usr := muser[uid]
fmt.Println("SESSION ID", c.Value)
http.SetCookie(w, c)
tpl.ExecuteTemplate(w, "other.gohtml", usr)
}
func logout(w http.ResponseWriter, r *http.Request) {
c, err := r.Cookie("mysess")
if err != nil {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
delete(msession, c.Value)
c.MaxAge = -1
http.SetCookie(w, c)
http.Redirect(w, r, "/", http.StatusSeeOther)
//fmt.Fprint(w, "You've been logged out")
}
func login(w http.ResponseWriter, r *http.Request) {
c, err := r.Cookie("mysess")
if err != nil {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
if r.Method == "POST" {
em := r.FormValue("email")
pw := r.FormValue("password")
// get all of the usernames and passwords
m := map[string]user{}
for _, v := range muser {
m[v.Email] = v
}
usr, ok := m[em]
if !ok {
http.Redirect(w, r, "/login", http.StatusForbidden)
return
}
if usr.Password != pw {
http.Redirect(w, r, "/login", http.StatusForbidden)
return
}
msession[c.Value] = usr.Id
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
tpl.ExecuteTemplate(w, "login.gohtml", nil)
}
func signup(w http.ResponseWriter, r *http.Request) {
c, err := r.Cookie("mysess")
// if no cookie, redirect to index
if err != nil {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
// user has already signed up, so redirect
uid := msession[c.Value]
usr := muser[uid]
if usr.Email != "" {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
if r.Method == "POST" {
fn := r.FormValue("first")
ln := r.FormValue("last")
em := r.FormValue("email")
pw := r.FormValue("password")
u := uuid.NewV4()
usr = user{
Id: u.String(),
First: fn,
Last: ln,
Email: em,
Password: pw,
}
muser[usr.Id] = usr
msession[c.Value] = usr.Id
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
tpl.ExecuteTemplate(w, "signup.gohtml", nil)
}