2021-09-13 11:46:43 +01:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.ctrlz.es/mgdelacroix/craban/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (a *App) Login(username, password string) (string, error) {
|
2021-09-13 13:52:00 +01:00
|
|
|
user, err := a.Store.User().GetByUsername(username)
|
2021-09-13 11:46:43 +01:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return "", nil
|
|
|
|
} else if err != nil {
|
|
|
|
return "", fmt.Errorf("cannot get user by username: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !utils.CheckHash(user.Password, password) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2021-09-13 13:52:00 +01:00
|
|
|
token, err := utils.GenerateToken(user.ID, *a.Config.Secret)
|
2021-09-13 11:46:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("cannot generate token: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
}
|