Пример #1
0
        public async Task <IActionResult> Register(Register register)
        {
            var user = new BaristaBuddyUser
            {
                Email       = register.Email,
                UserName    = register.Email,
                PhoneNumber = register.PhoneNumber,
                BirthDate   = register.BirthDate,
                FirstName   = register.FirstName,
                LastName    = register.LastName
            };

            var result = await userManager.CreateAsync(user, register.Password);

            if (!result.Succeeded)
            {
                return(BadRequest(new
                {
                    message = "registration failed",
                    errors = result.Errors
                }));
            }

            return(Ok(new UserWithToken
            {
                UserId = user.Id,
                Token = userManager.CreateToken(user)
            }));
        }
Пример #2
0
        public async Task <IActionResult> UpdateUser(string userId, BaristaBuddyUser data)
        {
            var user = await userManager.FindByIdAsync(userId);

            if (user == null)
            {
                return(NotFound());
            }

            user.FirstName = data.FirstName;
            user.LastName  = data.LastName;

            await userManager.UpdateAsync(user);

            return(Ok(new
            {
                UserId = user.Id,
                user.Email,
                user.BirthDate,
                user.ImageUrl,
                user.PhoneNumber,
                user.FirstName,
                user.LastName,
            }));
        }
        public string CreateToken(BaristaBuddyUser user)
        {
            var secret      = configuration["JWT:Secret"];
            var secretBytes = Encoding.UTF8.GetBytes(secret);
            var signingKey  = new SymmetricSecurityKey(secretBytes);

            var tokenClaims = new[]
            {
                new Claim(JwtRegisteredClaimNames.Sub, user.UserName),
                new Claim("UserId", user.Id),
                new Claim("FullName", $"{user.FirstName} {user.LastName}"),
            };

            var token = new JwtSecurityToken(
                expires: DateTime.UtcNow.AddSeconds(36000),
                claims: tokenClaims,
                signingCredentials: new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256)
                );

            var tokenString = new JwtSecurityTokenHandler().WriteToken(token);

            return(tokenString);
        }
 public Task <IdentityResult> UpdateAsync(BaristaBuddyUser user)
 {
     return(userManager.UpdateAsync(user));
 }
 public Task <IdentityResult> CreateAsync(BaristaBuddyUser user, string password)
 {
     return(userManager.CreateAsync(user, password));
 }
 public Task <bool> CheckPasswordAsync(BaristaBuddyUser user, string password)
 {
     return(userManager.CheckPasswordAsync(user, password));
 }
 public Task AccessFailedAsync(BaristaBuddyUser user)
 {
     return(userManager.AccessFailedAsync(user));
 }