From 15e2717ea814cdcd9dfaf8dc4a76604c5db48a97 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 17 Jun 2017 22:19:26 +0200 Subject: [PATCH] Heavily improved sessions --- auth/auth.go | 5 ++++- auth/google.go | 4 ++++ main.go | 5 +++-- pages/frontpage/frontpage.pixy | 6 +++--- utils/user.go | 4 ++++ 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/auth/auth.go b/auth/auth.go index c1777ecd..83f1259e 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -9,7 +9,10 @@ func Install(app *aero.Application) { // Logout app.Get("/logout", func(ctx *aero.Context) string { - ctx.Session().Set("userId", nil) + if ctx.HasSession() { + ctx.Session().Set("userId", nil) + } + return ctx.Redirect("/") }) } diff --git a/auth/google.go b/auth/google.go index 9df3b38d..9c9c589e 100644 --- a/auth/google.go +++ b/auth/google.go @@ -50,6 +50,10 @@ func InstallGoogleAuth(app *aero.Application) { // Auth Callback app.Get("/auth/google/callback", func(ctx *aero.Context) string { + if !ctx.HasSession() { + return ctx.Error(http.StatusUnauthorized, "Session does not exist", errors.New("Google login failed: Session does not exist")) + } + session := ctx.Session() if session.ID() != ctx.Query("state") { diff --git a/main.go b/main.go index 388cd7e8..21f60df3 100644 --- a/main.go +++ b/main.go @@ -31,8 +31,9 @@ func main() { // CSS app.SetStyle(components.CSS()) - // Session store - app.Sessions.Store = arn.NewAerospikeStore("Session") + // Sessions + app.Sessions.Duration = 3600 * 24 + app.Sessions.Store = arn.NewAerospikeStore("Session", app.Sessions.Duration) // Layout app.Layout = layout.Render diff --git a/pages/frontpage/frontpage.pixy b/pages/frontpage/frontpage.pixy index ffedef25..2af7a285 100644 --- a/pages/frontpage/frontpage.pixy +++ b/pages/frontpage/frontpage.pixy @@ -6,6 +6,6 @@ component FrontPage p a(href="https://github.com/animenotifier/notify.moe", target="_blank", rel="noopener") Source on GitHub - //- .login-buttons - //- a.login-button(href="/auth/google") - //- img.login-button-image(src="/images/login/google", alt="Google Login", title="Login with your Google account") \ No newline at end of file + .login-buttons + a.login-button(href="/auth/google") + img.login-button-image(src="/images/login/google", alt="Google Login", title="Login with your Google account") \ No newline at end of file diff --git a/utils/user.go b/utils/user.go index 194c5498..ff52a7df 100644 --- a/utils/user.go +++ b/utils/user.go @@ -7,6 +7,10 @@ import ( // GetUser ... func GetUser(ctx *aero.Context) *arn.User { + if !ctx.HasSession() { + return nil + } + userID := ctx.Session().GetString("userId") if userID == "" {