Login / logout
This commit is contained in:
@ -1,23 +1,20 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fiber-sessions/app/handlers/usersession"
|
||||
"fiber-sessions/app/templates"
|
||||
"fiber-sessions/app/types"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func Home(f *fiber.Ctx) error {
|
||||
user := types.User{
|
||||
FirstName: "Pero",
|
||||
LastName: "Perić",
|
||||
}
|
||||
func Home(ctx *fiber.Ctx) error {
|
||||
requestSession := usersession.NewRequestSession(ctx)
|
||||
|
||||
pc := templates.PageContext{
|
||||
Title: "Welcome to the demo",
|
||||
ActivePage: "home",
|
||||
User: &user,
|
||||
Title: "Welcome to the demo",
|
||||
ActivePage: "home",
|
||||
RequestSession: requestSession,
|
||||
}
|
||||
Render(f, templates.Home(pc))
|
||||
Render(ctx, templates.Home(pc))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -1,9 +1,8 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fiber-sessions/app/handlers/usersession"
|
||||
"fiber-sessions/app/templates"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
@ -13,37 +12,19 @@ var pcLogin templates.PageContext = templates.PageContext{
|
||||
ActivePage: "login",
|
||||
}
|
||||
|
||||
func checkUsernamePassword(username string, password string) bool {
|
||||
return true
|
||||
// return username == "pero" && password == "pero"
|
||||
}
|
||||
|
||||
func Login(f *fiber.Ctx) error {
|
||||
session, err := sessionStore.Get(f)
|
||||
if err != nil {
|
||||
panic("Error gettings sessionStore: " + err.Error())
|
||||
}
|
||||
|
||||
userId := session.Get("userId")
|
||||
fmt.Printf("userId: %+v\n", userId)
|
||||
|
||||
Render(f, templates.RenderLogin(pcLogin))
|
||||
func Login(ctx *fiber.Ctx) error {
|
||||
pcLogin.RequestSession = usersession.NewRequestSession(ctx)
|
||||
Render(ctx, templates.RenderLogin(pcLogin))
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoginSubmit(f *fiber.Ctx) error {
|
||||
session, err := sessionStore.Get(f)
|
||||
if err != nil {
|
||||
panic("Error gettings sessionStore: " + err.Error())
|
||||
}
|
||||
|
||||
userId := session.Get("userId")
|
||||
fmt.Printf("userId: %+v\n", userId)
|
||||
func LoginSubmit(ctx *fiber.Ctx) error {
|
||||
requestSession := usersession.NewRequestSession(ctx)
|
||||
|
||||
content := templates.LoginFormValidationContent{
|
||||
Validated: true,
|
||||
Username: f.FormValue("username"),
|
||||
Password: f.FormValue("password"),
|
||||
Username: ctx.FormValue("username"),
|
||||
Password: ctx.FormValue("password"),
|
||||
}
|
||||
hasError := false
|
||||
|
||||
@ -58,20 +39,14 @@ func LoginSubmit(f *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
if !hasError {
|
||||
if checkUsernamePassword(content.Username, content.Password) {
|
||||
session.Set("userId", "neki-user-id")
|
||||
session.SetExpiry(time.Second * 60)
|
||||
if err := session.Save(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
f.Set("HX-Location", "/")
|
||||
err := requestSession.LoginUser(content.Username, content.Password)
|
||||
if err == nil {
|
||||
ctx.Set("HX-Location", "/")
|
||||
return nil
|
||||
} else {
|
||||
content.LoginError = "Invalid username or password"
|
||||
}
|
||||
content.LoginError = "Invalid username or password"
|
||||
}
|
||||
|
||||
Render(f, templates.LoginFormContent(content))
|
||||
Render(ctx, templates.LoginFormContent(content))
|
||||
return nil
|
||||
}
|
||||
|
||||
15
app/handlers/logout.go
Normal file
15
app/handlers/logout.go
Normal file
@ -0,0 +1,15 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fiber-sessions/app/handlers/usersession"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func Logout(ctx *fiber.Ctx) error {
|
||||
requestSession := usersession.NewRequestSession(ctx)
|
||||
requestSession.Logout()
|
||||
|
||||
ctx.Redirect("/")
|
||||
return nil
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2/middleware/session"
|
||||
"github.com/gofiber/storage/memory/v2"
|
||||
)
|
||||
|
||||
var sessionStorage = memory.New()
|
||||
var sessionStore = session.New(session.Config{
|
||||
Storage: sessionStorage,
|
||||
})
|
||||
113
app/handlers/usersession/usersession.go
Normal file
113
app/handlers/usersession/usersession.go
Normal file
@ -0,0 +1,113 @@
|
||||
package usersession
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fiber-sessions/app/types"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/session"
|
||||
"github.com/gofiber/storage/memory/v2"
|
||||
)
|
||||
|
||||
var sessionStorage = memory.New()
|
||||
var sessionStore = session.New(session.Config{
|
||||
Storage: sessionStorage,
|
||||
})
|
||||
|
||||
const SESSION_EXPIRES = time.Minute * 30
|
||||
|
||||
var dbUsers map[string]types.User = map[string]types.User{
|
||||
"pero-uuid": {
|
||||
Id: "pero-uuid",
|
||||
Username: "pero",
|
||||
Password: "pero",
|
||||
FirstName: "Pero",
|
||||
LastName: "Perić",
|
||||
},
|
||||
"mirko-uuid": {
|
||||
Id: "mirko-uuid",
|
||||
Username: "mirko",
|
||||
Password: "mirko",
|
||||
FirstName: "Mirko",
|
||||
LastName: "Mirković",
|
||||
},
|
||||
}
|
||||
|
||||
type RequestSession struct {
|
||||
ctx *fiber.Ctx
|
||||
session *session.Session
|
||||
User *types.User
|
||||
}
|
||||
|
||||
func NewRequestSession(ctx *fiber.Ctx) *RequestSession {
|
||||
session, err := sessionStore.Get(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var user *types.User = nil
|
||||
|
||||
userId := session.Get("userId")
|
||||
fmt.Printf("userId: %+v\n", userId)
|
||||
if userId != nil {
|
||||
user = getUserById(userId.(string))
|
||||
}
|
||||
|
||||
return &RequestSession{
|
||||
ctx: ctx,
|
||||
session: session,
|
||||
User: user,
|
||||
}
|
||||
}
|
||||
|
||||
func getUserById(id string) *types.User {
|
||||
user, ok := dbUsers[id]
|
||||
if ok {
|
||||
return &user
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getUser(username string, password string) *types.User {
|
||||
for _, user := range dbUsers {
|
||||
if user.Username == username {
|
||||
if user.Password == password {
|
||||
return &user
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RequestSession) LoginUser(username string, password string) error {
|
||||
user := getUser(username, password)
|
||||
|
||||
if user == nil {
|
||||
return errors.New("invalid username or password")
|
||||
}
|
||||
r.User = user
|
||||
|
||||
r.session.Set("userId", r.User.Id)
|
||||
r.session.SetExpiry(SESSION_EXPIRES)
|
||||
if err := r.session.Save(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RequestSession) UserAuthenticated() bool {
|
||||
return r.User != nil
|
||||
}
|
||||
|
||||
func (r *RequestSession) Logout() error {
|
||||
if !r.UserAuthenticated() {
|
||||
return errors.New("user not authenticated")
|
||||
}
|
||||
r.session.Destroy()
|
||||
r.User = nil
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user