Skip to content

Commit

Permalink
Added code for sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
metamemelord committed Aug 3, 2019
1 parent ed37d3b commit a26ae53
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 0 deletions.
Binary file added sessions/2. Creating sessions/main
Binary file not shown.
88 changes: 88 additions & 0 deletions sessions/2. Creating sessions/main.go
@@ -0,0 +1,88 @@
package main

import (
"html/template"
"net/http"

uuid "github.com/satori/go.uuid"
)

type user struct {
UserName string
Password string
First string
Last string
}

var tpl *template.Template
var dbUsers = map[string]user{}
var dbSessions = make(map[string]string)

func init() {
tpl = template.Must(template.ParseGlob("templates/*"))
}

func main() {
http.HandleFunc("/", index)
http.HandleFunc("/bar", bar)
http.HandleFunc("/signup", signup)
http.Handle("/favicon.ico", http.NotFoundHandler())
http.ListenAndServe(":8080", nil)
}

func index(w http.ResponseWriter, req *http.Request) {
u := getUser(w, req)
tpl.ExecuteTemplate(w, "index.gohtml", u)
}

func bar(w http.ResponseWriter, req *http.Request) {

c, err := req.Cookie("session-id")
if err != nil {
http.Redirect(w, req, "/", http.StatusSeeOther)
return
}
un, ok := dbSessions[c.Value]
if !ok {
http.Redirect(w, req, "/", http.StatusSeeOther)
return
}
u := dbUsers[un]
tpl.ExecuteTemplate(w, "bar.gohtml", u)
}

func signup(w http.ResponseWriter, req *http.Request) {
if alreadyLoggedIn(req) {
http.Redirect(w, req, "/", http.StatusSeeOther)
return
}

if req.Method == http.MethodPost {

un := req.FormValue("username")
p := req.FormValue("password")
f := req.FormValue("firstname")
l := req.FormValue("lastname")

if _, ok := dbUsers[un]; ok {
http.Error(w, "Username already taken", http.StatusForbidden)
return
}

sID, _ := uuid.NewV4()
c := &http.Cookie{
Name: "session-id",
Value: sID.String(),
}
http.SetCookie(w, c)
dbSessions[c.Value] = un

u := user{un, p, f, l}
dbUsers[un] = u

http.Redirect(w, req, "/", http.StatusSeeOther)
return
}

tpl.ExecuteTemplate(w, "signup.gohtml", nil)
}
36 changes: 36 additions & 0 deletions sessions/2. Creating sessions/session.go
@@ -0,0 +1,36 @@
package main

import (
"net/http"

uuid "github.com/satori/go.uuid"
)

func getUser(w http.ResponseWriter, req *http.Request) user {

c, err := req.Cookie("session-id")
if err != nil {
sID, _ := uuid.NewV4()
c = &http.Cookie{
Name: "session-id",
Value: sID.String(),
}

}

var u user
if un, ok := dbSessions[c.Value]; ok {
u = dbUsers[un]
}
return u
}

func alreadyLoggedIn(req *http.Request) bool {
c, err := req.Cookie("session-id")
if err != nil {
return false
}
un := dbSessions[c.Value]
_, ok := dbUsers[un]
return ok
}
18 changes: 18 additions & 0 deletions sessions/2. Creating sessions/templates/bar.gohtml
@@ -0,0 +1,18 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BAR</title>
</head>
<body>

<h1>Welcome to the bar. What can I get you to drink?</h1>

{{if .First}}
USER NAME {{.UserName}}<br>
FIRST {{.First}}<br>
LAST {{.Last}}<br>
{{end}}

</body>
</html>
28 changes: 28 additions & 0 deletions sessions/2. Creating sessions/templates/index.gohtml
@@ -0,0 +1,28 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>

<form method="post">

<input type="email" name="username" placeholder="email"><br>
<input type="text" name="firstname" placeholder="first name"><br>
<input type="text" name="lastname" placeholder="last name"><br>
<input type="submit">

</form>


{{if .First}}
USER NAME {{.UserName}}<br>
FIRST {{.First}}<br>
LAST {{.Last}}<br>
{{end}}

<br>
<h2>Go to <a href="/bar">the bar</a></h2>
</body>
</html>
20 changes: 20 additions & 0 deletions sessions/2. Creating sessions/templates/signup.gohtml
@@ -0,0 +1,20 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>

<form method="post">

<input type="email" name="username" placeholder="email"><br>
<input type="text" name="password" placeholder="password"><br>
<input type="text" name="firstname" placeholder="first name"><br>
<input type="text" name="lastname" placeholder="last name"><br>
<input type="submit">

</form>

</body>
</html>

0 comments on commit a26ae53

Please sign in to comment.