Add login endpoint and generate a valid token
This commit is contained in:
parent
83a2d2a31f
commit
9b6f7cbdcc
6 changed files with 89 additions and 11 deletions
28
server/app/auth.go
Normal file
28
server/app/auth.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"git.ctrlz.es/mgdelacroix/craban/utils"
|
||||
)
|
||||
|
||||
func (a *App) Login(username, password string) (string, error) {
|
||||
user, err := a.store.User().GetByUsername(username)
|
||||
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
|
||||
}
|
||||
|
||||
token, err := utils.GenerateToken(user.ID, *a.config.Secret)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot generate token: %w", err)
|
||||
}
|
||||
|
||||
return token, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue