Beispiel #1
0
        public async Task <IActionResult> PostLogin(Login login)
        {
            try
            {
                var CheckCustomeEmil = _context.Customer.FirstOrDefault(m => m.Email.ToLower() == login.Email);
                var CheckCustomerPW  = _context.Customer.FirstOrDefault(m => m.Password.ToLower() == login.Password);


                var CheckPharmacyEmail = _context.Pharmacy.FirstOrDefault(m => m.Email.ToLower() == login.Email);
                var CheckPharmacyPW    = _context.Pharmacy.FirstOrDefault(m => m.Password.ToLower() == login.Password);


                if (((CheckCustomeEmil == null) || (CheckCustomerPW == null)) && ((CheckPharmacyEmail == null) || (CheckPharmacyPW == null)))
                {
                    return(BadRequest());
                }

                else
                {
                    var tokenString = _jwtService.GenerateJWTtoken(login);
                    return(Ok(new
                    {
                        token = tokenString
                    }));
                }
            }


            catch (Exception ex)
            { throw ex; }
        }
        public async Task <IActionResult> CustomerPost([FromBody] CustomerModel newcustomer)
        {
            // CustomerModelDB.Add(newcustomer);
            var customerWithSameEmail = _db.CustomerModels.FirstOrDefault(m => m.Email.ToLower() == newcustomer.Email.ToLower()); //check email already exit or not


            if (customerWithSameEmail == null)
            {
                //newcustomer.CustomerImage = await _iimageService.SaveImage(newcustomer.ImageData);


                var encryPassword       = _encryptService.Encryptword(newcustomer.Password);
                var encryReTypePassword = _encryptService.Encryptword(newcustomer.ReTypePassword);


                newcustomer.Password       = encryPassword;
                newcustomer.ReTypePassword = encryReTypePassword;
                newcustomer.UserRole       = "Customer";
                _db.CustomerModels.Add(newcustomer);
                _db.SaveChanges();

                LoginModel user = new LoginModel();
                user.Email    = newcustomer.Email;
                user.Password = newcustomer.Password;

                var tokenString = _jwtService.GenerateJWTtoken(user);

                string url = $"{_config["AppUrl"]}/api/auth/confirmemail?useremail={newcustomer.Email}&token={tokenString}";

                //await _mailService.SendEmailAsync(newcustomer.Email, "Confirm your email", $"<h1>Thank You for registering in S&D com</h1>" +
                //    $"<p>Please confirm your email by <a href='{url}'>Clicking here</a></p>");

                return(Ok(new
                {
                    token = tokenString,
                    message = "Success"
                }));
            }
            else
            {
                return(BadRequest());
            }
        }
        public IActionResult Login([FromBody] User login)
        {
            if (login == null)
            {
                return(BadRequest(new { error = "Validate Input" }));
            }

            User user = AuthenticateUser(login);

            if (user != null)
            {
                var tokenString = _jwtService.GenerateJWTtoken(user);

                return(Ok(new
                {
                    token = tokenString
                }));
            }

            return(BadRequest("User not found"));
        }
        public async Task <IActionResult> Login(Login login)
        {
            try
            {                                                                                                             // CustomerModelDB.Add(newcustomer);
                var CheckEmailBuyer    = _context.Buyers.FirstOrDefault(m => m.Email.ToLower() == login.Email.ToLower()); //check email already exit or not
                var CheckPasswordBuyer = _context.Buyers.FirstOrDefault(m => m.Password == login.Password);

                var CheckEmailSeller    = _context.Sellers.FirstOrDefault(m => m.Email.ToLower() == login.Email.ToLower()); //check email already exit or not
                var CheckPasswordSeller = _context.Sellers.FirstOrDefault(m => m.Password == login.Password);

                var CheckEmailAdmin    = _context.Admins.FirstOrDefault(m => m.Email.ToLower() == login.Email.ToLower()); //check email already exit or not
                var CheckPasswordAdmin = _context.Admins.FirstOrDefault(m => m.Password == login.Password);



                if ((CheckEmailBuyer == null || CheckPasswordBuyer == null) && (CheckEmailSeller == null || CheckPasswordSeller == null) && (CheckEmailAdmin == null || CheckPasswordAdmin == null))
                {
                    return(BadRequest()); //New page
                }



                else
                {
                    await _mailService.SendEmailAsync(login.Email, "New login", "<h1>Hey!, Did you login to your account</h1><p>New login to your account at " + DateTime.Now + "</p>");

                    // Return token
                    var tokenString = _jwtService.GenerateJWTtoken(login);
                    return(Ok(new
                    {
                        token = tokenString
                    }));
                }
            }



            catch (Exception ex)
            {
                throw ex;
            }
        }