Upgraded to latest aero version
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/notify.moe/utils"
|
||||
)
|
||||
@ -19,17 +21,17 @@ func Install(app *aero.Application) {
|
||||
InstallTwitterAuth(app)
|
||||
|
||||
// Logout
|
||||
app.Get("/logout", func(ctx *aero.Context) string {
|
||||
app.Get("/logout", func(ctx aero.Context) error {
|
||||
if ctx.HasSession() {
|
||||
user := utils.GetUser(ctx)
|
||||
|
||||
if user != nil {
|
||||
authLog.Info("%s logged out | %s | %s | %s | %s", user.Nick, user.ID, ctx.RealIP(), user.Email, user.RealName())
|
||||
authLog.Info("%s logged out | %s | %s | %s | %s", user.Nick, user.ID, ctx.IP(), user.Email, user.RealName())
|
||||
}
|
||||
|
||||
ctx.Session().Delete("userId")
|
||||
}
|
||||
|
||||
return ctx.Redirect("/")
|
||||
return ctx.Redirect(http.StatusFound, "/")
|
||||
})
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/arn"
|
||||
"github.com/animenotifier/notify.moe/assets"
|
||||
"github.com/animenotifier/notify.moe/utils"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"golang.org/x/oauth2"
|
||||
@ -33,7 +34,7 @@ func InstallFacebookAuth(app *aero.Application) {
|
||||
config := &oauth2.Config{
|
||||
ClientID: arn.APIKeys.Facebook.ID,
|
||||
ClientSecret: arn.APIKeys.Facebook.Secret,
|
||||
RedirectURL: "https://" + app.Config.Domain + "/auth/facebook/callback",
|
||||
RedirectURL: "https://" + assets.Domain + "/auth/facebook/callback",
|
||||
Scopes: []string{
|
||||
"public_profile",
|
||||
"email",
|
||||
@ -44,10 +45,10 @@ func InstallFacebookAuth(app *aero.Application) {
|
||||
// When a user visits /auth/facebook, we ask OAuth2 config for a URL
|
||||
// to redirect the user to. Once the user has logged in on that page,
|
||||
// he'll be redirected back to our servers to the callback page.
|
||||
app.Get("/auth/facebook", func(ctx *aero.Context) string {
|
||||
app.Get("/auth/facebook", func(ctx aero.Context) error {
|
||||
state := ctx.Session().ID()
|
||||
url := config.AuthCodeURL(state)
|
||||
return ctx.Redirect(url)
|
||||
return ctx.Redirect(http.StatusFound, url)
|
||||
})
|
||||
|
||||
// This is the redirect URL that we specified in the OAuth2 config.
|
||||
@ -55,7 +56,7 @@ func InstallFacebookAuth(app *aero.Application) {
|
||||
// Now we have to check for fraud requests and request user information.
|
||||
// If both Facebook ID and email can't be found in our DB, register a new user.
|
||||
// Otherwise, log in the user with the given Facebook ID or email.
|
||||
app.Get("/auth/facebook/callback", func(ctx *aero.Context) string {
|
||||
app.Get("/auth/facebook/callback", func(ctx aero.Context) error {
|
||||
if !ctx.HasSession() {
|
||||
return ctx.Error(http.StatusUnauthorized, "Facebook login failed", errors.New("Session does not exist"))
|
||||
}
|
||||
@ -108,9 +109,9 @@ func InstallFacebookAuth(app *aero.Application) {
|
||||
user.Save()
|
||||
|
||||
// Log
|
||||
authLog.Info("Added Facebook ID to existing account | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.RealIP(), user.Email, user.RealName())
|
||||
authLog.Info("Added Facebook ID to existing account | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.IP(), user.Email, user.RealName())
|
||||
|
||||
return ctx.Redirect("/")
|
||||
return ctx.Redirect(http.StatusFound, "/")
|
||||
}
|
||||
|
||||
var getErr error
|
||||
@ -119,7 +120,7 @@ func InstallFacebookAuth(app *aero.Application) {
|
||||
user, getErr = arn.GetUserByFacebookID(fbUser.ID)
|
||||
|
||||
if getErr == nil && user != nil {
|
||||
authLog.Info("User logged in via Facebook ID | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.RealIP(), user.Email, user.RealName())
|
||||
authLog.Info("User logged in via Facebook ID | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.IP(), user.Email, user.RealName())
|
||||
|
||||
// Add FacebookToUser reference
|
||||
user.ConnectFacebook(fbUser.ID)
|
||||
@ -128,20 +129,20 @@ func InstallFacebookAuth(app *aero.Application) {
|
||||
user.Save()
|
||||
|
||||
session.Set("userId", user.ID)
|
||||
return ctx.Redirect("/")
|
||||
return ctx.Redirect(http.StatusFound, "/")
|
||||
}
|
||||
|
||||
// Try to find an existing user via the associated e-mail address
|
||||
user, getErr = arn.GetUserByEmail(fbUser.Email)
|
||||
|
||||
if getErr == nil && user != nil {
|
||||
authLog.Info("User logged in via Email | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.RealIP(), user.Email, user.RealName())
|
||||
authLog.Info("User logged in via Email | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.IP(), user.Email, user.RealName())
|
||||
|
||||
user.LastLogin = arn.DateTimeUTC()
|
||||
user.Save()
|
||||
|
||||
session.Set("userId", user.ID)
|
||||
return ctx.Redirect("/")
|
||||
return ctx.Redirect(http.StatusFound, "/")
|
||||
}
|
||||
|
||||
// Register new user
|
||||
@ -169,9 +170,9 @@ func InstallFacebookAuth(app *aero.Application) {
|
||||
session.Set("userId", user.ID)
|
||||
|
||||
// Log
|
||||
authLog.Info("Registered new user via Facebook | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.RealIP(), user.Email, user.RealName())
|
||||
authLog.Info("Registered new user via Facebook | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.IP(), user.Email, user.RealName())
|
||||
|
||||
// Redirect to starting page for new users
|
||||
return ctx.Redirect(newUserStartRoute)
|
||||
return ctx.Redirect(http.StatusFound, newUserStartRoute)
|
||||
})
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/arn"
|
||||
"github.com/animenotifier/notify.moe/assets"
|
||||
"github.com/animenotifier/notify.moe/utils"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"golang.org/x/oauth2"
|
||||
@ -38,7 +39,7 @@ func InstallGoogleAuth(app *aero.Application) {
|
||||
config := &oauth2.Config{
|
||||
ClientID: arn.APIKeys.Google.ID,
|
||||
ClientSecret: arn.APIKeys.Google.Secret,
|
||||
RedirectURL: "https://" + app.Config.Domain + "/auth/google/callback",
|
||||
RedirectURL: "https://" + assets.Domain + "/auth/google/callback",
|
||||
Scopes: []string{
|
||||
"https://www.googleapis.com/auth/userinfo.email",
|
||||
"https://www.googleapis.com/auth/userinfo.profile",
|
||||
@ -51,10 +52,10 @@ func InstallGoogleAuth(app *aero.Application) {
|
||||
// When a user visits /auth/google, we ask OAuth2 config for a URL
|
||||
// to redirect the user to. Once the user has logged in on that page,
|
||||
// he'll be redirected back to our servers to the callback page.
|
||||
app.Get("/auth/google", func(ctx *aero.Context) string {
|
||||
app.Get("/auth/google", func(ctx aero.Context) error {
|
||||
state := ctx.Session().ID()
|
||||
url := config.AuthCodeURL(state)
|
||||
return ctx.Redirect(url)
|
||||
return ctx.Redirect(http.StatusFound, url)
|
||||
})
|
||||
|
||||
// This is the redirect URL that we specified in the OAuth2 config.
|
||||
@ -62,7 +63,7 @@ func InstallGoogleAuth(app *aero.Application) {
|
||||
// Now we have to check for fraud requests and request user information.
|
||||
// If both Google ID and email can't be found in our DB, register a new user.
|
||||
// Otherwise, log in the user with the given Google ID or email.
|
||||
app.Get("/auth/google/callback", func(ctx *aero.Context) string {
|
||||
app.Get("/auth/google/callback", func(ctx aero.Context) error {
|
||||
if !ctx.HasSession() {
|
||||
return ctx.Error(http.StatusUnauthorized, "Google login failed", errors.New("Session does not exist"))
|
||||
}
|
||||
@ -119,9 +120,9 @@ func InstallGoogleAuth(app *aero.Application) {
|
||||
user.Save()
|
||||
|
||||
// Log
|
||||
authLog.Info("Added Google ID to existing account | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.RealIP(), user.Email, user.RealName())
|
||||
authLog.Info("Added Google ID to existing account | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.IP(), user.Email, user.RealName())
|
||||
|
||||
return ctx.Redirect("/")
|
||||
return ctx.Redirect(http.StatusFound, "/")
|
||||
}
|
||||
|
||||
var getErr error
|
||||
@ -130,20 +131,20 @@ func InstallGoogleAuth(app *aero.Application) {
|
||||
user, getErr = arn.GetUserByGoogleID(googleUser.Sub)
|
||||
|
||||
if getErr == nil && user != nil {
|
||||
authLog.Info("User logged in via Google ID | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.RealIP(), user.Email, user.RealName())
|
||||
authLog.Info("User logged in via Google ID | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.IP(), user.Email, user.RealName())
|
||||
|
||||
user.LastLogin = arn.DateTimeUTC()
|
||||
user.Save()
|
||||
|
||||
session.Set("userId", user.ID)
|
||||
return ctx.Redirect("/")
|
||||
return ctx.Redirect(http.StatusFound, "/")
|
||||
}
|
||||
|
||||
// Try to find an existing user via the associated e-mail address
|
||||
user, getErr = arn.GetUserByEmail(googleUser.Email)
|
||||
|
||||
if getErr == nil && user != nil {
|
||||
authLog.Info("User logged in via Email | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.RealIP(), user.Email, user.RealName())
|
||||
authLog.Info("User logged in via Email | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.IP(), user.Email, user.RealName())
|
||||
|
||||
// Add GoogleToUser reference
|
||||
user.ConnectGoogle(googleUser.Sub)
|
||||
@ -152,7 +153,7 @@ func InstallGoogleAuth(app *aero.Application) {
|
||||
user.Save()
|
||||
|
||||
session.Set("userId", user.ID)
|
||||
return ctx.Redirect("/")
|
||||
return ctx.Redirect(http.StatusFound, "/")
|
||||
}
|
||||
|
||||
// Register new user
|
||||
@ -180,9 +181,9 @@ func InstallGoogleAuth(app *aero.Application) {
|
||||
session.Set("userId", user.ID)
|
||||
|
||||
// Log
|
||||
authLog.Info("Registered new user via Google | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.RealIP(), user.Email, user.RealName())
|
||||
authLog.Info("Registered new user via Google | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.IP(), user.Email, user.RealName())
|
||||
|
||||
// Redirect to starting page for new users
|
||||
return ctx.Redirect(newUserStartRoute)
|
||||
return ctx.Redirect(http.StatusFound, newUserStartRoute)
|
||||
})
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/arn"
|
||||
"github.com/animenotifier/notify.moe/assets"
|
||||
"github.com/animenotifier/notify.moe/utils"
|
||||
"github.com/gomodule/oauth1/oauth"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
@ -42,8 +43,8 @@ func InstallTwitterAuth(app *aero.Application) {
|
||||
// a request token and give us a URL to redirect the user to.
|
||||
// Once the user has approved the application on that page,
|
||||
// he'll be redirected back to our servers to the callback page.
|
||||
app.Get("/auth/twitter", func(ctx *aero.Context) string {
|
||||
callback := "https://" + ctx.App.Config.Domain + "/auth/twitter/callback"
|
||||
app.Get("/auth/twitter", func(ctx aero.Context) error {
|
||||
callback := "https://" + assets.Domain + "/auth/twitter/callback"
|
||||
tempCred, err := config.RequestTemporaryCredentials(nil, callback, nil)
|
||||
|
||||
if err != nil {
|
||||
@ -52,7 +53,7 @@ func InstallTwitterAuth(app *aero.Application) {
|
||||
|
||||
ctx.Session().Set("tempCred", tempCred)
|
||||
url := config.AuthorizationURL(tempCred, nil)
|
||||
return ctx.Redirect(url)
|
||||
return ctx.Redirect(http.StatusFound, url)
|
||||
})
|
||||
|
||||
// This is the redirect URL that we specified in /auth/twitter.
|
||||
@ -60,7 +61,7 @@ func InstallTwitterAuth(app *aero.Application) {
|
||||
// Now we have to check for fraud requests and request user information.
|
||||
// If both Twitter ID and email can't be found in our DB, register a new user.
|
||||
// Otherwise, log in the user with the given Twitter ID or email.
|
||||
app.Get("/auth/twitter/callback", func(ctx *aero.Context) string {
|
||||
app.Get("/auth/twitter/callback", func(ctx aero.Context) error {
|
||||
if !ctx.HasSession() {
|
||||
return ctx.Error(http.StatusUnauthorized, "Twitter login failed", errors.New("Session does not exist"))
|
||||
}
|
||||
@ -121,9 +122,9 @@ func InstallTwitterAuth(app *aero.Application) {
|
||||
user.Save()
|
||||
|
||||
// Log
|
||||
authLog.Info("Added Twitter ID to existing account | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.RealIP(), user.Email, user.RealName())
|
||||
authLog.Info("Added Twitter ID to existing account | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.IP(), user.Email, user.RealName())
|
||||
|
||||
return ctx.Redirect("/")
|
||||
return ctx.Redirect(http.StatusFound, "/")
|
||||
}
|
||||
|
||||
var getErr error
|
||||
@ -132,20 +133,20 @@ func InstallTwitterAuth(app *aero.Application) {
|
||||
user, getErr = arn.GetUserByTwitterID(twUser.ID)
|
||||
|
||||
if getErr == nil && user != nil {
|
||||
authLog.Info("User logged in via Twitter ID | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.RealIP(), user.Email, user.RealName())
|
||||
authLog.Info("User logged in via Twitter ID | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.IP(), user.Email, user.RealName())
|
||||
|
||||
user.LastLogin = arn.DateTimeUTC()
|
||||
user.Save()
|
||||
|
||||
session.Set("userId", user.ID)
|
||||
return ctx.Redirect("/")
|
||||
return ctx.Redirect(http.StatusFound, "/")
|
||||
}
|
||||
|
||||
// Try to find an existing user via the associated e-mail address
|
||||
user, getErr = arn.GetUserByEmail(twUser.Email)
|
||||
|
||||
if getErr == nil && user != nil {
|
||||
authLog.Info("User logged in via Email | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.RealIP(), user.Email, user.RealName())
|
||||
authLog.Info("User logged in via Email | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.IP(), user.Email, user.RealName())
|
||||
|
||||
// Add TwitterToUser reference
|
||||
user.ConnectTwitter(twUser.ID)
|
||||
@ -155,7 +156,7 @@ func InstallTwitterAuth(app *aero.Application) {
|
||||
user.Save()
|
||||
|
||||
session.Set("userId", user.ID)
|
||||
return ctx.Redirect("/")
|
||||
return ctx.Redirect(http.StatusFound, "/")
|
||||
}
|
||||
|
||||
// Register new user
|
||||
@ -187,9 +188,9 @@ func InstallTwitterAuth(app *aero.Application) {
|
||||
session.Set("userId", user.ID)
|
||||
|
||||
// Log
|
||||
authLog.Info("Registered new user via Twitter | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.RealIP(), user.Email, user.RealName())
|
||||
authLog.Info("Registered new user via Twitter | %s | %s | %s | %s | %s", user.Nick, user.ID, ctx.IP(), user.Email, user.RealName())
|
||||
|
||||
// Redirect to starting page for new users
|
||||
return ctx.Redirect(newUserStartRoute)
|
||||
return ctx.Redirect(http.StatusFound, newUserStartRoute)
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user