Пример #1
0
            public async Task <User> Handle(Register.Command request, CancellationToken cancellationToken)
            {
                if (await _context.Users.Where(x => x.Email == request.Email).AnyAsync())
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Email = "Email already exists" });
                }

                if (await _context.Users.Where(x => x.UserName == request.UserName).AnyAsync())
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { UserName = "******" });
                }

                var user = new AppUser
                {
                    DisplayName = request.DisplayName,
                    Email       = request.Email,
                    UserName    = request.UserName
                };

                var result = await _userManager.CreateAsync(user, request.Password);

                if (result.Succeeded)
                {
                    return(new User
                    {
                        UserName = user.UserName,
                        Token = _jwtGenerator.GenerateToken(user),
                        DisplayName = user.DisplayName,
                        Image = user.Photos.FirstOrDefault(x => x.IsMain)?.ImageUrl
                    });
                }

                throw new Exception("Problem Creating User");
            }
Пример #2
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                var user = await _userManager.FindByNameAsync(_userAccessor.GetCurrentUserName());

                return(new User
                {
                    DisplayName = user.DisplayName,
                    UserName = user.UserName,
                    Token = _jwtGenerator.GenerateToken(user),
                    Image = user.Photos.FirstOrDefault(x => x.IsMain)?.ImageUrl
                });
            }
Пример #3
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                var user = await _userManager.FindByEmailAsync(request.Email);

                if (user == null)
                {
                    throw new RestException(HttpStatusCode.Unauthorized);
                }

                var result = await _signInManager.CheckPasswordSignInAsync(user, request.Password, false);

                if (result.Succeeded)
                {
                    return(new User
                    {
                        UserName = user.UserName,
                        Token = _jwtGenerator.GenerateToken(user),
                        DisplayName = user.DisplayName,
                        Image = user.Photos.FirstOrDefault(x => x.IsMain)?.ImageUrl
                    });
                }

                throw new RestException(HttpStatusCode.Unauthorized);
            }