From 938057a6265633cc5a95bb707b667cda63179b49 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Thu, 15 Jun 2017 21:59:14 +0200 Subject: [PATCH] Implemented logout --- google.go | 22 +++++++++++++--------- logger.go | 4 ++-- login.go | 6 ++++++ 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/google.go b/google.go index 42707433..10ce5f35 100644 --- a/google.go +++ b/google.go @@ -81,18 +81,22 @@ func EnableGoogleLogin(app *aero.Application) { return ctx.Error(http.StatusBadRequest, "Failed parsing user data (JSON)", err) } - // Try to find an existing user by the associated e-mail address - email := googleUser.Email - user, getErr := arn.GetUserByEmail(email) + // Try to find an existing user by the Google user ID + user, getErr := arn.GetUserFromTable("GoogleToUser", googleUser.Sub) - if getErr != nil { - return ctx.Error(http.StatusForbidden, "Email not registered", err) + if getErr == nil && user != nil { + session.Set("userId", user.ID) + return ctx.Redirect("/") } - // Login - session.Set("userId", user.ID) + // Try to find an existing user by the associated e-mail address + user, getErr = arn.GetUserByEmail(googleUser.Email) - // Redirect back to frontpage - return ctx.Redirect("/") + if getErr == nil && user != nil { + session.Set("userId", user.ID) + return ctx.Redirect("/") + } + + return ctx.Error(http.StatusForbidden, "Account does not exist", err) }) } diff --git a/logger.go b/logger.go index a3a71fbb..c8bd9911 100644 --- a/logger.go +++ b/logger.go @@ -12,11 +12,11 @@ import ( ) func init() { - err := log.NewChannel("error") + err := log.NewLog() err.AddOutput(log.File("logs/error.log")) err.AddOutput(os.Stderr) - request := log.NewChannel("request") + request := log.NewLog() request.AddOutput(log.File("logs/request.log")) app.Use(func(ctx *aero.Context, next func()) { diff --git a/login.go b/login.go index e43e203e..328d50ca 100644 --- a/login.go +++ b/login.go @@ -7,6 +7,12 @@ func EnableLogin(app *aero.Application) { // Google EnableGoogleLogin(app) + // Logout + app.Get("/logout", func(ctx *aero.Context) string { + ctx.Session().Set("userId", nil) + return ctx.Redirect("/") + }) + // Session middleware app.Use(func(ctx *aero.Context, next func()) { // Handle the request first