Commit ea5d15f7 authored by Mo Firouz's avatar Mo Firouz
Browse files

Store email, avatar and DisplayName if social provider has one. (#612)

parent cc258714
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr

## [Unreleased]
### Changed
- Store email, avatar URL and display name provided by Apple, Facebook and Google login providers.
- Change runtime group add/kick/promote/demote APIs to include optional callerID parameter for permission checking. If callerID is an empty string it defaults to the admin user.
### Fixed
- Fix reading Lua authoritative match states that contain functions.
+8 −7
Original line number Diff line number Diff line
@@ -20,11 +20,12 @@ import (
	"encoding/json"
	"errors"
	"fmt"
	"google.golang.org/protobuf/types/known/timestamppb"
	"strconv"
	"strings"
	"time"

	"google.golang.org/protobuf/types/known/timestamppb"

	"github.com/gofrs/uuid"
	"github.com/heroiclabs/nakama-common/api"
	"github.com/heroiclabs/nakama/v3/social"
@@ -77,8 +78,8 @@ func AuthenticateApple(ctx context.Context, logger *zap.Logger, db *sql.DB, clie

	// Create a new account.
	userID := uuid.Must(uuid.NewV4()).String()
	query = "INSERT INTO users (id, username, apple_id, create_time, update_time) VALUES ($1, $2, $3, now(), now())"
	result, err := db.ExecContext(ctx, query, userID, username, profile.ID)
	query = "INSERT INTO users (id, username, email, apple_id, create_time, update_time) VALUES ($1, $2, $3, $4, now(), now())"
	result, err := db.ExecContext(ctx, query, userID, username, profile.Email, profile.ID)
	if err != nil {
		if e, ok := err.(pgx.PgError); ok && e.Code == dbErrorUniqueViolation {
			if strings.Contains(e.Message, "users_username_key") {
@@ -431,8 +432,8 @@ func AuthenticateFacebook(ctx context.Context, logger *zap.Logger, db *sql.DB, c

	// Create a new account.
	userID := uuid.Must(uuid.NewV4()).String()
	query = "INSERT INTO users (id, username, facebook_id, create_time, update_time) VALUES ($1, $2, $3, now(), now())"
	result, err := db.ExecContext(ctx, query, userID, username, facebookProfile.ID)
	query = "INSERT INTO users (id, username, display_name, email, avatar_url, facebook_id, create_time, update_time) VALUES ($1, $2, $3, $4, $5, $6, now(), now())"
	result, err := db.ExecContext(ctx, query, userID, username, facebookProfile.Name, facebookProfile.Email, facebookProfile.Picture, facebookProfile.ID)
	if err != nil {
		if e, ok := err.(pgx.PgError); ok && e.Code == dbErrorUniqueViolation {
			if strings.Contains(e.Message, "users_username_key") {
@@ -670,8 +671,8 @@ func AuthenticateGoogle(ctx context.Context, logger *zap.Logger, db *sql.DB, cli

	// Create a new account.
	userID := uuid.Must(uuid.NewV4()).String()
	query = "INSERT INTO users (id, username, google_id, display_name, avatar_url, create_time, update_time) VALUES ($1, $2, $3, $4, $5, now(), now())"
	result, err := db.ExecContext(ctx, query, userID, username, googleProfile.Sub, displayName, avatarURL)
	query = "INSERT INTO users (id, username, email, google_id, display_name, avatar_url, create_time, update_time) VALUES ($1, $2, $3, $4, $5, $6, now(), now())"
	result, err := db.ExecContext(ctx, query, userID, username, googleProfile.Email, googleProfile.Sub, displayName, avatarURL)
	if err != nil {
		if e, ok := err.(pgx.PgError); ok && e.Code == dbErrorUniqueViolation {
			if strings.Contains(e.Message, "users_username_key") {
+12 −11
Original line number Diff line number Diff line
@@ -17,6 +17,9 @@ package server
import (
	"context"
	"database/sql"
	"strconv"
	"strings"

	"github.com/gofrs/uuid"
	"github.com/heroiclabs/nakama/v3/social"
	"github.com/jackc/pgx"
@@ -24,8 +27,6 @@ import (
	"golang.org/x/crypto/bcrypt"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"strconv"
	"strings"
)

func LinkApple(ctx context.Context, logger *zap.Logger, db *sql.DB, config Config, socialClient *social.Client, userID uuid.UUID, token string) error {
@@ -44,15 +45,15 @@ func LinkApple(ctx context.Context, logger *zap.Logger, db *sql.DB, config Confi
	}

	res, err := db.ExecContext(ctx, `
UPDATE users
SET apple_id = $2, update_time = now()
UPDATE users AS u
SET apple_id = $2, email = COALESCE(NULLIF(u.email, ''), $3), update_time = now()
WHERE (id = $1)
AND (NOT EXISTS
    (SELECT id
     FROM users
     WHERE apple_id = $2 AND NOT id = $1))`,
		userID,
		profile.ID)
		profile.ID, profile.Email)

	if err != nil {
		logger.Error("Could not link Apple ID.", zap.Error(err), zap.Any("input", token))
@@ -202,15 +203,15 @@ func LinkFacebook(ctx context.Context, logger *zap.Logger, db *sql.DB, socialCli
	}

	res, err := db.ExecContext(ctx, `
UPDATE users
SET facebook_id = $2, update_time = now()
UPDATE users AS u
SET facebook_id = $2, display_name = COALESCE(NULLIF(u.display_name, ''), $3), email = COALESCE(NULLIF(u.email, ''), $4), avatar_url = COALESCE(NULLIF(u.avatar_url, ''), $5), update_time = now()
WHERE (id = $1)
AND (NOT EXISTS
    (SELECT id
     FROM users
     WHERE facebook_id = $2 AND NOT id = $1))`,
		userID,
		facebookProfile.ID)
		facebookProfile.ID, facebookProfile.Name, facebookProfile.Email, facebookProfile.Picture)

	if err != nil {
		logger.Error("Could not link Facebook ID.", zap.Error(err), zap.Any("input", token))
@@ -325,15 +326,15 @@ func LinkGoogle(ctx context.Context, logger *zap.Logger, db *sql.DB, socialClien
	}

	res, err := db.ExecContext(ctx, `
UPDATE users
SET google_id = $2, display_name = $3, avatar_url = $4, update_time = now()
UPDATE users AS u
SET google_id = $2, display_name = COALESCE(NULLIF(u.display_name, ''), $3), avatar_url = COALESCE(NULLIF(u.avatar_url, ''), $4), email = COALESCE(NULLIF(u.email, ''), $5), update_time = now()
WHERE (id = $1)
AND (NOT EXISTS
    (SELECT id
     FROM users
     WHERE google_id = $2 AND NOT id = $1))`,
		userID,
		googleProfile.Sub, displayName, avatarURL)
		googleProfile.Sub, displayName, avatarURL, googleProfile.Email)

	if err != nil {
		logger.Error("Could not link Google ID.", zap.Error(err), zap.Any("input", token))