saasitone/routes/login.go

95 lines
2.1 KiB
Go
Raw Normal View History

2021-12-14 08:13:53 -08:00
package routes
2021-12-03 12:41:40 -08:00
import (
2021-12-12 16:02:25 -08:00
"fmt"
"strings"
2021-12-12 16:02:25 -08:00
2022-01-01 07:44:18 -08:00
"github.com/mikestefanello/pagoda/context"
"github.com/mikestefanello/pagoda/controller"
"github.com/mikestefanello/pagoda/ent"
"github.com/mikestefanello/pagoda/ent/user"
"github.com/mikestefanello/pagoda/msg"
2021-12-03 13:35:11 -08:00
2021-12-03 12:41:40 -08:00
"github.com/labstack/echo/v4"
)
2021-12-12 16:02:25 -08:00
type (
2022-02-10 05:56:07 -08:00
login struct {
controller.Controller
2021-12-12 16:02:25 -08:00
}
2022-02-10 05:56:07 -08:00
loginForm struct {
Email string `form:"email" validate:"required,email"`
Password string `form:"password" validate:"required"`
2021-12-23 20:04:00 -08:00
Submission controller.FormSubmission
2021-12-12 16:02:25 -08:00
}
)
2021-12-03 12:41:40 -08:00
2022-02-10 05:56:07 -08:00
func (c *login) Get(ctx echo.Context) error {
2021-12-23 20:04:00 -08:00
page := controller.NewPage(ctx)
page.Layout = "auth"
page.Name = "login"
page.Title = "Log in"
2022-02-10 05:56:07 -08:00
page.Form = loginForm{}
2021-12-14 19:14:39 -08:00
2021-12-23 20:04:00 -08:00
if form := ctx.Get(context.FormKey); form != nil {
2022-02-10 05:56:07 -08:00
page.Form = form.(*loginForm)
2021-12-14 19:14:39 -08:00
}
2021-12-23 20:04:00 -08:00
return c.RenderPage(ctx, page)
2021-12-03 12:41:40 -08:00
}
2022-02-10 05:56:07 -08:00
func (c *login) Post(ctx echo.Context) error {
var form loginForm
2021-12-23 20:04:00 -08:00
ctx.Set(context.FormKey, &form)
authFailed := func() error {
form.Submission.SetFieldError("Email", "")
form.Submission.SetFieldError("Password", "")
msg.Danger(ctx, "Invalid credentials. Please try again.")
return c.Get(ctx)
2021-12-12 16:02:25 -08:00
}
2021-12-12 18:28:53 -08:00
// Parse the form values
2021-12-23 20:04:00 -08:00
if err := ctx.Bind(&form); err != nil {
return c.Fail(err, "unable to parse login form")
2021-12-23 20:04:00 -08:00
}
if err := form.Submission.Process(ctx, form); err != nil {
return c.Fail(err, "unable to process form submission")
2021-12-12 18:28:53 -08:00
}
2021-12-23 20:04:00 -08:00
if form.Submission.HasErrors() {
return c.Get(ctx)
}
2021-12-12 16:02:25 -08:00
// Attempt to load the user
2021-12-23 20:04:00 -08:00
u, err := c.Container.ORM.User.
Query().
Where(user.Email(strings.ToLower(form.Email))).
2021-12-23 20:04:00 -08:00
Only(ctx.Request().Context())
switch err.(type) {
case *ent.NotFoundError:
2021-12-23 20:04:00 -08:00
return authFailed()
case nil:
default:
return c.Fail(err, "error querying user during login")
2021-12-12 16:02:25 -08:00
}
// Check if the password is correct
2021-12-23 20:04:00 -08:00
err = c.Container.Auth.CheckPassword(form.Password, u.Password)
2021-12-12 16:02:25 -08:00
if err != nil {
2021-12-23 20:04:00 -08:00
return authFailed()
2021-12-12 16:02:25 -08:00
}
// Log the user in
2021-12-23 20:04:00 -08:00
err = c.Container.Auth.Login(ctx, u.ID)
2021-12-12 16:02:25 -08:00
if err != nil {
return c.Fail(err, "unable to log in user")
}
2021-12-23 20:04:00 -08:00
msg.Success(ctx, fmt.Sprintf("Welcome back, <strong>%s</strong>. You are now logged in.", u.Name))
return c.Redirect(ctx, "home")
2021-12-03 13:35:11 -08:00
}