Files
libredesk/cmd/login.go
Abhinav Raut 0e84fac099 feat: ACL and ABAC
- Simplify ws package.
2024-09-19 04:17:03 +05:30

46 lines
1.3 KiB
Go

package main
import (
"github.com/abhinavxd/artemis/internal/envelope"
"github.com/valyala/fasthttp"
"github.com/zerodha/fastglue"
)
// handleLogin logs in the user.
func handleLogin(r *fastglue.Request) error {
var (
app = r.Context.(*App)
p = r.RequestCtx.PostArgs()
email = string(p.Peek("email"))
password = p.Peek("password")
)
user, err := app.user.Login(email, password)
if err != nil {
return sendErrorEnvelope(r, err)
}
if err := app.auth.SaveSession(user, r); err != nil {
app.lo.Error("error saving session", "error", err)
return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, app.i18n.T("user.errorAcquiringSession"), nil))
}
return r.SendEnvelope(user)
}
// handleLogout logs out the user and redirects to the dashboard.
func handleLogout(r *fastglue.Request) error {
var (
app = r.Context.(*App)
)
if err := app.auth.DestroySession(r); err != nil {
return sendErrorEnvelope(r, envelope.NewError(envelope.GeneralError, app.i18n.T("user.errorAcquiringSession"), nil))
}
// Add no-cache headers.
r.RequestCtx.Response.Header.Add("Cache-Control",
"no-store, no-cache, must-revalidate, post-check=0, pre-check=0")
r.RequestCtx.Response.Header.Add("Pragma", "no-cache")
r.RequestCtx.Response.Header.Add("Expires", "-1")
return r.RedirectURI("dashboard", fasthttp.StatusFound, nil, "")
}