コード例 #1
0
 public Task <SignUpFlowResult> SignUpFlow(SignUpFlowInput input)
 {
     return(this.authService.SignUp(this.HttpContext, input));
 }
コード例 #2
0
ファイル: AuthService.cs プロジェクト: davitp/shoc
        /// <summary>
        /// The sign-up operation based on input
        /// </summary>
        /// <param name="httpContext">The HTTP Context</param>
        /// <param name="input">The input for sign-up</param>
        /// <returns></returns>
        public async Task <SignUpFlowResult> SignUp(HttpContext httpContext, SignUpFlowInput input)
        {
            // check if can sign-up
            if (!this.signOnSettings.SignUpEnabled)
            {
                throw ErrorDefinition.Validation(IdentityErrors.SIGNUP_DISABLED).AsException();
            }

            // gets the authorization context by the return URL (authorize callback URL)
            var context = await this.identityInteraction.GetAuthorizationContextAsync(input.ReturnUrl);

            // the return url
            var returnUrl = input.ReturnUrl.IsBlank() || context == null ? "/" : input.ReturnUrl;

            // validate email and password
            var validate = this.userService.ValidateEmailAndPassword(input.Email, input.Password);

            // failed validation
            if (validate.Count > 0)
            {
                throw new ShocException(validate);
            }

            // build a full name
            var fullname = input.FullName.IsBlank() ? "New User" : input.FullName;

            // try get root user if exists
            var root = await this.userService.GetRootUser();

            // use guest role as default, if no root yet create as root
            var role = root == null ? Roles.ROOT : Roles.USER;

            // the email is not verified by default, in case if creating root email is already verified
            var emailVerified = root == null;

            // try create user based on given input
            var user = await this.userService.Create(new CreateUserModel
            {
                Email         = input.Email,
                EmailVerified = emailVerified,
                FullName      = fullname,
                Role          = role,
                Password      = input.Password
            });

            // user is missing
            if (user == null)
            {
                throw ErrorDefinition.Validation(IdentityErrors.UNKNOWN_ERROR).AsException();
            }

            // confirmation message sent
            var confirmationSent = false;

            // need to send email verification code message
            if (!user.EmailVerified)
            {
                try
                {
                    var requestResult = await this.RequestConfirmation(httpContext, new ConfirmationRequest
                    {
                        Email     = user.Email,
                        ReturnUrl = returnUrl
                    });

                    confirmationSent = requestResult.Sent;
                }
                catch (Exception)
                {
                    confirmationSent = false;
                }
            }

            // sign in in case of verified email
            if (emailVerified)
            {
                // do sign-in registration was successful
                await this.SignInImpl(httpContext, new SignInPrincipal
                {
                    Subject     = user.Id,
                    Email       = user.Email,
                    DisplayName = user.FullName,
                    Provider    = IdentityProviders.LOCAL
                });
            }

            // build sign-up flow result
            return(new SignUpFlowResult
            {
                Subject = user.Id,
                Email = user.Email,
                EmailVerified = emailVerified,
                ConfirmationSent = confirmationSent,
                ContinueFlow = context != null,
                ReturnUrl = returnUrl
            });
        }