Skip to content

Commit

Permalink
create login page and /user/login api
Browse files Browse the repository at this point in the history
  • Loading branch information
ErfiDev committed Nov 30, 2021
1 parent f600e44 commit 68f9413
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
18 changes: 17 additions & 1 deletion controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ func (r Repository) Login(res http.ResponseWriter , req *http.Request) {
"path": "/login",
"title": "login in account",
},
Form: forms.New(nil),
})
}

Expand Down Expand Up @@ -439,6 +440,21 @@ func (r Repository) LoginPost(res http.ResponseWriter , req *http.Request) {
})
return
} else {

rawJson := make(map[string]interface{})
// authenticate
authenticate , err := r.DB.Authenticate(user.Email , user.Password)
if !authenticate {
rawJson["msg"] = err
rawJson["status"] = 403
toJson , _ := json.Marshal(rawJson)
res.Header().Add("Content-Type" , "application/json; charset=utf8")
res.Write(toJson)
} else {
rawJson["msg"] = "login successful"
rawJson["status"] = 200
toJson , _ := json.Marshal(rawJson)
res.Header().Add("Content-Type" , "application/json; charset=utf8")
res.Write(toJson)
}
}
}
2 changes: 1 addition & 1 deletion routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Routes() http.Handler {
router.Post("/book-now" , controllers.Repo.BookNowPost)
router.Post("/make-reservation" , controllers.Repo.MakeReservationPost)
router.Post("/search-availability" , controllers.Repo.SearchAvailability)
router.Post("/login" , controllers.Repo.LoginPost)
router.Post("/user/login" , controllers.Repo.LoginPost)

// Custom 404 page
router.NotFound(NotFound)
Expand Down
57 changes: 57 additions & 0 deletions views/login.page.gohtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{{template "base" .}}

{{define "content"}}
<section class="w-100 h-100" style="display: flex;flex-direction: column;justify-content: center;align-items: center">
<form action="/user/login" method="POST" class="user-login-form">
<input type="text" hidden name="csrf_token" value="{{.CSRF}}" />

{{$user := index .Data "user"}}

<div class="form-group mt-4">
<label for="first_name">First Name:</label>
{{with .Form.Errors.Get "first_name"}}
<label class="text-danger">{{.}}</label>
{{end}}
<input class="form-control {{with .Form.Errors.Get "first_name"}} is-invalid {{end}}"
id="first_name" autocomplete="off" type='text'
name='first_name' value="{{with $user.FirstName}}{{.}}{{end}}">
</div>

<div class="form-group mt-3">
<label for="last_name">Last Name:</label>
{{with .Form.Errors.Get "last_name"}}
<label class="text-danger">{{.}}</label>
{{end}}
<input class="form-control {{with .Form.Errors.Get "last_name"}} is-invalid {{end}}"
id="last_name" autocomplete="off" type='text'
name='last_name' value="{{with $user.LastName}}{{.}}{{end}}">
</div>

<div class="form-group mt-3">
<label for="email">Email:</label>
{{with .Form.Errors.Get "email"}}
<label class="text-danger">{{.}}</label>
{{end}}
<input class="form-control {{with .Form.Errors.Get "email"}} is-invalid {{end}}" id="email"
autocomplete="off" type='email'
name='email' value="{{with $user.Email}}{{.}}{{end}}">
</div>

<div class="form-group mt-3">
<label for="phone">Password</label>
{{with .Form.Errors.Get "password"}}
<label class="text-danger">{{.}}</label>
{{end}}
<input class="form-control {{with .Form.Errors.Get "password"}} is-invalid {{end}}" id="phone"
autocomplete="off" type="password"
name='password' value="{{with $user.Password}}{{.}}{{end}}">
</div>

<hr>
<input type="submit" class="btn btn-primary mb-5" value="Make Reservation">
</form>
</section>
{{end}}

{{define "css"}}
{{end}}

0 comments on commit 68f9413

Please sign in to comment.