registering user works
This commit is contained in:
parent
faf460470e
commit
acdee880e4
4 changed files with 48 additions and 32 deletions
13
Lab01/app/.idea/saveactions_settings.xml
Normal file
13
Lab01/app/.idea/saveactions_settings.xml
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="SaveActionSettings">
|
||||||
|
<option name="actions">
|
||||||
|
<set>
|
||||||
|
<option value="activate" />
|
||||||
|
<option value="activateOnShortcut" />
|
||||||
|
<option value="organizeImports" />
|
||||||
|
<option value="reformat" />
|
||||||
|
</set>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -1,6 +1,8 @@
|
||||||
package database
|
package database
|
||||||
|
|
||||||
import "gorm.io/gorm"
|
import (
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
type Database struct {
|
type Database struct {
|
||||||
ORM *gorm.DB
|
ORM *gorm.DB
|
||||||
|
@ -8,7 +10,7 @@ type Database struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
ID int
|
Username string `gorm:"primaryKey"`
|
||||||
Username string
|
Password string
|
||||||
Password string
|
CreatedAt int64 `gorm:"autoCreateTime"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,37 +9,37 @@
|
||||||
{{template "navbar" .}}
|
{{template "navbar" .}}
|
||||||
|
|
||||||
<div class="position-absolute top-50 start-50 translate-middle">
|
<div class="position-absolute top-50 start-50 translate-middle">
|
||||||
<form action="http://localhost:8080/register" method="POST">
|
<div class="mb-3">
|
||||||
<div class="mb-3">
|
<label for="username" class="form-label">Username</label>
|
||||||
<label for="username" class="form-label">Username</label>
|
<input type="text" class="form-control" id="username" aria-describedby="emailHelp">
|
||||||
<input type="text" class="form-control" id="username" aria-describedby="emailHelp">
|
<div id="emailHelp" class="form-text">The username needs to be unique</div>
|
||||||
<div id="emailHelp" class="form-text">The username needs to be unique</div>
|
</div>
|
||||||
</div>
|
<div class="mb-3">
|
||||||
<div class="mb-3">
|
<label for="password" class="form-label">Password</label>
|
||||||
<label for="password" class="form-label">Password</label>
|
<input type="password" class="form-control" id="password" aria-describedby="passwordHelp">
|
||||||
<input type="password" class="form-control" id="password" aria-describedby="passwordHelp">
|
<div id="passwordHelp" class="form-text">The password needs to be different from the username</div>
|
||||||
<div id="passwordHelp" class="form-text">The password needs to be different from the username</div>
|
</div>
|
||||||
</div>
|
<button onclick="registerUser()" class="btn btn-primary">Submit</button>
|
||||||
<button type="submit" class="btn btn-primary">Submit</button>
|
|
||||||
</form>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
|
||||||
|
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
|
||||||
|
crossorigin="anonymous"></script>
|
||||||
<script>
|
<script>
|
||||||
let formData = new FormData();
|
function registerUser() {
|
||||||
formData.append("username", "Florian");
|
const username = document.getElementById("username").value;
|
||||||
formData.append("password", "SuperSafe");
|
const password = document.getElementById("password").value;
|
||||||
|
let formData = new FormData();
|
||||||
|
formData.append("username", username);
|
||||||
|
formData.append("password", password);
|
||||||
|
|
||||||
let requestOptions = {
|
fetch("/register", {method: 'POST', body: formData, redirect: 'follow'})
|
||||||
method: 'POST',
|
.then(response => response.json())
|
||||||
body: formData,
|
.then(() => {
|
||||||
redirect: 'follow'
|
window.location.href = "/";
|
||||||
};
|
}
|
||||||
|
).catch(error => console.log(error));
|
||||||
fetch("http://localhost:8080/register", requestOptions)
|
}
|
||||||
.then(response => response.text())
|
|
||||||
.then(result => console.log(result))
|
|
||||||
.catch(error => console.log('error', error));
|
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
|
@ -45,7 +45,8 @@ func (wp *Webpage) defineRoutes() {
|
||||||
user := database.User{Username: username, Password: password}
|
user := database.User{Username: username, Password: password}
|
||||||
result := wp.Database.ORM.Create(&user)
|
result := wp.Database.ORM.Create(&user)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
c.JSON(200, gin.H{"message": "cannot create user"})
|
c.JSON(400, gin.H{"message": result.Error.Error()})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
c.JSON(200, gin.H{"message": "user registered"})
|
c.JSON(200, gin.H{"message": "user registered"})
|
||||||
})
|
})
|
||||||
|
|
Reference in a new issue