saasitone/routes/login.go

95 lines
2.0 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
2021-12-14 19:14:39 -08:00
"goweb/context"
"goweb/controller"
2021-12-12 16:02:25 -08:00
"goweb/ent"
"goweb/ent/user"
2021-12-03 13:35:11 -08:00
"goweb/msg"
2021-12-03 12:41:40 -08:00
"github.com/labstack/echo/v4"
)
2021-12-12 16:02:25 -08:00
type (
Login struct {
controller.Controller
2021-12-12 16:02:25 -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
2021-12-23 20:04:00 -08:00
func (c *Login) Get(ctx echo.Context) error {
page := controller.NewPage(ctx)
page.Layout = "auth"
page.Name = "login"
page.Title = "Log in"
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 {
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
}
2021-12-23 20:04:00 -08:00
func (c *Login) Post(ctx echo.Context) error {
var form LoginForm
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(ctx, err, "unable to parse login form")
}
if err := form.Submission.Process(ctx, form); err != nil {
return c.Fail(ctx, 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:
2021-12-23 20:04:00 -08:00
return c.Fail(ctx, 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 {
2021-12-23 20:04:00 -08:00
return c.Fail(ctx, 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
}