register and login works

This commit is contained in:
2024-12-21 09:56:37 +01:00
parent 44e150891b
commit 9b1d6053a0
15 changed files with 116 additions and 226 deletions
-144
View File
@@ -1,144 +0,0 @@
package main
type CharType struct {
Alter int `json:"alter"`
Anrede string `json:"anrede"`
Ap struct {
Max int `json:"max"`
Value int `json:"value"`
} `json:"ap"`
Ausruestung []struct {
Anzahl int `json:"anzahl"`
BeinhaltetIn *string `json:"beinhaltet_in"`
Beschreibung *string `json:"beschreibung"`
Bonus int `json:"bonus,omitempty"`
Gewicht float64 `json:"gewicht"`
ID string `json:"id"`
Magisch struct {
Abw int `json:"abw"`
Ausgebrannt bool `json:"ausgebrannt"`
IstMagisch bool `json:"ist_magisch"`
} `json:"magisch"`
Name string `json:"name"`
Wert float64 `json:"wert"`
} `json:"ausruestung"`
B struct {
Max int `json:"max"`
} `json:"b"`
Behaeltnisse []struct {
BeinhaltetIn any `json:"beinhaltet_in"`
Beschreibung *string `json:"beschreibung"`
Gewicht float64 `json:"gewicht"`
ID string `json:"id"`
Magisch struct {
Abw int `json:"abw"`
Ausgebrannt bool `json:"ausgebrannt"`
IstMagisch bool `json:"ist_magisch"`
} `json:"magisch"`
Name string `json:"name"`
Tragkraft float64 `json:"tragkraft"`
Volumen float64 `json:"volumen"`
Wert float64 `json:"wert"`
} `json:"behaeltnisse"`
Bennies struct {
Gg int `json:"gg"`
Gp int `json:"gp"`
Sg int `json:"sg"`
} `json:"bennies"`
Eigenschaften struct {
Au int `json:"au"`
Gs int `json:"gs"`
Gw int `json:"gw"`
In int `json:"in"`
Ko int `json:"ko"`
Pa int `json:"pa"`
St int `json:"st"`
Wk int `json:"wk"`
Zt int `json:"zt"`
} `json:"eigenschaften"`
Erfahrungsschatz struct {
Value int `json:"value"`
} `json:"erfahrungsschatz"`
Fertigkeiten []struct {
Beschreibung *string `json:"beschreibung"`
Bonus int `json:"bonus,omitempty"`
Fertigkeitswert int `json:"fertigkeitswert"`
ID string `json:"id"`
Name string `json:"name"`
Pp int `json:"pp,omitempty"`
Quelle string `json:"quelle"`
} `json:"fertigkeiten"`
Gestalt struct {
Breite string `json:"breite"`
Groesse string `json:"groesse"`
} `json:"gestalt"`
Gewicht int `json:"gewicht"`
Glaube string `json:"glaube"`
Grad int `json:"grad"`
Groesse int `json:"groesse"`
Hand string `json:"hand"`
ID string `json:"id"`
Image string `json:"image,omitempty"`
Lp struct {
Max int `json:"max"`
Value int `json:"value"`
} `json:"lp"`
Merkmale struct {
Augenfarbe string `json:"augenfarbe"`
Haarfarbe string `json:"haarfarbe"`
Sonstige string `json:"sonstige"`
} `json:"merkmale"`
Name string `json:"name"`
Rasse string `json:"rasse"`
Spezialisierung []string `json:"spezialisierung"`
Stand string `json:"stand"`
Transportmittel []struct {
BeinhaltetIn any `json:"beinhaltet_in"`
Beschreibung *string `json:"beschreibung"`
Gewicht int `json:"gewicht"`
ID string `json:"id"`
Magisch struct {
Abw int `json:"abw"`
Ausgebrannt bool `json:"ausgebrannt"`
IstMagisch bool `json:"ist_magisch"`
} `json:"magisch"`
Name string `json:"name"`
Tragkraft float64 `json:"tragkraft"`
Wert float64 `json:"wert"`
} `json:"transportmittel"`
Typ string `json:"typ"`
Waffen []struct {
Abwb int `json:"abwb"`
Anb int `json:"anb"`
Anzahl int `json:"anzahl"`
BeinhaltetIn *string `json:"beinhaltet_in"`
Beschreibung *string `json:"beschreibung"`
Gewicht float64 `json:"gewicht"`
ID string `json:"id"`
Magisch struct {
Abw int `json:"abw"`
Ausgebrannt bool `json:"ausgebrannt"`
IstMagisch bool `json:"ist_magisch"`
} `json:"magisch"`
Name string `json:"name"`
NameFuerSpezialisierung string `json:"nameFuerSpezialisierung"`
Schb int `json:"schb"`
Wert float64 `json:"wert"`
} `json:"waffen"`
Waffenfertigkeiten []struct {
Beschreibung *string `json:"beschreibung"`
Bonus int `json:"bonus"`
Fertigkeitswert int `json:"fertigkeitswert"`
ID string `json:"id"`
Name string `json:"name"`
Pp int `json:"pp"`
Quelle string `json:"quelle"`
} `json:"waffenfertigkeiten"`
Zauber []struct {
Beschreibung any `json:"beschreibung"`
Bonus int `json:"bonus"`
ID string `json:"id"`
Name string `json:"name"`
Quelle string `json:"quelle"`
} `json:"zauber"`
}
+1 -1
View File
@@ -10,7 +10,7 @@ import (
var DB *gorm.DB
func ConnectDatabase() {
dsn := "user:password@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
dsn := "bamort:bG4)efozrc@tcp(192.168.0.5:3306)/bamort?charset=utf8mb4&parseTime=True&loc=Local"
database, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatal("Failed to connect to database:", err)
+3 -3
View File
@@ -27,7 +27,7 @@ func RegisterUser(c *gin.Context) {
return
}
c.JSON(http.StatusCreated, gin.H{"message": "User registered successfully"})
c.JSON(http.StatusCreated, gin.H{"message": "User registered successfully:"})
}
func LoginUser(c *gin.Context) {
@@ -43,12 +43,12 @@ func LoginUser(c *gin.Context) {
}
if err := DB.Where("username = ?", input.Username).First(&user).Error; err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid username or password"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid username. or password"})
return
}
if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(input.Password)); err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid username or password"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid username or password."})
return
}
+12 -1
View File
@@ -1,6 +1,7 @@
package main
import (
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
@@ -10,6 +11,16 @@ func main() {
r := gin.Default()
// Add CORS middleware
r.Use(cors.New(cors.Config{
//AllowOrigins: []string{"http://localhost:3000"}, // Replace with your frontend's URL
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE"},
AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
}))
// Routes
r.POST("/register", RegisterUser)
r.POST("/login", LoginUser)
@@ -20,5 +31,5 @@ func main() {
r.PUT("/ausruestung/:ausruestung_id", UpdateAusruestung)
r.DELETE("/ausruestung/:ausruestung_id", DeleteAusruestung)
r.Run(":8080") // Start server on port 8080
r.Run(":8180") // Start server on port 8080
}
+1 -1
View File
@@ -13,7 +13,7 @@ type Character struct {
Name string
Rasse string
Typ string
Alter int
Age int
Anrede string
Grad int
Groesse int
+2
View File
@@ -5,6 +5,8 @@ npm init vue@latest bamort
go get -u github.com/gin-gonic/gin
go get -u gorm.io/gorm
go get -u gorm.io/driver/mysql
go get github.com/gin-contrib/cors
cd bamort
npm install
+1 -9
View File
@@ -5,21 +5,13 @@ CREATE TABLE users (
email VARCHAR(100) UNIQUE NOT NULL
);
CREATE TABLE users (
user_id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL
);
CREATE TABLE characters (
character_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
name VARCHAR(100),
rasse VARCHAR(50),
typ VARCHAR(50),
alter INT,
age INT,
anrede VARCHAR(20),
grad INT,
groesse INT,
+10 -42
View File
@@ -1,47 +1,15 @@
<script setup>
import HelloWorld from './components/HelloWorld.vue'
import TheWelcome from './components/TheWelcome.vue'
</script>
<template>
<header>
<img alt="Vue logo" class="logo" src="./assets/logo.svg" width="125" height="125" />
<div class="wrapper">
<HelloWorld msg="You did it!" />
</div>
</header>
<main>
<TheWelcome />
</main>
<div id="app">
<router-view />
</div>
</template>
<style scoped>
header {
line-height: 1.5;
}
<script>
export default {
name: "App",
};
</script>
.logo {
display: block;
margin: 0 auto 2rem;
}
@media (min-width: 1024px) {
header {
display: flex;
place-items: center;
padding-right: calc(var(--section-gap) / 2);
}
.logo {
margin: 0 2rem 0 0;
}
header .wrapper {
display: flex;
place-items: flex-start;
flex-wrap: wrap;
}
}
<style>
/* Add your global styles here */
</style>
+1
View File
@@ -5,6 +5,7 @@
<input v-model="password" type="password" placeholder="Password" required />
<button type="submit">Login</button>
<p v-if="error" class="error">{{ error }}</p>
<p>Don't have an account? <router-link to="/register">Register here</router-link>.</p>
</form>
</template>
+55
View File
@@ -0,0 +1,55 @@
<template>
<form @submit.prevent="register">
<h2>Register</h2>
<input v-model="username" placeholder="Username" required />
<input v-model="email" type="email" placeholder="Email" required />
<input v-model="password" type="password" placeholder="Password" required />
<button type="submit">Register</button>
<p v-if="error" class="error">{{ error }}</p>
<p v-if="success" class="success">{{ success }}</p>
</form>
</template>
<script>
import API from "../utils/api";
export default {
data() {
return {
username: "",
email: "",
password: "",
error: "",
success: "",
};
},
methods: {
async register() {
try {
const response = await API.post("/register", {
username: this.username,
email: this.email,
passwordhash: this.password,
});
this.success = "Registration successful! You can now log in.";
this.error = "";
this.username = "";
this.email = "";
this.password = "";
} catch (err) {
this.error = err.response?.data?.error || "Registration failed.";
this.success = "";
}
},
},
};
</script>
<style>
.error {
color: red;
}
.success {
color: green;
}
</style>
+6 -3
View File
@@ -1,6 +1,9 @@
import './assets/main.css'
import { createApp } from 'vue'
import App from './App.vue'
import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
createApp(App).mount('#app')
const app = createApp(App);
app.use(router);
app.mount("#app");
-11
View File
@@ -1,11 +0,0 @@
import './assets/main.css'
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
const app = createApp(App)
app.use(createPinia())
app.mount('#app')
+12 -10
View File
@@ -1,17 +1,19 @@
import { createRouter, createWebHistory } from 'vue-router'
import LoginView from '../views/LoginView.vue'
import DashboardView from '../views/DashboardView.vue'
import AusruestungView from '../views/AusruestungView.vue'
import { createRouter, createWebHistory } from "vue-router";
import LoginView from "../views/LoginView.vue";
import RegisterView from "../views/RegisterView.vue";
import DashboardView from "../views/DashboardView.vue";
import AusruestungView from "../views/AusruestungView.vue";
const routes = [
{ path: '/', name: 'Login', component: LoginView },
{ path: '/dashboard', name: 'Dashboard', component: DashboardView },
{ path: '/ausruestung/:characterId', name: 'Ausruestung', component: AusruestungView },
]
{ path: "/", name: "Login", component: LoginView },
{ path: "/register", name: "Register", component: RegisterView },
{ path: "/dashboard", name: "Dashboard", component: DashboardView },
{ path: "/ausruestung/:characterId", name: "Ausruestung", component: AusruestungView },
];
const router = createRouter({
history: createWebHistory(),
routes,
})
});
export default router
export default router;
+1 -1
View File
@@ -1,7 +1,7 @@
import axios from 'axios'
const API = axios.create({
baseURL: 'http://localhost:8080', // Replace with your backend URL
baseURL: 'http://localhost:8180', // Replace with your backend URL
})
export default API
+11
View File
@@ -0,0 +1,11 @@
<template>
<RegisterForm />
</template>
<script>
import RegisterForm from "../components/RegisterForm.vue";
export default {
components: { RegisterForm },
};
</script>