Ejemplo n.º 1
0
        public UserModel LoginGoogle(UserLoginGoogle userLoginGoogle)
        {
            using var unitOfWork = new UnitOfWork();
            var userRepo = unitOfWork.GetRepository <User>();
            var user     = userRepo.FirstOrDefault(x => x.GoogleId == userLoginGoogle.GoogleId);

            if (user == null)
            {
                user = new User
                {
                    Email        = userLoginGoogle.Email,
                    GoogleId     = userLoginGoogle.GoogleId,
                    FacebookId   = null,
                    UserType     = UserType.Normal,
                    Username     = userLoginGoogle.Email,
                    RefreshToken = GenerateRefreshToken(),
                    Password     = StringCipher.Encrypt("", "KI6rnfCy6YUFq0mLoO")
                };
                userRepo.Add(user);
                var playlistRepo = unitOfWork.GetRepository <Playlist>();
                playlistRepo.Add(new Playlist
                {
                    Name      = "Liked songs",
                    Public    = false,
                    ShareLink = "",
                    UserId    = user.Id
                });

                unitOfWork.Save();
            }
            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(_appSettings.Secret);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new[]
                {
                    new Claim(ClaimTypes.Name, user.Username)
                }),
                Expires            = DateTime.UtcNow.AddMonths(1),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token     = tokenHandler.CreateToken(tokenDescriptor);
            var userModel = UserMapper.MapUser(user);

            userModel.Token = tokenHandler.WriteToken(token);
            return(userModel);
        }
Ejemplo n.º 2
0
        public IActionResult LoginInGoogle([FromBody] UserLoginGoogle userLoginGoogle)
        {
            var loginResult = _userService.LoginGoogle(userLoginGoogle);

            return(loginResult == null?Unauthorized() as IActionResult : new OkObjectResult(loginResult));
        }