Login System Tutorial Part 2 - Login Form (Current Progress)
============================================================
`Back to Previous Page`_
.. _Back to Previous Page: https://uadmin-docs.readthedocs.io/en/latest/login_system_views/tutorial/part2.html
Structure:
* `templates`_
* `login.html`_
* `views`_
* `login.go`_
* `views_main.go`_
* `main.go`_
templates
---------
**login.html**
^^^^^^^^^^^^^^
`Back to Top`_
.. code-block:: html
Login Form
views
-----
**login.go**
^^^^^^^^^^^^
`Back to Top`_
.. code-block:: go
package views
import (
"net/http"
"github.com/uadmin/uadmin"
)
// LoginHandler verifies login data and creating sessions for users.
func LoginHandler(w http.ResponseWriter, r *http.Request) {
// Initialize the fields that we need in the custom struct.
type Context struct {
Err string
ErrExists bool
OTPRequired bool
Username string
Password string
}
// Call the Context struct.
c := Context{}
// Render the login filepath and pass the context data object to the HTML file.
uadmin.RenderHTML(w, r, "templates/login.html", c)
}
**views_main.go**
^^^^^^^^^^^^^^^^^^^
`Back to Top`_
.. code-block:: go
package views
import (
"net/http"
"strings"
)
// MainHandler is the main handler for the login system.
func MainHandler(w http.ResponseWriter, r *http.Request) {
// r.URL.Path creates a new path called "/login_system/"
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/login_system")
r.URL.Path = strings.TrimSuffix(r.URL.Path, "/")
// LoginHandler verifies login data and creating sessions for users.
LoginHandler(w, r)
}
main.go
-------
`Back to Top`_
.. _Back To Top: https://uadmin-docs.readthedocs.io/en/latest/login_system_views/tutorial/full_code/part2.html#login-system-tutorial-part-2-login-form-current-progress
.. code-block:: go
package main
import (
"net/http"
// Specify the username that you used inside github.com folder
"github.com/username/project_name/views"
"github.com/uadmin/uadmin"
)
func main() {
// Assign RootURL value as "/admin/" and Site Name as "Login System"
// NOTE: This code works only if database does not exist yet.
uadmin.RootURL = "/admin/"
uadmin.SiteName = "Login System"
// Login System Main Handler
http.HandleFunc("/login_system/", uadmin.Handler(views.MainHandler))
// Run the server
uadmin.StartServer()
}