implemented logout
This commit is contained in:
+21
-6
@@ -25,7 +25,9 @@ import (
|
||||
|
||||
// AmContext is the interface for Amsterdam's wrapper context that exposes the required functionality.
|
||||
type AmContext interface {
|
||||
ClearSession()
|
||||
CurrentUser() *database.User
|
||||
CurrentUserId() int32
|
||||
FormField(string) string
|
||||
FormFieldInt(string) (int, error)
|
||||
FormFieldIsSet(string) bool
|
||||
@@ -35,8 +37,8 @@ type AmContext interface {
|
||||
RemoteIP() string
|
||||
Render(string) error
|
||||
ReplaceUser(*database.User)
|
||||
SaveSession() error
|
||||
SubRender(string) ([]byte, error)
|
||||
Session() *sessions.Session
|
||||
SetOutputType(string)
|
||||
SetRC(int)
|
||||
GetScratch(string) any
|
||||
@@ -55,6 +57,14 @@ type amContext struct {
|
||||
session *sessions.Session
|
||||
}
|
||||
|
||||
// ClearSession clears the current session.
|
||||
func (c *amContext) ClearSession() {
|
||||
for k := range c.session.Values {
|
||||
delete(c.session.Values, k)
|
||||
}
|
||||
SetupAmSession(c.session)
|
||||
}
|
||||
|
||||
// CurrentUser returns the current user from the session.
|
||||
func (c *amContext) CurrentUser() *database.User {
|
||||
u, err := database.AmGetUser(c.session.Values["user_id"].(int32))
|
||||
@@ -64,6 +74,11 @@ func (c *amContext) CurrentUser() *database.User {
|
||||
return u
|
||||
}
|
||||
|
||||
// CurrentUserId returns the current user ID.
|
||||
func (c *amContext) CurrentUserId() int32 {
|
||||
return c.session.Values["user_id"].(int32)
|
||||
}
|
||||
|
||||
/* FormField returns the value of a form field from the request.
|
||||
* Parameters:
|
||||
* name - The name of the field to retrieve.
|
||||
@@ -146,6 +161,11 @@ func (c *amContext) ReplaceUser(u *database.User) {
|
||||
c.session.Values["user_id"] = u.Uid
|
||||
}
|
||||
|
||||
// SaveSession saves the session link to cookies.
|
||||
func (c *amContext) SaveSession() error {
|
||||
return c.session.Save(c.echoContext.Request(), c.echoContext.Response())
|
||||
}
|
||||
|
||||
// Scratchpad returns the per-request scratchpad for values.
|
||||
func (c *amContext) Scratchpad() map[string]any {
|
||||
if c.scratchpad == nil {
|
||||
@@ -171,11 +191,6 @@ func (c *amContext) SubRender(name string) ([]byte, error) {
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
||||
// Session returns the HTTP session.
|
||||
func (c *amContext) Session() *sessions.Session {
|
||||
return c.session
|
||||
}
|
||||
|
||||
// SetOutputType sets the MIME output type for the current operation.
|
||||
func (c *amContext) SetOutputType(typ string) {
|
||||
c.outputType = typ
|
||||
|
||||
+1
-1
@@ -79,7 +79,7 @@ func AmWrap(myfunc func(AmContext) (string, any, error)) echo.HandlerFunc {
|
||||
}
|
||||
what, rc, err := myfunc(amctxt)
|
||||
if err == nil {
|
||||
if err = amctxt.Session().Save(ctxt.Request(), ctxt.Response()); err != nil {
|
||||
if err = amctxt.SaveSession(); err != nil {
|
||||
ctxt.Logger().Errorf("Session save error: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@ func SetupSessionManager() {
|
||||
|
||||
// SetupAmSession sets up a newly created Amsterdam session.
|
||||
func SetupAmSession(session *sessions.Session) {
|
||||
session.Values["temp"] = "Active"
|
||||
u, err := database.AmGetAnonUser()
|
||||
if err == nil {
|
||||
session.Values["user_id"] = u.Uid
|
||||
|
||||
+1
-1
@@ -65,7 +65,7 @@
|
||||
{{ else }}
|
||||
You are logged in as <b>{{ .CurrentUser().Username }}</b>
|
||||
<span class="mx-2">-</span>
|
||||
<a href="/TODO/logout" class="text-yellow-300 hover:text-yellow-400">Log Out</a>
|
||||
<a href="/logout" class="text-yellow-300 hover:text-yellow-400">Log Out</a>
|
||||
<span class="mx-2">|</span>
|
||||
<a href="/TODO/profile" class="text-yellow-300 hover:text-yellow-400">Profile</a>
|
||||
{{ end }}
|
||||
|
||||
Reference in New Issue
Block a user