Example #1
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                var userInfo = await m_facebookAccessor.FacebookLogin(request.AccessToken);

                if (userInfo == null)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
                }

                var user = await m_userManager.FindByEmailAsync(userInfo.Email);

                var refreshToken = m_jwtGenerator.GenerateRefreshtoken();

                if (user != null)
                {
                    user.RefreshTokens.Add(refreshToken);
                    await m_userManager.UpdateAsync(user);
                }
                else
                {
                    user = await this.CreateUser(userInfo, refreshToken);
                }

                return(new User(user, m_jwtGenerator, refreshToken.Token));
            }
Example #2
0
        public async Task <Response <LoggedUser> > FacebookLogin(string accessToken)
        {
            var userInfo = await facebookAccessor.FacebookLogin(accessToken);

            if (userInfo == null)
            {
                return(Response <LoggedUser> .Failure(ResponseResult.UserIsNotAuthorized, "Nie udało się zalogować z wykorzystaniem dostawcy: Facebook"));
            }

            var user = await userManager.FindByNameAsync("fb_" + userInfo.Id);

            if (user == null)
            {
                user = new AppUser
                {
                    Id        = Guid.NewGuid().ToString(),
                    FirstName = userInfo.Name.Split(' ', 2)[0],
                    LastName  = userInfo.Name.Split(' ', 2)[1],
                    Email     = userInfo.Email,
                    UserName  = "******" + userInfo.Id,
                    PhotoUrl  = userInfo.Picture.Data.Url,
                    Tags      = new Collection <Tag>(),
                    TodoSets  = new Collection <TodoSet>(),
                };

                var result = await userManager.CreateAsync(user);

                if (!result.Succeeded)
                {
                    return(Response <LoggedUser> .Failure(ResponseResult.BadRequestStructure, "Nie udało się utworzyć konta użytkownika dla podanych informacji"));
                }

                await userManager.AddToRoleAsync(user, "RegularUser");

                await userManager.AddClaimAsync(user, new Claim(ClaimTypes.Role, "RegularUser"));
            }

            var existingRole = await roleManager.RoleExistsAsync("RegularUser");

            if (!existingRole)
            {
                return(Response <LoggedUser> .Failure(ResponseResult.BadRequestStructure, "Nie udało się utworzyć konta użytkownika dla podanych informacjiB"));
            }

            var loggedUser = new LoggedUser
            {
                Id          = user.Id,
                FirstName   = user.FirstName,
                LastName    = user.LastName,
                UserName    = user.UserName,
                PhoneNumber = user.PhoneNumber,
                Email       = user.Email,
                PhotoUrl    = user.PhotoUrl,
                Token       = webTokenGenerator.CreateToken(user, "RegularUser")
            };

            return(Response <LoggedUser> .Success(ResponseResult.DataObtained, loggedUser));
        }
Example #3
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                // handler logic
                var userInfo = await _facebookAccessor.FacebookLogin(request.AccessToken);

                // if token is unverified or unsuccessful response from facebook
                if (userInfo == null)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
                }

                // find the user by their email
                var user = await _userManager.FindByEmailAsync(userInfo.Email);

                var refreshToken = _jwtGenerator.GenerateRefreshToken();

                if (user != null)
                {
                    // they have loggen in before
                    user.RefreshTokens.Add(refreshToken);
                    await _userManager.UpdateAsync(user);

                    return(new User(user, _jwtGenerator, refreshToken.Token));
                }

                // if no user create them
                user = new AppUser
                {
                    DisplayName    = userInfo.Name,
                    Id             = userInfo.Id,
                    Email          = userInfo.Email,
                    UserName       = "******" + userInfo.Id,
                    EmailConfirmed = true // because facebook confirmed their email already
                };

                var photo = new Photo
                {
                    Id = "fb_" + userInfo.Id,
                    // this is their main img in facebook
                    Url    = userInfo.Picture.Data.Url,
                    IsMain = true
                };

                user.Photos.Add(photo);

                user.RefreshTokens.Add(refreshToken);

                var result = await _userManager.CreateAsync(user);

                if (!result.Succeeded)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
                }

                return(new User(user, _jwtGenerator, refreshToken.Token));
            }
            public async Task <User> Handle(Command request, CancellationToken cancellationToken)
            {
                var userInfo = await _facebookAccessor.FacebookLogin(request.AccessToken);

                if (userInfo == null)
                {
                    throw new RestException(HttpStatusCode.BadGateway, new { User = "******" });
                }

                var user = await _userManager.FindByEmailAsync(userInfo.Email);

                var token = "";

                if (user == null)
                {
                    user = new AppUser
                    {
                        DisplayName        = userInfo.Name,
                        Id                 = userInfo.Id,
                        Email              = userInfo.Email,
                        UserName           = "******" + userInfo.Id,
                        RefreshToken       = _jwtGenerator.GenerateRefreshToken(),
                        RefreshTokenExpiry = DateTime.UtcNow.AddDays(30)
                    };

                    //try create local user
                    token = _jwtGenerator.CreateToken(user);
                    var userCreated = await _userActivitiesApp.CreateUser(user.DisplayName, token);

                    if (!userCreated)
                    {
                        throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
                    }

                    var result = await _userManager.CreateAsync(user);

                    if (!result.Succeeded)
                    {
                        throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
                    }
                }

                if (token == "")
                {
                    token = _jwtGenerator.CreateToken(user);
                }

                return(new User
                {
                    Email = user.Email,
                    DisplayName = user.DisplayName,
                    Username = user.UserName,
                    Token = token,
                    RefreshToken = user.RefreshToken
                });
            }
Example #5
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                var userInfo = await _facebookAccessor.FacebookLogin(request.AccessToken);

                if (userInfo == null)
                {
                    throw new RestException(HttpStatusCode.Unauthorized, ErrorType.FacebookAuthorizationFailed);
                }

                var user = await _userManager.FindByEmailAsync(userInfo.Email);

                if (user != null && !user.UserName.Contains("_fb"))
                {
                    throw new RestException(HttpStatusCode.BadRequest, ErrorType.DuplicateEmailFound);
                }

                if (user == null)
                {
                    user = new AppUser
                    {
                        DisplayName = userInfo.Name,
                        Id          = userInfo.Id,
                        Email       = userInfo.Email,
                        UserName    = userInfo.Email.Split("@")[0] + "_fb",

                        Avatar = userInfo.Picture.Data.Url,

                        RefreshToken       = _jwtGenerator.GenerateRefreshToken(),
                        RefreshTokenExpiry = DateTime.Now.AddDays(30),
                    };

                    var result = await _userManager.CreateAsync(user);

                    if (!result.Succeeded)
                    {
                        throw new RestException(HttpStatusCode.InternalServerError, ErrorType.SavingChangesError);
                    }
                }

                return(new User
                {
                    DisplayName = user.DisplayName,
                    Email = user.Email,
                    Username = user.UserName,

                    Avatar = user.Avatar,

                    Token = _jwtGenerator.CreateToken(user),
                    RefreshToken = user.RefreshToken,

                    TourCompleted = user.TourCompleted,
                    ItemsTourCompleted = user.ItemsTourCompleted,
                    NewItemTourCompleted = user.NewItemTourCompleted,
                    LearningTourCompleted = user.LearningTourCompleted
                });
            }
            public async Task <AppUserDto> Handle(Query request, CancellationToken cancellationToken)
            {
                var userInfo = await _facebookAccessor.FacebookLogin(request.AccessToken);

                if (userInfo == null)
                {
                    throw new RestException(HttpStatusCode.BadRequest,
                                            new { User = "******" });
                }

                var user = await _userManager.FindByEmailAsync(userInfo.Email);

                var refreshToken = _jwtGenerator.GenerateRefreshToken();

                if (user != null)
                {
                    user.RefreshTokens.Add(refreshToken);
                    await _userManager.UpdateAsync(user);

                    return(new AppUserDto(user, _jwtGenerator, refreshToken.Token));
                }

                // If no user exists - generate the new AppUser
                user = new AppUser()
                {
                    DisplayName    = userInfo.Name,
                    Id             = userInfo.Id,
                    Email          = userInfo.Email,
                    UserName       = "******" + userInfo.Id,
                    EmailConfirmed = true
                };

                if (userInfo.ImageData != null)
                {
                    var photo = new Photo
                    {
                        Id       = "fb_" + userInfo.Id,
                        Url      = userInfo.ImageData?.Image?.Url,
                        IsAvatar = true
                    };

                    user.Photos.Add(photo);
                }

                user.RefreshTokens.Add(refreshToken);

                var result = await _userManager.CreateAsync(user);

                if (!result.Succeeded)
                {
                    throw new RestException(HttpStatusCode.BadRequest);
                }


                return(new AppUserDto(user, _jwtGenerator, refreshToken.Token));
            }
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                var userInfo = await _facebookAccessor.FacebookLogin(request.AccessToken);


                if (userInfo == null)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
                }

                var user = await _userManager.FindByEmailAsync(userInfo.Email);

                var refreshToken = _jwtGenerator.GenerateRefreshToken();

                if (user != null)
                {
                    user.RefreshTokens.Add(refreshToken);
                    await _userManager.UpdateAsync(user);

                    return(new User(user, _jwtGenerator, refreshToken.Token));
                }


                user = new AppUser
                {
                    DisplayName    = userInfo.Id,
                    Id             = userInfo.Id,
                    Email          = userInfo.Email,
                    UserName       = "******" + userInfo.Id,
                    EmailConfirmed = true
                };

                var photo = new Photo
                {
                    Id     = "fb_" + userInfo.Id,
                    Url    = userInfo.Picture.Data.Url,
                    IsMain = true
                };

                //To initialized photo collection
                user.Photos.Add(photo);
                user.RefreshTokens.Add(refreshToken);

                var result = await _userManager.CreateAsync(user);

                if (!result.Succeeded)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
                }

                // return new User(user, _jwtGenerator, refreshToken.Token);

                return(new User(user, _jwtGenerator, refreshToken.Token));
            }
Example #8
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                var userInfo = await _facebookAccessor.FacebookLogin(request.AccessToken);

                if (userInfo == null)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
                }

                var user = await _userManager.FindByEmailAsync(userInfo.Email);

                var refreshToken = _jwtGenerator.GenerateRefreshToken();

                user.RefreshTokens.Add(refreshToken);

                //Check user was logged in previously hence issue Refresh Token
                if (user != null)
                {
                    user.RefreshTokens.Add(refreshToken);
                    //Update via _userManager since we are storing tokens in the db; it's saved automatically
                    await _userManager.UpdateAsync(user);

                    return(new User(user, _jwtGenerator, refreshToken.Token));
                }

                //If user not logged in
                user = new AppUser
                {
                    DisplayName = userInfo.Name,
                    Id          = userInfo.Id,
                    Email       = userInfo.Email,
                    UserName    = "******" + userInfo.Id //To differentiate from normal username
                };

                var photo = new Photo
                {
                    Id     = "fb_" + userInfo.Id,
                    Url    = userInfo.Picture.Data.Url,
                    IsMain = true
                };

                user.RefreshTokens.Add(refreshToken);

                user.Photos.Add(photo);                            //Had to modify AppUser.cs n create a constructor with Photos collection
                var result = await _userManager.CreateAsync(user); //Password stored in FB

                if (!result.Succeeded)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
                }

                return(new User(user, _jwtGenerator, refreshToken.Token));
            }
            public async Task <User> Handle(Query request,
                                            CancellationToken cancellationToken)
            {
                var userInfo = await _facebookAccessor.FacebookLogin(request.AccessToken);

                // Could be null if the token isn't verified, or if the response is unsuccessful when we get the UserInfo
                if (userInfo == null)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
                }

                var user = await _userManager.FindByEmailAsync(userInfo.Email);

                var refreshToken = _jwtGenerator.GenerateRefreshToken();

                if (user != null)
                {
                    user.RefreshTokens.Add(refreshToken);
                    await _userManager.UpdateAsync(user);

                    return(new User(user, _jwtGenerator, refreshToken.Token));
                }

                user = new AppUser
                {
                    DisplayName    = userInfo.Name,
                    Id             = userInfo.Id,
                    Email          = userInfo.Email,
                    UserName       = "******" + userInfo.Id,
                    EmailConfirmed = true
                };

                var photo = new Photo
                {
                    Id     = "fb_" + userInfo.Id, // helps us recognize it's not a cloudinary photo
                    Url    = userInfo.Picture.Data.Url,
                    IsMain = true
                };

                user.Photos.Add(photo);
                user.RefreshTokens.Add(refreshToken);

                var result = await _userManager.CreateAsync(user);

                if (!result.Succeeded)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
                }


                return(new User(user, _jwtGenerator, refreshToken.Token));
            }
Example #10
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                // get user info and verify token is valid
                var userInfo = await _facebookAccessor.FacebookLogin(request.AccessToken);

                // if user info is null then token could not be validated
                if (userInfo == null)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
                }

                var user = await _userManager.FindByEmailAsync(userInfo.Email);

                if (user == null)
                {
                    user = new AppUser
                    {
                        DisplayName        = userInfo.FirstName,
                        Id                 = userInfo.Id,
                        Email              = userInfo.Email,
                        UserName           = "******" + userInfo.Id,
                        RefreshToken       = _jwtGenerator.GenerateRefreshToken(),
                        RefreshTokenExpiry = DateTime.Now.AddDays(30)
                    };

                    var photo = new Photo
                    {
                        Id     = Guid.NewGuid().ToString(),
                        Url    = userInfo.Picture.Data.Url,
                        IsMain = true
                    };

                    user.Photos.Add(photo);

                    var result = await _userManager.CreateAsync(user);

                    if (!result.Succeeded)
                    {
                        throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
                    }
                }

                return(new User
                {
                    DisplayName = user.DisplayName,
                    Token = _jwtGenerator.CreateToken(user),
                    RefreshToken = user.RefreshToken,
                    Username = user.UserName,
                    Image = user.Photos.FirstOrDefault(x => x.IsMain)?.Url
                });
            }
Example #11
0
            public async Task <User> Handle(Query request,
                                            CancellationToken cancellationToken)
            {
                var userInfo = await _facebookAccessor
                               .FacebookLogin(request.AccessToken);

                if (userInfo == null)
                {
                    throw new RestException(HttpStatusCode.BadRequest,
                                            new { User = "******" });
                }

                var user = await _userManager.FindByEmailAsync(
                    userInfo.Email);

                if (user == null)
                {
                    user = new AppUser
                    {
                        DisplayName = userInfo.Name,
                        Id          = userInfo.Id,
                        Email       = userInfo.Email,
                        UserName    = "******" + userInfo.Id
                    };

                    var photo = new Photo
                    {
                        Id     = "fb_" + userInfo.Id,
                        Url    = userInfo.Picture.Data.Url,
                        IsMain = true
                    };

                    user.Photos.Add(photo);

                    var result = await _userManager.CreateAsync(user);

                    if (!result.Succeeded)
                    {
                        throw new RestException(HttpStatusCode.BadRequest,
                                                new { User = "******" });
                    }
                }

                return(new User
                {
                    DisplayName = user.DisplayName,
                    Token = _jwtGenerator.CreateToken(user),
                    Username = user.UserName,
                    Image = user.Photos.FirstOrDefault(x => x.IsMain)?.Url
                });
            }
Example #12
0
            public async Task <User> Handle(ExternalLoginQuery request, CancellationToken cancellationToken)
            {
                var userInfo = await _facebookAccessor.FacebookLogin(request.AccessToken);

                if (userInfo == null)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
                }
                var user = await _userManager.FindByEmailAsync(userInfo.Email);

                if (user != null && !user.EmailConfirmed)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Login = "******" });
                }
                if (user == null)
                {
                    user = new AppUser
                    {
                        DisplayName        = userInfo.Name,
                        Id                 = userInfo.Id,
                        Email              = userInfo.Email,
                        UserName           = "******" + userInfo.Id,
                        RefreshToken       = _jwtGenerator.GenerateRefreshToken(),
                        RefreshTokenExpiry = DateTime.Now.AddDays(30)
                    };
                    var photo = new Photo
                    {
                        Id     = "fb_" + userInfo.Id,
                        Url    = userInfo.Picture.Data.Url,
                        IsMain = true
                    };
                    user.Photos.Add(photo);

                    var result = await _userManager.CreateAsync(user);

                    if (!result.Succeeded)
                    {
                        throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
                    }
                }
                return(new User
                {
                    DisplayName = user.DisplayName,
                    Token = _jwtGenerator.CreateToken(user),
                    RefreshToken = user.RefreshToken,
                    Id = user.Id,
                    Image = user.Photos.FirstOrDefault(x => x.IsMain)?.Url
                });
            }
Example #13
0
            public async Task <UserDTO> Handle(Query request, CancellationToken cancellationToken)
            {
                var fbUser = await _fbAccessor.FacebookLogin(request.AccessToken);

                if (fbUser == null)
                {
                    throw new Application.Errors.RestException(HttpStatusCode.BadRequest, new { User = "******" });
                }

                var user = await _userManager.FindByEmailAsync(fbUser.Email);

                if (user == null)
                {
                    user = new AppUser
                    {
                        DisplayName        = fbUser.Name,
                        Id                 = fbUser.Id,
                        UserName           = "******" + fbUser.Id,
                        Email              = fbUser.Email,
                        RefreshToken       = _jwtGenerator.CreateRefreshToken(),
                        RefreshTokenExpiry = DateTime.Now.AddDays(30)
                    };

                    var photo = new Photo
                    {
                        Id     = "fb_" + fbUser.Id,
                        Url    = fbUser.Picture.Data.Url,
                        IsMain = true
                    };

                    user.Photos.Add(photo);

                    var result = await _userManager.CreateAsync(user);

                    if (!result.Succeeded)
                    {
                        throw new Application.Errors.RestException(HttpStatusCode.BadRequest, new { User = "******" });
                    }
                }

                return(new UserDTO
                {
                    DisplayName = user.DisplayName,
                    Token = _jwtGenerator.CreateToken(user),
                    RefreshToken = user.RefreshToken,
                    UserName = user.UserName,
                    Image = user.Photos.FirstOrDefault(x => x.IsMain)?.Url
                });
            }