mirror of
https://github.com/abhinavxd/libredesk.git
synced 2025-11-03 13:33:32 +00:00
46 lines
1.3 KiB
Go
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, "")
|
|
}
|