public async Task<ApiJsonResult> Register(RegisterJobseekerParams registerJobseekerParams)
        {
            try {
                var user = await new AccountManager().RegisterJobseeker(registerJobseekerParams);
                if (registerJobseekerParams.AccountType == SwipeJob.Model.Extra.AccountType.Facebook || registerJobseekerParams.AccountType == SwipeJob.Model.Extra.AccountType.Google) {
                    SetAuthenticatedUser(user.Id, true);
                }

                return new ApiJsonResult { Success = true };
            }
            catch (Exception ex) {
                return ProcessException(ex);
            }
        }
예제 #2
0
        public async Task<User> RegisterJobseeker(RegisterJobseekerParams registerJobseekerParams)
        {
            Utils.CheckNullOrEmpty(new List<string> { "Email", "Token", "AccountType" }, registerJobseekerParams.Email, registerJobseekerParams.Token, registerJobseekerParams.AccountType);

            if (!Utils.IsEmail(registerJobseekerParams.Email))
            {
                throw new UserException(ErrorCode.EMAIL_INVALID.ToString());
            }

            using (AppDbContext context = new AppDbContext())
            {
                if (await context.Users.AnyAsync(p => p.Email == registerJobseekerParams.Email))
                {
                    throw new UserException(ErrorCode.EMAIL_IN_USED.ToString());
                }

                byte[] imageBytes = null;
                bool activated = true;
                string confirmationCode = string.Empty;
                if (registerJobseekerParams.AccountType == AccountType.Email)
                {
                    activated = false;
                    confirmationCode = Guid.NewGuid().ToString();
                    registerJobseekerParams.Token = UtilsCryptography.GenerateBCryptHash(registerJobseekerParams.Token);

                }
                else if (registerJobseekerParams.AccountType == AccountType.Facebook)
                {
                    try
                    {
                        FacebookClient facebookClient = new FacebookClient(registerJobseekerParams.Token);
                        await facebookClient.GetTaskAsync("me?fields=id");

                        WebClient webClient = new WebClient();
                        imageBytes = webClient.DownloadData(registerJobseekerParams.AvatarPath);
                        registerJobseekerParams.Token = null;
                    }
                    catch (Exception)
                    {
                        throw new UserException(ErrorCode.FACEBOOK_INVALID.ToString());
                    }

                }
                else
                {
                    try
                    {
                        string query = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + registerJobseekerParams.Token;
                        HttpClient client = new HttpClient();
                        await client.GetStringAsync(query);

                        WebClient webClient = new WebClient();
                        imageBytes = webClient.DownloadData(registerJobseekerParams.AvatarPath);
                        registerJobseekerParams.Token = null;
                    }
                    catch (Exception)
                    {
                        throw new UserException(ErrorCode.GOOGLE_INVALID.ToString());
                    }
                }

                User user = new User
                {
                    Id = Guid.NewGuid(),
                    Email = registerJobseekerParams.Email,
                    Password = registerJobseekerParams.Token,
                    AccountType = registerJobseekerParams.AccountType,
                    UserType = UserType.JobSeeker,
                    RegisteredDateUtc = DateTime.UtcNow,
                    IsActivated = activated,
                    ConfirmationCode = confirmationCode
                };
                context.Users.Add(user);

                Language defaultLanguage = await context.Languages.FirstOrDefaultAsync(p => p.Name == "English - United States");


                if (registerJobseekerParams.AccountType != AccountType.Email)
                {
                    registerJobseekerParams.DayOfBirthUtc = new DateTime(2000, 1, 1);
                    registerJobseekerParams.NRICType = NRICType.Citizen;
                }

                JobSeeker jobSeeker = new JobSeeker
                {
                    UserId = user.Id,
                    Avartar = imageBytes,
                    FullName = registerJobseekerParams.FullName,
                    Gender = Gender.Male,
                    NRICNumber = registerJobseekerParams.NRICNumber,
                    DateOfBirth = registerJobseekerParams.DayOfBirthUtc,
                    NRICType = registerJobseekerParams.NRICType,
                    ExperienceYear = ExperienceYear.Student,
                    LanguageId = defaultLanguage.Id,
                    CanNegotiation = true,
                    CreatedDateUtc = DateTime.UtcNow,
                    UpdatedDateUtc = DateTime.UtcNow
                };
                context.JobSeekers.Add(jobSeeker);

                await context.SaveChangesAsync();

                if (user.IsActivated)
                {
                    await EmailDelivery.SendJobSeekerRegisterCompleted(registerJobseekerParams.Email);
                }
                else
                {
                    await EmailDelivery.SendJobSeekerRegisterActivation(registerJobseekerParams.Email, confirmationCode);
                    return null;
                }

                return user;
            }
        }