really basic login
This commit is contained in:
parent
177a26a2e9
commit
539a36dffe
6 changed files with 116 additions and 50 deletions
|
@ -23,3 +23,21 @@ func (db *Database) Initialize() {
|
||||||
migrateInitial(orm)
|
migrateInitial(orm)
|
||||||
db.ORM = orm
|
db.ORM = orm
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *Database) CreateUser(username string, password string) error {
|
||||||
|
user := User{Username: username, Password: password}
|
||||||
|
result := db.ORM.Create(&user)
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *Database) LoginUser(username string, password string) (bool, error) {
|
||||||
|
user := User{Username: username, Password: password}
|
||||||
|
result := db.ORM.Where("username = ? AND password = ?", username, password).Find(&user)
|
||||||
|
if result.Error != nil {
|
||||||
|
return false, result.Error
|
||||||
|
}
|
||||||
|
if result.RowsAffected == 1 {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
|
@ -9,10 +9,13 @@
|
||||||
{{template "navbar" .}}
|
{{template "navbar" .}}
|
||||||
|
|
||||||
<div class="position-absolute top-50 start-50 translate-middle text-center">
|
<div class="position-absolute top-50 start-50 translate-middle text-center">
|
||||||
<div>Welcome to the {{ .title }}.</div>
|
{{if .loggedIn}}
|
||||||
<div>
|
<div>Welcome to the {{ .title }}.</div>
|
||||||
<a class="link-secondary" href="/login">Login</a> to continue.
|
{{else}}
|
||||||
</div>
|
<div>
|
||||||
|
<a class="link-secondary" href="/login">Login</a> to continue.
|
||||||
|
</div>
|
||||||
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{{template "scripts" .}}
|
{{template "scripts" .}}
|
||||||
|
@ -42,9 +45,18 @@
|
||||||
</button>
|
</button>
|
||||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||||
<ul class="ms-auto navbar-nav mb-2 mb-lg-0">
|
<ul class="ms-auto navbar-nav mb-2 mb-lg-0">
|
||||||
<li class="nav-item">
|
{{if .loggedIn}}
|
||||||
<a class="btn btn-primary" href="/login">Login</a>
|
<li class="nav-item me-2">
|
||||||
</li>
|
<a class="btn btn-primary" href="/tasks">Tasks</a>
|
||||||
|
</li>
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="btn btn-danger" href="/logout">Logout</a>
|
||||||
|
</li>
|
||||||
|
{{else}}
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="btn btn-primary" href="/login">Login</a>
|
||||||
|
</li>
|
||||||
|
{{end}}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -101,5 +113,9 @@
|
||||||
form.classList.add('was-validated');
|
form.classList.add('was-validated');
|
||||||
}, false);
|
}, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function redirect(location) {
|
||||||
|
window.location.href = location;
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
|
@ -17,13 +17,15 @@
|
||||||
|
|
||||||
{{template "scripts" .}}
|
{{template "scripts" .}}
|
||||||
<script>
|
<script>
|
||||||
function submitForm(formData) {
|
async function submitForm(formData) {
|
||||||
fetch("/login", {method: 'POST', body: formData, redirect: 'follow'})
|
const response = await fetch("/login", {method: 'POST', body: formData, redirect: 'follow'});
|
||||||
.then(response => response.json())
|
if (response.ok) {
|
||||||
.then((json) => {
|
const json = await response.json();
|
||||||
console.log(json)
|
console.log("JSON:", json.message);
|
||||||
}
|
redirect("/");
|
||||||
).catch(error => console.log(error));
|
} else {
|
||||||
|
redirect("/login");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -18,13 +18,15 @@
|
||||||
|
|
||||||
{{template "scripts" .}}
|
{{template "scripts" .}}
|
||||||
<script>
|
<script>
|
||||||
function submitForm(formData) {
|
async function submitForm(formData) {
|
||||||
fetch("/register", {method: 'POST', body: formData, redirect: 'follow'})
|
const response = await fetch("/register", {method: 'POST', body: formData, redirect: 'follow'});
|
||||||
.then(response => response.json())
|
if (response.ok) {
|
||||||
.then(() => {
|
const json = await response.json();
|
||||||
window.location.href = "/";
|
console.log("JSON:", json.message);
|
||||||
}
|
redirect("/");
|
||||||
).catch(error => console.log(error));
|
} else {
|
||||||
|
redirect("/register");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Webpage struct {
|
type Webpage struct {
|
||||||
Database database.Database
|
Database database.Database
|
||||||
Router *gin.Engine
|
Router *gin.Engine
|
||||||
|
isLoggedIn bool
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,66 +2,94 @@ package webpage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"app/database"
|
"app/database"
|
||||||
"fmt"
|
|
||||||
"github.com/gin-contrib/static"
|
"github.com/gin-contrib/static"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (wp *Webpage) redirectHome(c *gin.Context) {
|
||||||
|
c.Redirect(http.StatusTemporaryRedirect, "/")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wp *Webpage) userIsLoggedIn(c *gin.Context) bool {
|
||||||
|
if wp.isLoggedIn {
|
||||||
|
wp.redirectHome(c)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func (wp *Webpage) defineRoutes() {
|
func (wp *Webpage) defineRoutes() {
|
||||||
|
|
||||||
wp.Router.GET("/", func(c *gin.Context) {
|
wp.Router.GET("/", func(c *gin.Context) {
|
||||||
c.HTML(http.StatusOK, "index.tmpl", gin.H{
|
c.HTML(http.StatusOK, "index.tmpl", gin.H{
|
||||||
"title": "SuperSafe",
|
"title": "SuperSafe",
|
||||||
|
"loggedIn": wp.isLoggedIn,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
wp.Router.GET("/login", func(c *gin.Context) {
|
wp.Router.GET("/login", func(c *gin.Context) {
|
||||||
|
if wp.userIsLoggedIn(c) {
|
||||||
|
return
|
||||||
|
}
|
||||||
c.HTML(http.StatusOK, "login.tmpl", gin.H{
|
c.HTML(http.StatusOK, "login.tmpl", gin.H{
|
||||||
"title": "Login",
|
"title": "Login",
|
||||||
|
"loggedIn": wp.isLoggedIn,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
wp.Router.GET("/logout", func(c *gin.Context) {
|
||||||
|
wp.isLoggedIn = false
|
||||||
|
wp.redirectHome(c)
|
||||||
|
})
|
||||||
|
|
||||||
wp.Router.GET("/register", func(c *gin.Context) {
|
wp.Router.GET("/register", func(c *gin.Context) {
|
||||||
|
wp.userIsLoggedIn(c)
|
||||||
c.HTML(http.StatusOK, "register.tmpl", gin.H{
|
c.HTML(http.StatusOK, "register.tmpl", gin.H{
|
||||||
"title": "Register",
|
"title": "Register",
|
||||||
|
"loggedIn": wp.isLoggedIn,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
wp.Router.GET("/health", func(c *gin.Context) {
|
|
||||||
currentTime := time.Now().UnixMilli()
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"timestamp": currentTime,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
wp.Router.NoRoute(func(c *gin.Context) {
|
|
||||||
c.Redirect(http.StatusTemporaryRedirect, "/")
|
|
||||||
})
|
|
||||||
|
|
||||||
wp.Router.POST("/login", func(c *gin.Context) {
|
wp.Router.POST("/login", func(c *gin.Context) {
|
||||||
username, existing := c.GetPostForm("username")
|
username, uExisting := c.GetPostForm("username")
|
||||||
password, existing := c.GetPostForm("password")
|
password, pExisting := c.GetPostForm("password")
|
||||||
if existing == false || username == "" || password == "" {
|
if uExisting == false || pExisting == false || username == "" || password == "" {
|
||||||
c.JSON(400, gin.H{"message": "bad post form"})
|
c.JSON(400, gin.H{"message": "bad post form"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(username, password)
|
success, err := wp.Database.LoginUser(username, password)
|
||||||
c.JSON(200, gin.H{"message": "login successfull"})
|
if err != nil {
|
||||||
|
c.JSON(500, gin.H{"message": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if success == true {
|
||||||
|
wp.isLoggedIn = true
|
||||||
|
c.JSON(200, gin.H{"message": "login successfull"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
wp.isLoggedIn = false
|
||||||
|
c.JSON(401, gin.H{"message": "user or password not found"})
|
||||||
})
|
})
|
||||||
|
|
||||||
wp.Router.POST("/register", func(c *gin.Context) {
|
wp.Router.POST("/register", func(c *gin.Context) {
|
||||||
username, existing := c.GetPostForm("username")
|
username, uExisting := c.GetPostForm("username")
|
||||||
password, existing := c.GetPostForm("password")
|
password, pExisting := c.GetPostForm("password")
|
||||||
if existing == false || username == "" || password == "" {
|
if uExisting == false || pExisting == false || username == "" || password == "" {
|
||||||
c.JSON(400, gin.H{"message": "bad post form"})
|
c.JSON(400, gin.H{"message": "bad post form"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user := database.User{Username: username, Password: password}
|
err := wp.Database.CreateUser(username, password)
|
||||||
result := wp.Database.ORM.Create(&user)
|
if err != nil {
|
||||||
if result.Error != nil {
|
c.JSON(500, gin.H{"message": err.Error()})
|
||||||
c.JSON(400, gin.H{"message": result.Error.Error()})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(200, gin.H{"message": "user registered"})
|
c.JSON(200, gin.H{"message": "user registered"})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
wp.Router.NoRoute(func(c *gin.Context) {
|
||||||
|
wp.redirectHome(c)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wp *Webpage) initialize() {
|
func (wp *Webpage) initialize() {
|
||||||
|
@ -73,7 +101,6 @@ func (wp *Webpage) Run() {
|
||||||
wp.initialize()
|
wp.initialize()
|
||||||
wp.Router = gin.New()
|
wp.Router = gin.New()
|
||||||
wp.Router.Use(gin.Recovery())
|
wp.Router.Use(gin.Recovery())
|
||||||
wp.Router.Use(gin.Logger())
|
|
||||||
wp.Router.SetTrustedProxies(nil)
|
wp.Router.SetTrustedProxies(nil)
|
||||||
wp.Router.Use(static.Serve("/static", static.LocalFile("./static", false)))
|
wp.Router.Use(static.Serve("/static", static.LocalFile("./static", false)))
|
||||||
wp.Router.LoadHTMLGlob("templates/*")
|
wp.Router.LoadHTMLGlob("templates/*")
|
||||||
|
|
Reference in a new issue