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)
}
// 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
if getErr == nil && user != nil {
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() {
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()) {

View File

@ -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