mirror of
https://github.com/grassrootseconomics/farmstar-survey-backend.git
synced 2025-04-16 17:31:02 +02:00
30 lines
734 B
Go
30 lines
734 B
Go
package router
|
|
|
|
import (
|
|
"github.com/labstack/echo/v5"
|
|
"github.com/pocketbase/pocketbase/apis"
|
|
"github.com/pocketbase/pocketbase/core"
|
|
)
|
|
|
|
func (r *RouterContainer) AuthRoute() {
|
|
r.PB.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
|
e.Router.GET("/login", func(c echo.Context) error {
|
|
data := struct {
|
|
Phone string `json:"phone" form:"phone"`
|
|
}{}
|
|
if err := c.Bind(&data); err != nil {
|
|
return apis.NewBadRequestError("Failed to read request data", err)
|
|
}
|
|
|
|
record, err := r.PB.Dao().FindFirstRecordByData("users", "phone", data.Phone)
|
|
if err != nil {
|
|
return apis.NewBadRequestError("Invalid credentials", err)
|
|
}
|
|
|
|
return apis.RecordAuthResponse(r.PB, c, record, nil)
|
|
})
|
|
|
|
return nil
|
|
})
|
|
}
|