Example #1
0
        private async Task <IActionResult> AuthoriseUser(string authToken, AuthType authType, string firstName, string lastName)
        {
            if (authType == AuthType.Unknown)
            {
                return(BadRequest());
            }

            var metadata = await tokenUtil.GetMetadataForToken(authToken, authType).ConfigureAwait(false);

            if (metadata == null || string.IsNullOrWhiteSpace(metadata.AuthIdentifier))
            {
                return(BadRequest());
            }

            var user = await users.FetchUserForAuthIdentifier(metadata.AuthIdentifier, authType).ConfigureAwait(false);

            if (user.IsFailure)
            {
                return(StatusCode(500));
            }

            if (user.Value.HasValue)
            {
                var userId = user.Value.Value.UserId;

                if (string.IsNullOrWhiteSpace(user.Value.Value.ExternalPaymentProcessorId))
                {
                    try
                    {
                        var billingId = await billingManager.CreateCustomer(user.Value.Value).ConfigureAwait(false);

                        await users.UpdateUser(new UserPatch
                        {
                            ResourceId = userId,
                            ExternalPaymentProcessorId = new PatchOperation <string> {
                                Operation = OperationKind.Update, Value = billingId
                            }
                        }).ConfigureAwait(false);
                    }
                    catch (Exception)
                    {
                        return(StatusCode(500));
                    }
                }

                if (!string.IsNullOrWhiteSpace(firstName) || !string.IsNullOrWhiteSpace(lastName))
                {
                    await users.UpdateUser(new UserPatch
                    {
                        ResourceId = userId,
                        FirstName  = new PatchOperation <string> {
                            Operation = OperationKind.Update, Value = firstName ?? "Anonymous"
                        },
                        LastName = new PatchOperation <string> {
                            Operation = OperationKind.Update, Value = lastName
                        },
                    }).ConfigureAwait(false);
                }

                var existingSession = await userSessions.FetchUserSessionAndUserFromToken(authToken, (int)authType)
                                      .Ensure(s => s.HasValue, "Data found")
                                      .OnSuccess(s => s.Value)
                                      .ConfigureAwait(false);

                if (existingSession.IsFailure)
                {
                    var session = new UserSession
                    {
                        AuthToken       = authToken,
                        UserId          = userId,
                        TokenType       = (int)authType,
                        AuthTokenExpiry = metadata.Expiry
                    };

                    var sessionId = await userSessions.CreateUserSession(session).ConfigureAwait(false);

                    if (sessionId.IsFailure)
                    {
                        return(StatusCode(500));
                    }
                }
            }
            else
            {
                var newUser = new User {
                    IsVerified = true, WantAdvertising = false, RegistrationId = tokenUtil.GenerateToken(), FirstName = firstName ?? "Anonymous", LastName = lastName
                };
                switch (authType)
                {
                case AuthType.Apple:
                    newUser.AppleUserIdentifier = metadata.AuthIdentifier;
                    break;

                case AuthType.Google:
                    newUser.GoogleUserIdentifier = metadata.AuthIdentifier;
                    break;

                case AuthType.Facebook:
                    newUser.FacebookUserIdentifier = metadata.AuthIdentifier;
                    break;

                case AuthType.Unknown:
                    break;
                }
                var result = await users.CreateUser(newUser).ConfigureAwait(false);

                if (result.IsFailure)
                {
                    return(StatusCode(500));
                }

                var userId = result.Value.Value;

                try
                {
                    var billingId = await billingManager.CreateCustomer(newUser).ConfigureAwait(false);

                    await users.UpdateUser(new UserPatch
                    {
                        ResourceId = userId,
                        ExternalPaymentProcessorId = new PatchOperation <string> {
                            Operation = OperationKind.Update, Value = billingId
                        }
                    }).ConfigureAwait(false);
                }
                catch (Exception)
                {
                    return(StatusCode(500));
                }

                var existingSession = await userSessions.FetchUserSessionAndUserFromToken(authToken, (int)authType)
                                      .Ensure(s => s.HasValue, "Data found")
                                      .OnSuccess(s => s.Value)
                                      .ConfigureAwait(false);

                if (existingSession.IsFailure)
                {
                    var session = new UserSession
                    {
                        AuthToken       = authToken,
                        UserId          = userId,
                        TokenType       = (int)authType,
                        AuthTokenExpiry = metadata.Expiry
                    };

                    var sessionId = await userSessions.CreateUserSession(session).ConfigureAwait(false);

                    if (sessionId.IsFailure)
                    {
                        return(StatusCode(500));
                    }
                }
            }

            return(Ok());
        }