Implemented logout

This commit is contained in:
Eduard Urbach 2017-06-15 21:59:14 +02:00
parent 1790bce104
commit 938057a626
3 changed files with 21 additions and 11 deletions

View File

@ -81,18 +81,22 @@ func EnableGoogleLogin(app *aero.Application) {
return ctx.Error(http.StatusBadRequest, "Failed parsing user data (JSON)", err) return ctx.Error(http.StatusBadRequest, "Failed parsing user data (JSON)", err)
} }
// Try to find an existing user by the associated e-mail address // Try to find an existing user by the Google user ID
email := googleUser.Email user, getErr := arn.GetUserFromTable("GoogleToUser", googleUser.Sub)
user, getErr := arn.GetUserByEmail(email)
if getErr != nil { if getErr == nil && user != nil {
return ctx.Error(http.StatusForbidden, "Email not registered", err) session.Set("userId", user.ID)
return ctx.Redirect("/")
} }
// Login // Try to find an existing user by the associated e-mail address
session.Set("userId", user.ID) user, getErr = arn.GetUserByEmail(googleUser.Email)
// Redirect back to frontpage if getErr == nil && user != nil {
return ctx.Redirect("/") session.Set("userId", user.ID)
return ctx.Redirect("/")
}
return ctx.Error(http.StatusForbidden, "Account does not exist", err)
}) })
} }

View File

@ -12,11 +12,11 @@ import (
) )
func init() { func init() {
err := log.NewChannel("error") err := log.NewLog()
err.AddOutput(log.File("logs/error.log")) err.AddOutput(log.File("logs/error.log"))
err.AddOutput(os.Stderr) err.AddOutput(os.Stderr)
request := log.NewChannel("request") request := log.NewLog()
request.AddOutput(log.File("logs/request.log")) request.AddOutput(log.File("logs/request.log"))
app.Use(func(ctx *aero.Context, next func()) { app.Use(func(ctx *aero.Context, next func()) {

View File

@ -7,6 +7,12 @@ func EnableLogin(app *aero.Application) {
// Google // Google
EnableGoogleLogin(app) EnableGoogleLogin(app)
// Logout
app.Get("/logout", func(ctx *aero.Context) string {
ctx.Session().Set("userId", nil)
return ctx.Redirect("/")
})
// Session middleware // Session middleware
app.Use(func(ctx *aero.Context, next func()) { app.Use(func(ctx *aero.Context, next func()) {
// Handle the request first // Handle the request first