Implemented Google login
This commit is contained in:
parent
3cc9ba54c6
commit
c222a814e4
47
google.go
47
google.go
@ -4,14 +4,27 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/aerogo/aero"
|
"github.com/aerogo/aero"
|
||||||
|
"github.com/animenotifier/arn"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"golang.org/x/oauth2/google"
|
"golang.org/x/oauth2/google"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GoogleUser is the user data we receive from Google
|
||||||
|
type GoogleUser struct {
|
||||||
|
Sub string `json:"sub"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
GivenName string `json:"given_name"`
|
||||||
|
FamilyName string `json:"family_name"`
|
||||||
|
Profile string `json:"profile"`
|
||||||
|
Picture string `json:"picture"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
EmailVerified bool `json:"email_verified"`
|
||||||
|
Gender string `json:"gender"`
|
||||||
|
}
|
||||||
|
|
||||||
// EnableGoogleLogin enables Google login for the app.
|
// EnableGoogleLogin enables Google login for the app.
|
||||||
func EnableGoogleLogin(app *aero.Application) {
|
func EnableGoogleLogin(app *aero.Application) {
|
||||||
var api APIKeys
|
var api APIKeys
|
||||||
@ -30,7 +43,8 @@ func EnableGoogleLogin(app *aero.Application) {
|
|||||||
|
|
||||||
// Auth
|
// Auth
|
||||||
app.Get("/auth/google", func(ctx *aero.Context) string {
|
app.Get("/auth/google", func(ctx *aero.Context) string {
|
||||||
url := conf.AuthCodeURL(ctx.Session().ID())
|
sessionID := ctx.Session().ID()
|
||||||
|
url := conf.AuthCodeURL(sessionID)
|
||||||
ctx.Redirect(url)
|
ctx.Redirect(url)
|
||||||
return ""
|
return ""
|
||||||
})
|
})
|
||||||
@ -38,11 +52,12 @@ func EnableGoogleLogin(app *aero.Application) {
|
|||||||
// Auth Callback
|
// Auth Callback
|
||||||
app.Get("/auth/google/callback", func(ctx *aero.Context) string {
|
app.Get("/auth/google/callback", func(ctx *aero.Context) string {
|
||||||
if ctx.Session().ID() != ctx.Query("state") {
|
if ctx.Session().ID() != ctx.Query("state") {
|
||||||
return ctx.Error(http.StatusBadRequest, "Authorization not allowed for this session", errors.New("Google login failed: Incorrect state"))
|
return ctx.Error(http.StatusUnauthorized, "Authorization not allowed for this session", errors.New("Google login failed: Incorrect state"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle the exchange code to initiate a transport
|
// Handle the exchange code to initiate a transport
|
||||||
token, err := conf.Exchange(oauth2.NoContext, ctx.Query("code"))
|
token, err := conf.Exchange(oauth2.NoContext, ctx.Query("code"))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ctx.Error(http.StatusBadRequest, "Could not obtain OAuth token", err)
|
return ctx.Error(http.StatusBadRequest, "Could not obtain OAuth token", err)
|
||||||
}
|
}
|
||||||
@ -51,14 +66,30 @@ func EnableGoogleLogin(app *aero.Application) {
|
|||||||
client := conf.Client(oauth2.NoContext, token)
|
client := conf.Client(oauth2.NoContext, token)
|
||||||
|
|
||||||
resp, err := client.Get("https://www.googleapis.com/oauth2/v3/userinfo")
|
resp, err := client.Get("https://www.googleapis.com/oauth2/v3/userinfo")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ctx.Error(http.StatusBadRequest, "Failed requesting user data from Google", err)
|
return ctx.Error(http.StatusBadRequest, "Failed requesting user data from Google", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
|
||||||
dataBytes, _ := ioutil.ReadAll(resp.Body)
|
|
||||||
data := string(dataBytes)
|
|
||||||
log.Println("Resp body: ", data)
|
|
||||||
|
|
||||||
return ctx.Text(data)
|
defer resp.Body.Close()
|
||||||
|
data, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
|
||||||
|
var googleUser GoogleUser
|
||||||
|
err = json.Unmarshal(data, &googleUser)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return ctx.Error(http.StatusBadRequest, "Failed parsing user data (JSON)", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
email := googleUser.Email
|
||||||
|
user, getErr := arn.GetUserByEmail(email)
|
||||||
|
|
||||||
|
if getErr != nil {
|
||||||
|
return ctx.Error(http.StatusForbidden, "Email not registered", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.Session().Set("userId", user.ID)
|
||||||
|
|
||||||
|
return ctx.Redirect("/")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sort"
|
||||||
|
|
||||||
"github.com/animenotifier/arn"
|
"github.com/animenotifier/arn"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
@ -16,7 +18,9 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// sort.Slice
|
sort.Slice(animeList, func(i, j int) bool {
|
||||||
|
return animeList[i].StartDate > animeList[j].StartDate
|
||||||
|
})
|
||||||
|
|
||||||
// Convert to small anime list
|
// Convert to small anime list
|
||||||
cache := &arn.ListOfIDs{}
|
cache := &arn.ListOfIDs{}
|
||||||
|
@ -10,6 +10,7 @@ func main() {
|
|||||||
|
|
||||||
// Delete Nick:User records
|
// Delete Nick:User records
|
||||||
arn.Truncate("NickToUser")
|
arn.Truncate("NickToUser")
|
||||||
|
arn.Truncate("EmailToUser")
|
||||||
|
|
||||||
// Get a stream of all anime
|
// Get a stream of all anime
|
||||||
allUsers, err := arn.AllUsers()
|
allUsers, err := arn.AllUsers()
|
||||||
@ -24,7 +25,8 @@ func main() {
|
|||||||
count++
|
count++
|
||||||
println(count, user.Nick)
|
println(count, user.Nick)
|
||||||
|
|
||||||
user.ChangeNick(user.Nick)
|
user.SetNick(user.Nick)
|
||||||
|
user.SetEmail(user.Email)
|
||||||
}
|
}
|
||||||
|
|
||||||
color.Green("Finished.")
|
color.Green("Finished.")
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
package dashboard
|
package dashboard
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/aerogo/aero"
|
"github.com/aerogo/aero"
|
||||||
|
"github.com/animenotifier/arn"
|
||||||
)
|
)
|
||||||
|
|
||||||
const maxPosts = 5
|
const maxPosts = 5
|
||||||
@ -21,5 +24,17 @@ func Get(ctx *aero.Context) string {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
// return ctx.HTML(components.Dashboard(posts))
|
// return ctx.HTML(components.Dashboard(posts))
|
||||||
|
userID := ctx.Session().GetString("userId")
|
||||||
|
|
||||||
|
if userID != "" {
|
||||||
|
user, err := arn.GetUser(userID)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return ctx.Error(http.StatusInternalServerError, "Error fetching user data", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx.HTML("Welcome back, " + user.Nick + "!")
|
||||||
|
}
|
||||||
|
|
||||||
return ctx.HTML("ARN 4.0 is currently under construction.<br><a href='https://paypal.me/blitzprog' target='_blank' rel='noopener'>Support the development</a><br><a href='/auth/google'>Login via Google</a>")
|
return ctx.HTML("ARN 4.0 is currently under construction.<br><a href='https://paypal.me/blitzprog' target='_blank' rel='noopener'>Support the development</a><br><a href='/auth/google'>Login via Google</a>")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user