added user signup feature
This commit is contained in:
8
front/package-lock.json
generated
8
front/package-lock.json
generated
@@ -57,6 +57,7 @@
|
||||
"integrity": "sha512-2BCOP7TN8M+gVDj7/ht3hsaO/B/n5oDbiAyyvnRlNOs+u1o+JWNYTQrmpuNp1/Wq2gcFrI01JAW+paEKDMx/CA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"@babel/code-frame": "^7.27.1",
|
||||
"@babel/generator": "^7.28.3",
|
||||
@@ -1823,6 +1824,7 @@
|
||||
"integrity": "sha512-NZyJarBfL7nWwIq+FDL6Zp/yHEhePMNnnJ0y3qfieCrmNvYct8uvtiV41UvlSe6apAfk0fY1FbWx+NwfmpvtTg==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"bin": {
|
||||
"acorn": "bin/acorn"
|
||||
},
|
||||
@@ -1945,6 +1947,7 @@
|
||||
}
|
||||
],
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"caniuse-lite": "^1.0.30001737",
|
||||
"electron-to-chromium": "^1.5.211",
|
||||
@@ -2274,6 +2277,7 @@
|
||||
"integrity": "sha512-QePbBFMJFjgmlE+cXAlbHZbHpdFVS2E/6vzCy7aKlebddvl1vadiC4JFV5u/wqTkNUwEV8WrQi257jf5f06hrg==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"@eslint-community/eslint-utils": "^4.8.0",
|
||||
"@eslint-community/regexpp": "^4.12.1",
|
||||
@@ -2335,6 +2339,7 @@
|
||||
"integrity": "sha512-82GZUjRS0p/jganf6q1rEO25VSoHH0hKPCTrgillPjdI/3bgBhAE1QzHrHTizjpRvy6pGAvKjDJtk2pF9NDq8w==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"bin": {
|
||||
"eslint-config-prettier": "bin/cli.js"
|
||||
},
|
||||
@@ -3374,6 +3379,7 @@
|
||||
"integrity": "sha512-I7AIg5boAr5R0FFtJ6rCfD+LFsWHp81dolrFD8S79U9tb8Az2nGrJncnMSnys+bpQJfRUzqs9hnA81OAA3hCuQ==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"bin": {
|
||||
"prettier": "bin/prettier.cjs"
|
||||
},
|
||||
@@ -3769,6 +3775,7 @@
|
||||
"integrity": "sha512-4cKBO9wR75r0BeIWWWId9XK9Lj6La5X846Zw9dFfzMRw38IlTk2iCcUt6hsyiDRcPidc55ZParFYDXi0nXOeLQ==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"esbuild": "^0.25.0",
|
||||
"fdir": "^6.5.0",
|
||||
@@ -3955,6 +3962,7 @@
|
||||
"resolved": "https://registry.npmjs.org/vue/-/vue-3.5.21.tgz",
|
||||
"integrity": "sha512-xxf9rum9KtOdwdRkiApWL+9hZEMWE90FHh8yS1+KJAiWYh+iGWV1FquPjoO9VUHQ+VIhsCXNNyZ5Sf4++RVZBA==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"@vue/compiler-dom": "3.5.21",
|
||||
"@vue/compiler-sfc": "3.5.21",
|
||||
|
||||
@@ -31,9 +31,9 @@
|
||||
<div class="navbar-end">
|
||||
<div class="navbar-item">
|
||||
<div class="buttons">
|
||||
<a class="button is-primary">
|
||||
<RouterLink to="/signup" class="button is-primary">
|
||||
<strong>Sign up</strong>
|
||||
</a>
|
||||
</RouterLink>
|
||||
<a class="button is-light">
|
||||
Log in
|
||||
</a>
|
||||
|
||||
71
front/src/SignUp.vue
Normal file
71
front/src/SignUp.vue
Normal file
@@ -0,0 +1,71 @@
|
||||
<script setup>
|
||||
import { ref, reactive, computed } from 'vue'
|
||||
import { postBook, postSignup } from './api.js'
|
||||
import { useRouter, useRoute } from 'vue-router'
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
const user = ref({
|
||||
username: "",
|
||||
password: ""
|
||||
});
|
||||
const errors = ref(null)
|
||||
const userError = computed(() => {
|
||||
return extractErrorFromField("Username");
|
||||
})
|
||||
const passwordError = computed(() => {
|
||||
return extractErrorFromField("Password");
|
||||
})
|
||||
|
||||
function extractErrorFromField(fieldName) {
|
||||
if (errors.value === null) {
|
||||
return "";
|
||||
}
|
||||
const titleErr = errors.value.find((e) => e["field"] === fieldName);
|
||||
if (typeof titleErr !== 'undefined') {
|
||||
return titleErr.error
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
function onSubmit(e) {
|
||||
postSignup(user)
|
||||
.then((res) => {
|
||||
if (res.ok) {
|
||||
router.push('/');
|
||||
return;
|
||||
} else {
|
||||
res.json().then((json) => (errors.value = json));
|
||||
}
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<form @submit.prevent="onSubmit">
|
||||
<div class="field">
|
||||
<label class="label">Username</label>
|
||||
<div class="control">
|
||||
<input :class="'input ' + (userError ? 'is-danger' : '')" type="text" minlength="2" maxlength="20"
|
||||
required v-model="user.username" placeholder="Username">
|
||||
</div>
|
||||
<p v-if="userError" class="help is-danger">{{userError}}</p>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label class="label">Password</label>
|
||||
<div class="control">
|
||||
<input :class="'input ' + (passwordError ? 'is-danger' : '')" type="password" minlength="6"
|
||||
maxlength="100" v-model="user.password" required placeholder="Password">
|
||||
</div>
|
||||
<p v-if="passwordError" class="help is-danger">{{passwordError}}</p>
|
||||
</div>
|
||||
<div class="field">
|
||||
<div class="control">
|
||||
<button class="button is-link">Submit</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</template>
|
||||
|
||||
<style scoped></style>
|
||||
@@ -27,3 +27,13 @@ export function postBook(book) {
|
||||
body: JSON.stringify(book.value)
|
||||
})
|
||||
}
|
||||
|
||||
export function postSignup(user) {
|
||||
return fetch(baseUrl + '/auth/signup', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(user.value)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,11 +3,13 @@ import { createRouter, createWebHistory } from 'vue-router'
|
||||
import App from './App.vue'
|
||||
import BooksBrowser from './BooksBrowser.vue'
|
||||
import AddBook from './AddBook.vue'
|
||||
import SignUp from './SignUp.vue'
|
||||
|
||||
|
||||
const routes = [
|
||||
{ path: '/', component: BooksBrowser },
|
||||
{ path: '/add', component: AddBook },
|
||||
{ path: '/signup', component: SignUp },
|
||||
]
|
||||
|
||||
export const router = createRouter({
|
||||
|
||||
@@ -5,3 +5,8 @@ type bookPostCreate struct {
|
||||
Author string `json:"author" binding:"max=100"`
|
||||
Rating int `json:"rating" binding:"min=0,max=10"`
|
||||
}
|
||||
|
||||
type userPostCreate struct {
|
||||
Username string `json:"username" binding:"required,min=2,max=20"`
|
||||
Password string `json:"password" binding:"required,min=6,max=100"`
|
||||
}
|
||||
|
||||
@@ -1,9 +1,27 @@
|
||||
package api
|
||||
|
||||
import "git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
import (
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func (b bookPostCreate) toBook() model.Book {
|
||||
return model.Book{Title: b.Title,
|
||||
return model.Book{
|
||||
Title: b.Title,
|
||||
Author: b.Author,
|
||||
Rating: b.Rating}
|
||||
Rating: b.Rating,
|
||||
}
|
||||
}
|
||||
|
||||
func (u userPostCreate) toUser() (model.User, error) {
|
||||
user := model.User{
|
||||
Name: u.Username,
|
||||
Password: "",
|
||||
}
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return user, err
|
||||
}
|
||||
user.Password = string(hashedPassword)
|
||||
return user, nil
|
||||
}
|
||||
|
||||
@@ -20,15 +20,47 @@ func PostBookHandler(c *gin.Context, db *gorm.DB) {
|
||||
var book bookPostCreate
|
||||
err := c.ShouldBindJSON(&book)
|
||||
if err != nil {
|
||||
var ve validator.ValidationErrors
|
||||
if errors.As(err, &ve) {
|
||||
c.JSON(http.StatusBadRequest, getValidationErrors(&ve))
|
||||
} else {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
}
|
||||
manageBindingError(c, err)
|
||||
return
|
||||
}
|
||||
bookDb := book.toBook()
|
||||
db.Model(&model.Book{}).Save(&bookDb)
|
||||
err = db.Model(&model.Book{}).Save(&bookDb).Error
|
||||
if err != nil {
|
||||
manageDefaultError(c, err)
|
||||
return
|
||||
}
|
||||
c.String(200, "Success")
|
||||
}
|
||||
|
||||
func PostUserHandler(c *gin.Context, db *gorm.DB) {
|
||||
var user userPostCreate
|
||||
err := c.ShouldBindJSON(&user)
|
||||
if err != nil {
|
||||
manageBindingError(c, err)
|
||||
return
|
||||
}
|
||||
userDb, err := user.toUser()
|
||||
if err != nil {
|
||||
manageDefaultError(c, err)
|
||||
return
|
||||
}
|
||||
err = db.Model(&model.User{}).Save(&userDb).Error
|
||||
if err != nil {
|
||||
manageDefaultError(c, err)
|
||||
return
|
||||
}
|
||||
c.String(200, "Success")
|
||||
}
|
||||
|
||||
func manageBindingError(c *gin.Context, err error) {
|
||||
var ve validator.ValidationErrors
|
||||
if errors.As(err, &ve) {
|
||||
c.JSON(http.StatusBadRequest, getValidationErrors(&ve))
|
||||
} else {
|
||||
manageDefaultError(c, err)
|
||||
}
|
||||
}
|
||||
|
||||
func manageDefaultError(c *gin.Context, err error) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@ func computeValidationMessage(fe *validator.FieldError) string {
|
||||
switch tag {
|
||||
case "required":
|
||||
return fmt.Sprintf("%s is required.", (*fe).Field())
|
||||
case "min":
|
||||
return fmt.Sprintf("%s is not long enough. It should be at least %s characters.", (*fe).Field(), (*fe).Param())
|
||||
case "max":
|
||||
return fmt.Sprintf("%s is too long. It should be under %s characters.", (*fe).Field(), (*fe).Param())
|
||||
default:
|
||||
|
||||
@@ -19,6 +19,7 @@ func Initdb(databasePath string, demoDataPath string) *gorm.DB {
|
||||
}
|
||||
// Migrate the schema
|
||||
db.AutoMigrate(&model.Book{})
|
||||
db.AutoMigrate(&model.User{})
|
||||
var book model.Book
|
||||
queryResult := db.Limit(1).Find(&book)
|
||||
if queryResult.RowsAffected == 0 && demoDataPath != "" {
|
||||
|
||||
@@ -4,7 +4,7 @@ import "gorm.io/gorm"
|
||||
|
||||
type Book struct {
|
||||
gorm.Model
|
||||
Title string `json:"title"`
|
||||
Title string `json:"title" gorm:"not null"`
|
||||
Author string `json:"author"`
|
||||
Rating int `json:"rating"`
|
||||
Rating int `json:"rating"`
|
||||
}
|
||||
9
internal/model/user.go
Normal file
9
internal/model/user.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package model
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type User struct {
|
||||
gorm.Model
|
||||
Name string `gorm:"index;uniqueIndex"`
|
||||
Password string
|
||||
}
|
||||
3
main.go
3
main.go
@@ -25,5 +25,8 @@ func setup(config *config.Config) *gin.Engine {
|
||||
r.POST("/book", func(c *gin.Context) {
|
||||
api.PostBookHandler(c, db)
|
||||
})
|
||||
r.POST("/auth/signup", func(c *gin.Context) {
|
||||
api.PostUserHandler(c, db)
|
||||
})
|
||||
return r
|
||||
}
|
||||
|
||||
67
user_test.go
Normal file
67
user_test.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPostUserHandler_Working(t *testing.T) {
|
||||
userJson :=
|
||||
`{
|
||||
"username": "artlef",
|
||||
"password": "123456789"
|
||||
}`
|
||||
testPostUserHandler(t, userJson, 200)
|
||||
}
|
||||
|
||||
func TestPostUserHandler_UsernameTooSmall(t *testing.T) {
|
||||
userJson :=
|
||||
`{
|
||||
"username": "a",
|
||||
"password": "123456789"
|
||||
}`
|
||||
testPostUserHandler(t, userJson, 400)
|
||||
}
|
||||
|
||||
func TestPostUserHandler_UsernameTooBig(t *testing.T) {
|
||||
userJson :=
|
||||
`{
|
||||
"username": "thisusernameistoolong",
|
||||
"password": "123456789"
|
||||
}`
|
||||
testPostUserHandler(t, userJson, 400)
|
||||
}
|
||||
|
||||
func TestPostUserHandler_PasswordTooSmall(t *testing.T) {
|
||||
userJson :=
|
||||
`{
|
||||
"username": "thisusernameisok",
|
||||
"password": "lol"
|
||||
}`
|
||||
testPostUserHandler(t, userJson, 400)
|
||||
}
|
||||
|
||||
func TestPostUserHandler_PasswordTooBig(t *testing.T) {
|
||||
userJson :=
|
||||
`{
|
||||
"username": "thisusernameisok",
|
||||
"password": "According to all known laws of aviation, there is no way a bee should be able to fly. Its wings are too small to get its fat little body off the ground."
|
||||
}`
|
||||
testPostUserHandler(t, userJson, 400)
|
||||
}
|
||||
|
||||
func testPostUserHandler(t *testing.T, userJson string, expectedCode int) {
|
||||
router := testSetup()
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
req, _ := http.NewRequest("POST", "/auth/signup",
|
||||
strings.NewReader(string(userJson)))
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, expectedCode, w.Code)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user