コード例 #1
0
        public async Task <IActionResult> RegisterAsync([FromBody] RegisterModel registerModel)
        {
            var account = AccountAdapter.RegisterModelToModel(registerModel);

            if (account == null || !ModelState.IsValid)
            {
                throw new CustomException("Errors.INVALID_REGISTRATION_DATA", "Errors.INVALID_REGISTRATION_DATA_MSG");
            }

            try
            {
                if (registerModel.PhoneNumber != null)
                {
                    var formatedPhoneNumber = PhoneNumbers.PhoneNumberHelpers.GetFormatedPhoneNumber(registerModel.PhoneNumber);
                    account.PhoneNumber = formatedPhoneNumber;
                }
                //standardize phone number
            }
            catch (NumberParseException)
            {
                throw new CustomException("Errors.INVALID_PHONE_NUMBER", "Errors.INVALID_PHONE_NUMBER_MSG");
            }

            account.AccountPermissions = new List <Models.AccountPermission>()
            {
                new Models.AccountPermission
                {
                    PermissionId = "MEMBER"
                }
            };
            string password = account.Password;

            account = await _accountService.CreateAsync(account, account.Password);

            //publish a jobseeker account is created message to rabbit mq bus
            await _rawRabbitBus.PublishAsync(new AccountCreatedForEmail { Id = account.AccountId, Password = password, Birthday = registerModel.Birthday, Position = registerModel.Position, PhoneNumber = account.PhoneNumber, FirstName = registerModel.FirstName, LastName = registerModel.LastName, Email = registerModel.Email, LoginUrl = CommonContants.LoginUrl });

            //string smsContent = $"Verification code at JobHop: {account.VerificationCodes.First().VerifyCode}";

            ////send SMS using eSMS.vn
            //var response = await _esmsService.SendSMS(account.PhoneNumber, smsContent, 4);

            var viewModel = AccountAdapter.ToViewModel(account);


            return(new JsonResult(viewModel));
        }
コード例 #2
0
        private async Task GenerateToken(HttpContext context)
        {
            var username = context.Request.Form["username"].ToString();
            var password = context.Request.Form["password"].ToString();

            var _accountService = (IAccountService)context.RequestServices.GetService(typeof(IAccountService));
            var _verifyService  = (IVerificationService)context.RequestServices.GetService(typeof(IVerificationService));

            //var _rawRabbitClient = (IBusClient)context.RequestServices.GetService(typeof(IBusClient));

            //if username is not an email
            if (username != null && !username.Contains("@"))
            {
                //the username user provide is not an email
                context.Response.ContentType = "application/json";
                context.Response.StatusCode  = 400;
                await context.Response.WriteAsync(JsonConvert.SerializeObject(new
                {
                    Code    = "Errors.INCORRECT_LOGIN",
                    Custom  = "Errors.INVALID_EMAIL_ADDRESS",
                    Message = "Errors.INCORRECT_LOGIN_MSG"
                }, Formatting.Indented));

                return;
            }

            var identity = await _accountService.CheckAsync(username, password);

            //response if account null or inactive
            if (identity == null || identity.Status == UserStatus.InActive || (!username.Contains("@")))
            {
                context.Response.ContentType = "application/json";
                context.Response.StatusCode  = 400;
                var code    = "INCORRECT_LOGIN";
                var message = "INCORRECT_LOGIN_MSG";
                if (identity != null && identity.Status == UserStatus.InActive)
                {
                    code    = "ACCOUNT_INACTIVE";
                    message = "ACCOUNT_INACTIVE_MSG";
                }

                await context.Response.WriteAsync(JsonConvert.SerializeObject(new
                {
                    Code    = code,
                    Message = message
                }, Formatting.Indented));

                return;
            }

            //if (identity.AccountType == AccountType.Jobseeker && !identity.PhoneNumberVerified)
            //{
            //    context.Response.ContentType = "application/json";
            //    context.Response.StatusCode = 400;

            //    //1 account has only 1 verification => get first
            //    var verification = (await _verifyService.GetVerificationsOfAccount(identity.Id)).FirstOrDefault();

            //    //account is locked because exceeded limit of retried or resend times
            //    if (verification.Retry >= VerificationService.MAX_RETRY || verification.Resend > VerificationService.MAX_RESEND)
            //    {
            //        await context.Response.WriteAsync(JsonConvert.SerializeObject(new
            //        {
            //            Code = Errors.VERIFICATION_LOCKED,
            //            Message = Errors.VERIFICATION_LOCKED_MSG
            //        }, Formatting.Indented));
            //    }
            //    else //wait for verification
            //    {
            //        await context.Response.WriteAsync(JsonConvert.SerializeObject(new
            //        {
            //            Code = Errors.WAIT_FOR_VERIFICATION,
            //            Message = Errors.WAIT_FOR_VERIFICATION_MSG
            //        }, Formatting.Indented));
            //    }
            //    return;
            //}

            //add banana reward for first login in day
            //if (identity.AccountType == AccountType.Jobseeker)
            //{
            //    var tracker = await _accountService.AddTracker(new LoginTracker { Account = identity, LoginAt = DateTime.Now });
            //    if (tracker != null)
            //    {
            //        await _rawRabbitClient.PublishAsync(new AccountLoggedIn { AccountId = identity.Id, LoginAt = tracker.LoginAt });
            //    }
            //}

            var permissions = await _accountService.GetPermissionsOfAccountAsync(identity.AccountId);

            var now = DateTime.Now;

            var encodedJwt = TokenProviderMiddleware.GenerateAccessToken(_options, now, identity.UserName, identity.AccountId.ToString(), permissions.ToArray());

            var response = new SignInResponseModel
            {
                AccessToken = encodedJwt,
                Expires     = now.AddSeconds((int)_options.Expiration.TotalSeconds),
                Account     = AccountAdapter.ToViewModel(identity)
            };

            // Serialize and return the response
            context.Response.ContentType = "application/json";
            await context.Response.WriteAsync(JsonConvert.SerializeObject(response, new JsonSerializerSettings
            {
                Formatting = Formatting.Indented
            }));
        }