Added creation a new admin when none exist

This commit is contained in:
kolaente 2018-12-07 22:19:45 +01:00
parent ef9f105ab1
commit a5ff0a4cf9
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 27 additions and 5 deletions

16
http-tests/user.http Normal file
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}}
###

View File

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

View File

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