Skip to content

Commit 24cedce

Browse files
committed
Login and register functions
- Login / Register functions and routes configured - Configured build for linux too ( in future make it work differently with GOPATH variable
1 parent 41d3a2f commit 24cedce

File tree

7 files changed

+65
-5
lines changed

7 files changed

+65
-5
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
run:
2-
CompileDaemon -command="./homelab-backend"
2+
CompileDaemon -command="./homelab-backend"
3+
4+
run linux:
5+
/home/kaizer/go/bin/CompileDaemon -command="./homelab-backend"

controllers/Devices.controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func GetDeviceById(c *gin.Context) {
2222

2323
id := c.Param("id")
2424

25-
var device []models.Devices
25+
var device models.Devices
2626
result := initializers.DB.First(&device, id)
2727

2828
if result.Error != nil {

controllers/Rooms.controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func GetAllRooms(c *gin.Context) {
2020
func GetAllRoomsByID(c *gin.Context) {
2121

2222
// >> Get all rooms from database
23-
var room []models.Rooms
23+
var room models.Rooms
2424
result := initializers.DB.Find(room)
2525

2626
if result.Error != nil {

controllers/User.controller.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,58 @@ func GetUserByID(c *gin.Context) {
3232

3333
c.IndentedJSON(http.StatusOK, user)
3434
}
35+
36+
func Login(c *gin.Context) {
37+
// >> Login user to his account if account exist
38+
var body struct {
39+
Email string
40+
Password string
41+
}
42+
43+
c.Bind(&body)
44+
45+
var user models.User
46+
result := initializers.DB.First(&user, body)
47+
48+
if result.Error != nil {
49+
c.JSON(http.StatusNotFound, gin.H{
50+
"message": "User with that credentials doesn't exist.",
51+
})
52+
}
53+
}
54+
55+
func Register(c *gin.Context) {
56+
var body struct {
57+
Name string
58+
Surname string
59+
Email string
60+
Password string
61+
City string
62+
}
63+
c.Bind(&body)
64+
65+
// Check if user already exists in database
66+
var existingUser models.User
67+
result := initializers.DB.Where("email = ?", body.Email).First(&existingUser)
68+
if result.Error == nil {
69+
c.JSON(http.StatusConflict, gin.H{
70+
"message": "User already exists.",
71+
})
72+
return
73+
}
74+
75+
// Insert new user into database
76+
user := models.User{Name: body.Name, Surname: body.Surname, Email: body.Email, City: body.City, Password: body.Password}
77+
result = initializers.DB.Create(&user)
78+
79+
if result.Error != nil {
80+
c.JSON(http.StatusInternalServerError, gin.H{
81+
"message": "Internal server error.",
82+
})
83+
return
84+
}
85+
86+
c.JSON(http.StatusOK, gin.H{
87+
"message": "Account successfully registered.",
88+
})
89+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/kaizerpwn/homelab-backend
22

3-
go 1.21.1
3+
go 1.21
44

55
require (
66
github.com/bytedance/sonic v1.10.1 // indirect

homelab-backend

16 MB
Binary file not shown.

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ func main() {
3030
}))
3131

3232
// >> Users routes
33-
r.GET("/api/users", controllers.GetAllUsers)
3433
r.GET("/api/users/:id", controllers.GetUserByID)
34+
r.GET("/api/users", controllers.GetAllUsers)
35+
r.POST("/api/users/register", controllers.Register)
36+
r.POST("/api/users/login", controllers.Login)
3537

3638
// >> Devices routes
3739
r.GET("/api/devices", controllers.GetAllDevices)

0 commit comments

Comments
 (0)