Example #1
0
        public async Task <LoginWithOTPResponseModel> AuthenticateUserWithOTP(LoginWithOTPFormModel loginFormDataViewModel, Func <Contracts.Models.User, Task <bool> > accessValidation)
        {
            if (string.IsNullOrEmpty(loginFormDataViewModel.PhoneCode) ||
                string.IsNullOrEmpty(loginFormDataViewModel.PhoneNumber))
            {
                return(new LoginWithOTPResponseModel {
                    Success = false
                });
            }
            var queryResult = await _querySender.Send(new LoginWithOTPQuery
            {
                PhoneCode      = loginFormDataViewModel.PhoneCode,
                PhoneNumber    = loginFormDataViewModel.PhoneNumber,
                ChannelId      = loginFormDataViewModel.ChannelId,
                SignUpMethodId = loginFormDataViewModel.SignUpMethodId
            });

            var user = queryResult.User;

            if (!queryResult.Success || user == null)
            {
                return(new LoginWithOTPResponseModel
                {
                    Success = false,
                    IsAdditionalFieldsReqd = queryResult.IsAdditionalFieldsReqd
                });
            }

            var accessGranted = await accessValidation(queryResult.User);

            if (accessGranted)
            {
                var claims = new List <Claim>
                {
                    new Claim(ClaimTypes.NameIdentifier, user.AltId.ToString()),
                    new Claim(ClaimTypes.Name, user.Email),
                    new Claim("Roles", user.RolesId.ToString())
                };

                var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claims, "login"));
                await _httpContextAccessor.HttpContext.SignInAsync(claimsPrincipal);

                // Set so we can access SessionProvider in a normal way during the rest of the request
                _httpContextAccessor.HttpContext.User = claimsPrincipal;
            }
            return(new LoginWithOTPResponseModel
            {
                Success = true,
                User = queryResult.User
            });
        }
Example #2
0
        public async Task <LoginWithOTPResponseModel> AuthenticateUser(string phoneCode, string phoneNumber)
        {
            var UserData = new LoginWithOTPFormModel
            {
                PhoneCode      = phoneCode,
                PhoneNumber    = phoneNumber,
                SignUpMethodId = Contracts.Enums.SignUpMethods.OTP,
                ChannelId      = Contracts.Enums.Channels.Feel
            };

            var authenticatedResponse = await _authenticationHelper.AuthenticateUserWithOTP(UserData, u =>
            {
                return(Task.FromResult(true));
            });

            return(authenticatedResponse);
        }
Example #3
0
        public async Task <LoginWithOTPResponseModel> LoginWithOTP([FromBody] LoginWithOTPFormModel model)
        {
            if (ModelState.IsValid)
            {
                if (string.IsNullOrEmpty(model.Email))
                {
                    var userAuthenticate = AuthenticateUser(model.PhoneCode.Split("~")[0], model.PhoneNumber).Result;
                    return(new LoginWithOTPResponseModel
                    {
                        Success = userAuthenticate.Success,
                        Session = await _sessionProvider.Get(),
                        IsAdditionalFieldsReqd = userAuthenticate.IsAdditionalFieldsReqd
                    });
                }
                else
                {
                    //new user or new phoneNumber
                    var    passwordHasher = new PasswordHasher <string>();
                    string password       = new Random().NextDouble().ToString();
                    string PasswordHash   = passwordHasher.HashPassword(model.Email, password);
                    RegisterUserWithOTPCommandCommandResult registerUserResult = await _commandSender.Send <RegisterUserWithOTPCommand, RegisterUserWithOTPCommandCommandResult>(new RegisterUserWithOTPCommand
                    {
                        Email          = model.Email,
                        PasswordHash   = PasswordHash,
                        UserName       = model.Email,
                        FirstName      = model.FirstName,
                        LastName       = model.LastName,
                        PhoneCode      = model.PhoneCode,
                        PhoneNumber    = model.PhoneNumber,
                        ChannelId      = Contracts.Enums.Channels.Feel,
                        SignUpMethodId = Contracts.Enums.SignUpMethods.OTP,
                        ReferralId     = model.ReferralId,
                        Ip             = _clientIpProvider.Get(),
                    });

                    if (registerUserResult.Success)
                    {
                        var userAuthenticate = AuthenticateUser(model.PhoneCode.Split("~")[0], model.PhoneNumber).Result;
                        return(new LoginWithOTPResponseModel
                        {
                            Success = userAuthenticate.Success,
                            Session = await _sessionProvider.Get()
                        });
                    }
                    else if (Convert.ToBoolean(registerUserResult.EmailAlreadyRegistered))
                    {
                        return(new LoginWithOTPResponseModel
                        {
                            Success = false,
                            EmailAlreadyRegistered = true
                        });
                    }
                    else
                    {
                        return(new LoginWithOTPResponseModel
                        {
                            Success = false,
                        });
                    }
                }
            }
            else
            {
                return(new LoginWithOTPResponseModel {
                    Success = false
                });
            }
        }