mirror of
https://github.com/simon987/task_tracker.git
synced 2025-12-14 07:19:02 +00:00
some work on auth/sessions
This commit is contained in:
53
api/auth.go
Normal file
53
api/auth.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/kataras/go-sessions"
|
||||
"github.com/simon987/task_tracker/storage"
|
||||
)
|
||||
|
||||
type LoginRequest struct {
|
||||
Username []byte
|
||||
Password []byte
|
||||
}
|
||||
|
||||
type LoginResponse struct {
|
||||
Ok bool
|
||||
Message string
|
||||
Manager *storage.Manager
|
||||
}
|
||||
|
||||
func (api *WebAPI) Login(r *Request) {
|
||||
|
||||
req := &LoginRequest{}
|
||||
err := json.Unmarshal(r.Ctx.Request.Body(), req)
|
||||
|
||||
if err != nil {
|
||||
r.Json(LoginResponse{
|
||||
Ok: false,
|
||||
Message: "Could not parse request",
|
||||
}, 400)
|
||||
return
|
||||
}
|
||||
|
||||
manager, err := api.Database.ValidateCredentials(req.Username, req.Password)
|
||||
if err != nil {
|
||||
logrus.WithError(err).WithFields(logrus.Fields{
|
||||
"username": string(manager.Username),
|
||||
}).Warning("Login attempt")
|
||||
|
||||
r.Json(LoginResponse{
|
||||
Ok: false,
|
||||
Message: "Invalid username/password",
|
||||
}, 403)
|
||||
return
|
||||
}
|
||||
|
||||
sess := sessions.StartFasthttp(r.Ctx)
|
||||
sess.Set("manager", manager)
|
||||
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"username": string(manager.Username),
|
||||
}).Info("Logged in")
|
||||
}
|
||||
Reference in New Issue
Block a user