forked from goffee/cup
start cookie session
This commit is contained in:
parent
017ef4c066
commit
1b6f3e6103
6 changed files with 299 additions and 55 deletions
|
|
@ -8,7 +8,10 @@ package controllers
|
|||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
|
@ -19,6 +22,8 @@ import (
|
|||
"git.smarteching.com/goffee/cup/utils"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"git.smarteching.com/goffee/core/template/components"
|
||||
)
|
||||
|
||||
func Signup(c *core.Context) *core.Response {
|
||||
|
|
@ -123,6 +128,13 @@ func Signin(c *core.Context) *core.Response {
|
|||
email := c.GetRequestParam("email")
|
||||
password := c.GetRequestParam("password")
|
||||
|
||||
// check if template engine is enable
|
||||
TemplateEnableStr := os.Getenv("TEMPLATE_ENABLE")
|
||||
if TemplateEnableStr == "" {
|
||||
TemplateEnableStr = "false"
|
||||
}
|
||||
TemplateEnable, _ := strconv.ParseBool(TemplateEnableStr)
|
||||
|
||||
data := map[string]interface{}{
|
||||
"email": email,
|
||||
"password": password,
|
||||
|
|
@ -135,36 +147,61 @@ func Signin(c *core.Context) *core.Response {
|
|||
|
||||
if v.Failed() {
|
||||
c.GetLogger().Error(v.GetErrorMessagesJson())
|
||||
return c.Response.SetStatusCode(http.StatusUnprocessableEntity).Json(v.GetErrorMessagesJson())
|
||||
if TemplateEnable {
|
||||
// TODO set error in session
|
||||
return c.Response.Redirect("/applogin")
|
||||
} else {
|
||||
return c.Response.SetStatusCode(http.StatusUnprocessableEntity).Json(v.GetErrorMessagesJson())
|
||||
}
|
||||
}
|
||||
|
||||
var user models.User
|
||||
res := c.GetGorm().Where("email = ?", c.CastToString(email)).First(&user)
|
||||
if res.Error != nil && !errors.Is(res.Error, gorm.ErrRecordNotFound) {
|
||||
c.GetLogger().Error(res.Error.Error())
|
||||
return c.Response.SetStatusCode(http.StatusInternalServerError).Json(c.MapToJson(map[string]string{
|
||||
"message": "internal server error",
|
||||
}))
|
||||
if TemplateEnable {
|
||||
// TODO set error in session
|
||||
return c.Response.Redirect("/applogin")
|
||||
} else {
|
||||
return c.Response.SetStatusCode(http.StatusInternalServerError).Json(c.MapToJson(map[string]string{
|
||||
"message": "internal server error",
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
if res.Error != nil && errors.Is(res.Error, gorm.ErrRecordNotFound) {
|
||||
return c.Response.SetStatusCode(http.StatusUnprocessableEntity).Json(c.MapToJson(map[string]string{
|
||||
"message": "invalid email or password",
|
||||
}))
|
||||
if TemplateEnable {
|
||||
// TODO set error in session
|
||||
return c.Response.Redirect("/applogin")
|
||||
} else {
|
||||
return c.Response.SetStatusCode(http.StatusUnprocessableEntity).Json(c.MapToJson(map[string]string{
|
||||
"message": "invalid email or password",
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
ok, err := c.GetHashing().CheckPasswordHash(user.Password, c.CastToString(password))
|
||||
if err != nil {
|
||||
c.GetLogger().Error(err.Error())
|
||||
return c.Response.SetStatusCode(http.StatusInternalServerError).Json(c.MapToJson(map[string]string{
|
||||
"message": err.Error(),
|
||||
}))
|
||||
if TemplateEnable {
|
||||
// TODO set error in session
|
||||
return c.Response.Redirect("/applogin")
|
||||
} else {
|
||||
return c.Response.SetStatusCode(http.StatusInternalServerError).Json(c.MapToJson(map[string]string{
|
||||
"message": err.Error(),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
if !ok {
|
||||
return c.Response.SetStatusCode(http.StatusUnprocessableEntity).Json(c.MapToJson(map[string]string{
|
||||
"message": "invalid email or password",
|
||||
}))
|
||||
if TemplateEnable {
|
||||
// TODO set error in session
|
||||
return c.Response.Redirect("/applogin")
|
||||
} else {
|
||||
return c.Response.SetStatusCode(http.StatusUnprocessableEntity).Json(c.MapToJson(map[string]string{
|
||||
"message": "invalid email or password",
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
token, err := c.GetJWT().GenerateToken(map[string]interface{}{
|
||||
|
|
@ -173,9 +210,14 @@ func Signin(c *core.Context) *core.Response {
|
|||
|
||||
if err != nil {
|
||||
c.GetLogger().Error(err.Error())
|
||||
return c.Response.SetStatusCode(http.StatusInternalServerError).Json(c.MapToJson(map[string]string{
|
||||
"message": "internal server error",
|
||||
}))
|
||||
// TODO set error in session
|
||||
if TemplateEnable {
|
||||
return c.Response.Redirect("/applogin")
|
||||
} else {
|
||||
return c.Response.SetStatusCode(http.StatusInternalServerError).Json(c.MapToJson(map[string]string{
|
||||
"message": "internal server error",
|
||||
}))
|
||||
}
|
||||
}
|
||||
// cache the token
|
||||
userAgent := c.GetUserAgent()
|
||||
|
|
@ -183,14 +225,31 @@ func Signin(c *core.Context) *core.Response {
|
|||
err = c.GetCache().Set(hashedCacheKey, token)
|
||||
if err != nil {
|
||||
c.GetLogger().Error(err.Error())
|
||||
return c.Response.SetStatusCode(http.StatusInternalServerError).Json(c.MapToJson(map[string]interface{}{
|
||||
"message": "internal server error",
|
||||
}))
|
||||
if TemplateEnable {
|
||||
// TODO set error in session
|
||||
return c.Response.Redirect("/applogin")
|
||||
} else {
|
||||
return c.Response.SetStatusCode(http.StatusInternalServerError).Json(c.MapToJson(map[string]interface{}{
|
||||
"message": "internal server error",
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
return c.Response.Json(c.MapToJson(map[string]string{
|
||||
"token": token,
|
||||
}))
|
||||
if TemplateEnable {
|
||||
// create cookie
|
||||
err = core.SetCookie(c.Response.HttpResponseWriter, email.(string), token)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Error write encrypted cookie: %v", err))
|
||||
return c.Response.SetStatusCode(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// redirecto to app
|
||||
return c.Response.Redirect("/appsample")
|
||||
} else {
|
||||
return c.Response.Json(c.MapToJson(map[string]string{
|
||||
"token": token,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
func ResetPasswordRequest(c *core.Context) *core.Response {
|
||||
|
|
@ -389,3 +448,41 @@ func Signout(c *core.Context) *core.Response {
|
|||
"message": "signed out successfully",
|
||||
}))
|
||||
}
|
||||
|
||||
// Show basic app login
|
||||
func AppLogin(c *core.Context) *core.Response {
|
||||
|
||||
type templateData struct {
|
||||
TheTitle components.Title
|
||||
}
|
||||
|
||||
tmplData := templateData{
|
||||
TheTitle: components.Title{
|
||||
Label: "Login form",
|
||||
},
|
||||
}
|
||||
|
||||
return c.Response.Template("login.html", tmplData)
|
||||
|
||||
}
|
||||
|
||||
// Show basic app sample
|
||||
func AppSample(c *core.Context) *core.Response {
|
||||
|
||||
title := "lorem"
|
||||
|
||||
type templateData struct {
|
||||
TheTitle components.Title
|
||||
}
|
||||
|
||||
tmplData := templateData{
|
||||
TheTitle: components.Title{
|
||||
Label: title,
|
||||
},
|
||||
}
|
||||
|
||||
//fmt.Printf("Outside cookie user is: %s", user.Email)
|
||||
|
||||
return c.Response.Template("app.html", tmplData)
|
||||
|
||||
}
|
||||
|
|
|
|||
27
controllers/sample.go
Normal file
27
controllers/sample.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
|
||||
// Use of this source code is governed by MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"git.smarteching.com/goffee/core"
|
||||
"git.smarteching.com/goffee/core/template/components"
|
||||
)
|
||||
|
||||
// Show basic template
|
||||
func Sample(c *core.Context) *core.Response {
|
||||
|
||||
type templateData struct {
|
||||
TheTitle components.Title
|
||||
}
|
||||
|
||||
tmplData := templateData{
|
||||
TheTitle: components.Title{
|
||||
Label: "Lorem ipsum inside",
|
||||
},
|
||||
}
|
||||
|
||||
return c.Response.Template("basic.html", tmplData)
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue