Browse Source

Added creation a new admin when none exist

master
kolaente 2 years ago
parent
commit
a5ff0a4cf9
Signed by: konrad GPG Key ID: F40E70337AB24C9B
3 changed files with 27 additions and 5 deletions
  1. +16
    -0
      http-tests/user.http
  2. +10
    -4
      pkg/models/user.go
  3. +1
    -1
      pkg/routes/routes.go

+ 16
- 0
http-tests/user.http View File

@ -0,0 +1,16 @@
### Authorization by token, part 1. Retrieve and save token.
POST http://localhost:1073/api/v1/user/auth
Content-Type: application/json
{
"username": "admin",
"password": "admin"
}
> {% client.global.set("auth_token", response.body.token); %}
### Authorization by token, part 2. Use token to authorize.
GET http://localhost:1073/api/v1/user
Authorization: Bearer {{auth_token}}
###

+ 10
- 4
pkg/models/user.go View File

@ -104,15 +104,20 @@ func GetUserByID(id int64) (user *User, err error) {
}
// GetUser gets a user object
func GetUser(user *User) (userOut *User, err error) {
userOut = user
exists, err := x.Get(&userOut)
func GetUser(user *User) (*User, error) {
exists, err := x.Get(user)
fmt.Println(user, err, exists)
if err != nil {
return &User{}, err
}
if !exists {
return &User{}, ErrUserDoesNotExist{UserID: user.ID}
}
return userOut, err
return user, err
}
// CheckUserCredentials checks user credentials
@ -123,6 +128,7 @@ func CheckUserCredentials(u *UserLogin) (*User, error) {
}
// Check if the user exists
fmt.Println(u)
user, err := GetUser(&User{Username: u.Username})
if err != nil {
return &User{}, err

+ 1
- 1
pkg/routes/routes.go View File

@ -94,7 +94,7 @@ func RegisterRoutes(e *echo.Echo) {
// Swagger UI
a.GET("/swagger/*", echoSwagger.WrapHandler)
a.POST("/login", apiv1.Login)
a.POST("/user/auth", apiv1.Login)
// ===== Routes with Authetification =====
// Authetification

Loading…
Cancel
Save