Esempio n. 1
0
        public async Task <ActionResult> LoginGoogle([FromBody] LoginDataset loginDataset)
        {
            try
            {
                FirebaseToken decodedToken = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(loginDataset.Token);

                AccountDataset account = await _service.Login(decodedToken, loginDataset.Flg);

                if (account != null)
                {
                    var claims = new[]
                    {
                        new Claim(JwtRegisteredClaimNames.Sub, account.AccountId.ToString()),
                        new Claim(JwtRegisteredClaimNames.Email, account.Email),
                        new Claim(ClaimTypes.Role, account.Role.RoleName),
                        new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                    };

                    var key   = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(AppSettings.Settings.JwtSecret));
                    var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
                    var token = new JwtSecurityToken(AppSettings.Settings.Issuer,
                                                     AppSettings.Settings.Audience,
                                                     claims,
                                                     // expires: DateTime.Now.AddSeconds(55 * 60),
                                                     signingCredentials: creds);
                    return(Ok(new
                    {
                        token = new JwtSecurityTokenHandler().WriteToken(token)
                    }));
                }
                else
                {
                    return(Forbid());
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(new { message = ex.Message }));
            }
        }
        public async Task <ActionResult> Login(LoginDataset dataset)
        {
            // if (username is not Usrname || password is not Psswrd) return Forbid();

            // var toke = _factory.CreateToken(username);

            var decodeToken = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(dataset.GGToken);

            // _authenticationService.Login(decodeToken);

            var user = await _authenticationService.CreateNewAccount(decodeToken);

            if (user is null)
            {
                return(Forbid());
            }

            var toke = _factory.CreateToken(user);

            return(Ok(new
            {
                token = new JwtSecurityTokenHandler().WriteToken(toke)
            }));
        }