Save credentials for google in the database
This commit is contained in:
@@ -5,12 +5,13 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
. "git.gorlug.de/code/ersteller"
|
|
||||||
"git.gorlug.de/code/ersteller/authentication"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
. "git.gorlug.de/code/ersteller"
|
||||||
|
"git.gorlug.de/code/ersteller/authentication"
|
||||||
|
|
||||||
"github.com/gorilla/sessions"
|
"github.com/gorilla/sessions"
|
||||||
|
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
@@ -39,6 +40,7 @@ type GoogleAuth struct {
|
|||||||
type Database interface {
|
type Database interface {
|
||||||
GetUserIdByEmail(ctx context.Context, email string) (int, error)
|
GetUserIdByEmail(ctx context.Context, email string) (int, error)
|
||||||
CreateUser(ctx context.Context, email string) (int, error)
|
CreateUser(ctx context.Context, email string) (int, error)
|
||||||
|
SaveCredentials(ctx context.Context, userId int, token *oauth2.Token) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type Environment struct {
|
type Environment struct {
|
||||||
@@ -180,7 +182,10 @@ func (g *GoogleAuth) getUserDataFromGoogle(code string) (GoogleUserData, error)
|
|||||||
|
|
||||||
func (g *GoogleAuth) SaveCredentials(userId int, token *oauth2.Token) error {
|
func (g *GoogleAuth) SaveCredentials(userId int, token *oauth2.Token) error {
|
||||||
Debug("saving google credentials for user ", userId)
|
Debug("saving google credentials for user ", userId)
|
||||||
// For now, we'll just log this - in a real implementation you'd save to database
|
err := g.db.SaveCredentials(context.Background(), userId, token)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
Debug("saved credentials for user", userId)
|
Debug("saved credentials for user", userId)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,14 @@ package google
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"git.gorlug.de/code/ersteller"
|
"git.gorlug.de/code/ersteller"
|
||||||
google_http "git.gorlug.de/code/ersteller/authentication/google/http"
|
google_http "git.gorlug.de/code/ersteller/authentication/google/http"
|
||||||
"git.gorlug.de/code/ersteller/starter/ent"
|
"git.gorlug.de/code/ersteller/starter/ent"
|
||||||
|
"git.gorlug.de/code/ersteller/starter/ent/googleauth"
|
||||||
|
"git.gorlug.de/code/ersteller/starter/ent/schema"
|
||||||
"git.gorlug.de/code/ersteller/starter/ent/user"
|
"git.gorlug.de/code/ersteller/starter/ent/user"
|
||||||
|
"golang.org/x/oauth2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Database struct {
|
type Database struct {
|
||||||
@@ -30,3 +34,19 @@ func (d *Database) CreateUser(ctx context.Context, email string) (int, error) {
|
|||||||
}
|
}
|
||||||
return newUser.ID, nil
|
return newUser.ID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Database) SaveCredentials(ctx context.Context, userId int, token *oauth2.Token) error {
|
||||||
|
existing, err := d.db.GoogleAuth.Query().Where(googleauth.HasUserWith(user.ID(userId))).Only(ctx)
|
||||||
|
if err == nil {
|
||||||
|
_, err = existing.Update().SetCredentials(schema.Credentials{Token: *token}).Save(ctx)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !ent.IsNotFound(err) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = d.db.GoogleAuth.Create().
|
||||||
|
SetUserID(userId).
|
||||||
|
SetCredentials(schema.Credentials{Token: *token}).
|
||||||
|
Save(ctx)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user