Add Sign in page
This commit is contained in:
parent
7d234c5aad
commit
2d53824194
@ -11,7 +11,7 @@ type Template interface {
|
|||||||
Execute(w http.ResponseWriter, data interface{})
|
Execute(w http.ResponseWriter, data interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func StaticTemplate(templatePath ...string) http.HandlerFunc {
|
func StaticController(templatePath ...string) http.HandlerFunc {
|
||||||
tpl := views.Must(views.FromFS(templates.FS, templatePath...))
|
tpl := views.Must(views.FromFS(templates.FS, templatePath...))
|
||||||
|
|
||||||
var testWriter strings.Builder
|
var testWriter strings.Builder
|
||||||
|
|||||||
@ -12,20 +12,21 @@ import (
|
|||||||
|
|
||||||
type Users struct {
|
type Users struct {
|
||||||
Templates struct {
|
Templates struct {
|
||||||
New Template
|
Signup Template
|
||||||
|
Signin Template
|
||||||
}
|
}
|
||||||
UserService *models.UserService
|
UserService *models.UserService
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u Users) New(w http.ResponseWriter, r *http.Request) {
|
func (u Users) GetSignup(w http.ResponseWriter, r *http.Request) {
|
||||||
var data struct {
|
var data struct {
|
||||||
Email string
|
Email string
|
||||||
}
|
}
|
||||||
data.Email = r.FormValue("email")
|
data.Email = r.FormValue("email")
|
||||||
u.Templates.New.Execute(w, data)
|
u.Templates.Signup.Execute(w, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u Users) Create(w http.ResponseWriter, r *http.Request) {
|
func (u Users) PostSignup(w http.ResponseWriter, r *http.Request) {
|
||||||
email := r.FormValue("email")
|
email := r.FormValue("email")
|
||||||
password := r.FormValue("password")
|
password := r.FormValue("password")
|
||||||
user, err := u.UserService.Create(email, password)
|
user, err := u.UserService.Create(email, password)
|
||||||
@ -37,17 +38,35 @@ func (u Users) Create(w http.ResponseWriter, r *http.Request) {
|
|||||||
fmt.Fprintf(w, "User created: %+v", user)
|
fmt.Fprintf(w, "User created: %+v", user)
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithStaticTemplate(user_service *models.UserService, templatePath ...string) Users {
|
func (u Users) GetSignin(w http.ResponseWriter, r *http.Request) {
|
||||||
tpl := views.Must(views.FromFS(templates.FS, templatePath...))
|
var data struct {
|
||||||
|
Email string
|
||||||
|
}
|
||||||
|
data.Email = r.FormValue("email")
|
||||||
|
u.Templates.Signin.Execute(w, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithTemplates(user_service *models.UserService, signup Template, signin Template) Users {
|
||||||
|
u := Users{}
|
||||||
|
u.Templates.Signup = signup
|
||||||
|
u.Templates.Signin = signin
|
||||||
|
u.UserService = user_service
|
||||||
|
return u
|
||||||
|
}
|
||||||
|
|
||||||
|
func Default(user_service *models.UserService, templatePath ...string) Users {
|
||||||
|
signup_tpl := views.Must(views.FromFS(templates.FS, "signup.gohtml", "tailwind.gohtml"))
|
||||||
|
signin_tpl := views.Must(views.FromFS(templates.FS, "signin.gohtml", "tailwind.gohtml"))
|
||||||
|
|
||||||
var testWriter strings.Builder
|
var testWriter strings.Builder
|
||||||
err := tpl.ExecuteWriter(&testWriter, nil)
|
err := signup_tpl.ExecuteWriter(&testWriter, nil)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
err = signin_tpl.ExecuteWriter(&testWriter, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
u := Users{}
|
return WithTemplates(user_service, signup_tpl, signin_tpl)
|
||||||
u.Templates.New = tpl
|
|
||||||
u.UserService = user_service
|
|
||||||
return u
|
|
||||||
}
|
}
|
||||||
|
|||||||
13
main.go
13
main.go
@ -37,15 +37,18 @@ func main() {
|
|||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
userService := models.UserService{DB: db}
|
userService := models.UserService{DB: db}
|
||||||
var usersCtrlr ctrlrs.Users = ctrlrs.WithStaticTemplate(&userService, "signup.gohtml", "tailwind.gohtml")
|
var usersCtrlr ctrlrs.Users = ctrlrs.Default(&userService)
|
||||||
|
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
r.Use(middleware.Logger)
|
r.Use(middleware.Logger)
|
||||||
r.Get("/", ctrlrs.StaticTemplate("home.gohtml", "tailwind.gohtml"))
|
r.Get("/", ctrlrs.StaticController("home.gohtml", "tailwind.gohtml"))
|
||||||
r.Get("/contact", ctrlrs.StaticTemplate("contact.gohtml", "tailwind.gohtml"))
|
r.Get("/contact", ctrlrs.StaticController("contact.gohtml", "tailwind.gohtml"))
|
||||||
r.Get("/faq", ctrlrs.FAQ("faq.gohtml", "tailwind.gohtml"))
|
r.Get("/faq", ctrlrs.FAQ("faq.gohtml", "tailwind.gohtml"))
|
||||||
r.Get("/signup", usersCtrlr.New)
|
|
||||||
r.Post("/signup", usersCtrlr.Create)
|
r.Get("/signup", usersCtrlr.GetSignup)
|
||||||
|
r.Post("/signup", usersCtrlr.PostSignup)
|
||||||
|
r.Get("/signin", usersCtrlr.GetSignin)
|
||||||
|
|
||||||
r.NotFound(notFoundHandler)
|
r.NotFound(notFoundHandler)
|
||||||
fmt.Println("Starting the server on :3000...")
|
fmt.Println("Starting the server on :3000...")
|
||||||
http.ListenAndServe(":3000", r)
|
http.ListenAndServe(":3000", r)
|
||||||
|
|||||||
64
templates/signin.gohtml
Normal file
64
templates/signin.gohtml
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
{{template "head" .}}
|
||||||
|
<body class="min-h-screen bg-gray-100">
|
||||||
|
{{template "header".}}
|
||||||
|
<main class="py-12 flex justify-center">
|
||||||
|
<div class="px-8 py-8 bg-white rounded shadow">
|
||||||
|
<h1 class="pt-4 pb-8 text-center text-3xl font-bold text-gray-900">
|
||||||
|
Welcome back!
|
||||||
|
</h1>
|
||||||
|
<form action="/signin" method="post">
|
||||||
|
<div class="py-2">
|
||||||
|
<label for="email" class="text-sm font-semibold text-gray-800">
|
||||||
|
Email Address
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
name="email"
|
||||||
|
id="email"
|
||||||
|
type="email"
|
||||||
|
placeholder="Email address"
|
||||||
|
required
|
||||||
|
autocomplete="email"
|
||||||
|
class="w-full px-3 py-2 border border-gray-300 placeholder-gray-500
|
||||||
|
text-gray-800 rounded"
|
||||||
|
value="{{.Email}}"
|
||||||
|
{{if not .Email}}autofocus{{end}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div class="py-2">
|
||||||
|
<label for="password" class="text-sm font-semibold text-gray-800">
|
||||||
|
Password
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
name="password"
|
||||||
|
id="password"
|
||||||
|
type="password"
|
||||||
|
placeholder="Password"
|
||||||
|
required
|
||||||
|
class="w-full px-3 py-2 border border-gray-300 placeholder-gray-500
|
||||||
|
text-gray-800 rounded"
|
||||||
|
{{if .Email}}autofocus{{end}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div class="py-4">
|
||||||
|
<button class="w-full py-4 px-2 bg-indigo-600 hover:bg-indigo-700
|
||||||
|
text-white rounded font-bold text-lg">
|
||||||
|
Sign in
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="py-2 w-full flex justify-between">
|
||||||
|
<p class="text-xs text-gray-500">
|
||||||
|
Need an account?
|
||||||
|
<a href="/signup" class="underline">Sign up</a>
|
||||||
|
</p>
|
||||||
|
<p class="text-xs text-gray-500">
|
||||||
|
<a href="/reset-pw" class="underline">Forgot your password?</a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
{{template "footer" .}}
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@ -16,7 +16,7 @@
|
|||||||
<a class="text-base font-semibold hover:text-blue-100 pr-8" href="/faq">FAQ</a>
|
<a class="text-base font-semibold hover:text-blue-100 pr-8" href="/faq">FAQ</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="space-x-4">
|
<div class="space-x-4">
|
||||||
<a href="#">Sign in</a>
|
<a href="/signin">Sign in</a>
|
||||||
<a href="/signup" clss="px-4 py-2 bg-blue-700 hover:bg-blue-600 rounded">Sign up</a>
|
<a href="/signup" clss="px-4 py-2 bg-blue-700 hover:bg-blue-600 rounded">Sign up</a>
|
||||||
</div>
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user