implemented logout
This commit is contained in:
@@ -94,6 +94,28 @@ func Login(ctxt ui.AmContext) (string, any, error) {
|
|||||||
return ui.ErrorPage(ctxt, err)
|
return ui.ErrorPage(ctxt, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Logout handles logging out from Amsterdam.
|
||||||
|
* Parameters:
|
||||||
|
* ctxt - The AmContext for the request.
|
||||||
|
* Returns:
|
||||||
|
* Command string dictating what to be rendered.
|
||||||
|
* Data as a parameter for the command string.
|
||||||
|
* Standard Go error status.
|
||||||
|
*/
|
||||||
|
func Logout(ctxt ui.AmContext) (string, any, error) {
|
||||||
|
// Get target URI.
|
||||||
|
target := ctxt.Parameter("tgt")
|
||||||
|
if target == "" {
|
||||||
|
target = "/"
|
||||||
|
}
|
||||||
|
|
||||||
|
if !ctxt.CurrentUser().IsAnon {
|
||||||
|
// TODO: erase login cookie
|
||||||
|
ctxt.ClearSession()
|
||||||
|
}
|
||||||
|
return "redirect", target, nil
|
||||||
|
}
|
||||||
|
|
||||||
/* NewAccountUserAgreement renders the Amsterdam user agreement for new accounts.
|
/* NewAccountUserAgreement renders the Amsterdam user agreement for new accounts.
|
||||||
* Parameters:
|
* Parameters:
|
||||||
* ctxt - The AmContext for the request.
|
* ctxt - The AmContext for the request.
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ func setupEcho() *echo.Echo {
|
|||||||
e.GET("/about", ui.AmWrap(AboutPage))
|
e.GET("/about", ui.AmWrap(AboutPage))
|
||||||
e.GET("/login", ui.AmWrap(LoginForm))
|
e.GET("/login", ui.AmWrap(LoginForm))
|
||||||
e.POST("/login", ui.AmWrap(Login))
|
e.POST("/login", ui.AmWrap(Login))
|
||||||
|
e.GET("/logout", ui.AmWrap(Logout))
|
||||||
e.GET("/newacct", ui.AmWrap(NewAccountUserAgreement))
|
e.GET("/newacct", ui.AmWrap(NewAccountUserAgreement))
|
||||||
e.GET("/newacct2", ui.AmWrap(NewAccountForm))
|
e.GET("/newacct2", ui.AmWrap(NewAccountForm))
|
||||||
|
|
||||||
|
|||||||
@@ -125,7 +125,7 @@ func TopPage(ctxt ui.AmContext) (string, any, error) {
|
|||||||
ctxt.VarMap().Set("amsterdam_pageTitle", "My Front Page")
|
ctxt.VarMap().Set("amsterdam_pageTitle", "My Front Page")
|
||||||
|
|
||||||
// Retrieve the sideboxes and create the data to be presented.
|
// Retrieve the sideboxes and create the data to be presented.
|
||||||
uid := ctxt.Session().Values["user_id"].(int32)
|
uid := ctxt.CurrentUserId()
|
||||||
sboxes, err := database.AmGetSideboxes(uid)
|
sboxes, err := database.AmGetSideboxes(uid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "string", "Unable to retrieve sideboxes", err
|
return "string", "Unable to retrieve sideboxes", err
|
||||||
|
|||||||
+21
-6
@@ -25,7 +25,9 @@ import (
|
|||||||
|
|
||||||
// AmContext is the interface for Amsterdam's wrapper context that exposes the required functionality.
|
// AmContext is the interface for Amsterdam's wrapper context that exposes the required functionality.
|
||||||
type AmContext interface {
|
type AmContext interface {
|
||||||
|
ClearSession()
|
||||||
CurrentUser() *database.User
|
CurrentUser() *database.User
|
||||||
|
CurrentUserId() int32
|
||||||
FormField(string) string
|
FormField(string) string
|
||||||
FormFieldInt(string) (int, error)
|
FormFieldInt(string) (int, error)
|
||||||
FormFieldIsSet(string) bool
|
FormFieldIsSet(string) bool
|
||||||
@@ -35,8 +37,8 @@ type AmContext interface {
|
|||||||
RemoteIP() string
|
RemoteIP() string
|
||||||
Render(string) error
|
Render(string) error
|
||||||
ReplaceUser(*database.User)
|
ReplaceUser(*database.User)
|
||||||
|
SaveSession() error
|
||||||
SubRender(string) ([]byte, error)
|
SubRender(string) ([]byte, error)
|
||||||
Session() *sessions.Session
|
|
||||||
SetOutputType(string)
|
SetOutputType(string)
|
||||||
SetRC(int)
|
SetRC(int)
|
||||||
GetScratch(string) any
|
GetScratch(string) any
|
||||||
@@ -55,6 +57,14 @@ type amContext struct {
|
|||||||
session *sessions.Session
|
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.
|
// CurrentUser returns the current user from the session.
|
||||||
func (c *amContext) CurrentUser() *database.User {
|
func (c *amContext) CurrentUser() *database.User {
|
||||||
u, err := database.AmGetUser(c.session.Values["user_id"].(int32))
|
u, err := database.AmGetUser(c.session.Values["user_id"].(int32))
|
||||||
@@ -64,6 +74,11 @@ func (c *amContext) CurrentUser() *database.User {
|
|||||||
return u
|
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.
|
/* FormField returns the value of a form field from the request.
|
||||||
* Parameters:
|
* Parameters:
|
||||||
* name - The name of the field to retrieve.
|
* 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
|
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.
|
// Scratchpad returns the per-request scratchpad for values.
|
||||||
func (c *amContext) Scratchpad() map[string]any {
|
func (c *amContext) Scratchpad() map[string]any {
|
||||||
if c.scratchpad == nil {
|
if c.scratchpad == nil {
|
||||||
@@ -171,11 +191,6 @@ func (c *amContext) SubRender(name string) ([]byte, error) {
|
|||||||
return buf.Bytes(), err
|
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.
|
// SetOutputType sets the MIME output type for the current operation.
|
||||||
func (c *amContext) SetOutputType(typ string) {
|
func (c *amContext) SetOutputType(typ string) {
|
||||||
c.outputType = typ
|
c.outputType = typ
|
||||||
|
|||||||
+1
-1
@@ -79,7 +79,7 @@ func AmWrap(myfunc func(AmContext) (string, any, error)) echo.HandlerFunc {
|
|||||||
}
|
}
|
||||||
what, rc, err := myfunc(amctxt)
|
what, rc, err := myfunc(amctxt)
|
||||||
if err == nil {
|
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)
|
ctxt.Logger().Errorf("Session save error: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ func SetupSessionManager() {
|
|||||||
|
|
||||||
// SetupAmSession sets up a newly created Amsterdam session.
|
// SetupAmSession sets up a newly created Amsterdam session.
|
||||||
func SetupAmSession(session *sessions.Session) {
|
func SetupAmSession(session *sessions.Session) {
|
||||||
session.Values["temp"] = "Active"
|
|
||||||
u, err := database.AmGetAnonUser()
|
u, err := database.AmGetAnonUser()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
session.Values["user_id"] = u.Uid
|
session.Values["user_id"] = u.Uid
|
||||||
|
|||||||
+1
-1
@@ -65,7 +65,7 @@
|
|||||||
{{ else }}
|
{{ else }}
|
||||||
You are logged in as <b>{{ .CurrentUser().Username }}</b>
|
You are logged in as <b>{{ .CurrentUser().Username }}</b>
|
||||||
<span class="mx-2">-</span>
|
<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>
|
<span class="mx-2">|</span>
|
||||||
<a href="/TODO/profile" class="text-yellow-300 hover:text-yellow-400">Profile</a>
|
<a href="/TODO/profile" class="text-yellow-300 hover:text-yellow-400">Profile</a>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|||||||
Reference in New Issue
Block a user