public async Task <IActionResult> EmailLogin([FromBody] EmailUserLoginIn userIn) { var result = await _userRepo.EmailLogin(userIn.Email, userIn.Username, userIn.Password); if (result) { string key = _configuration.GetSection("SecretKey").ToString(); double expireTime = Double.Parse(_configuration.GetSection("ExpireTime").ToString()); string token = JwtHelper.CreateJwtToken(userIn.Username, userIn.Email, key, expireTime); if (token != null) { return(Ok(new { token })); } } return(BadRequest()); }
private string UserLogin(User user, bool isRememberMe) { var token = new Entities.UserLoginToken() { Id = 0, UserId = user.Id }; token.LastLoginDated = DateTimeHelper.GetDateTimeNow(); token.ExpiredDated = token.LastLoginDated.AddDays(isRememberMe ? 14 : 1); token.Token = System.Guid.NewGuid().ToString().Replace("-", ""); BaseDBRepository.UserLoginTokenRepository.Save(token); BaseDBRepository.Commit(); // Delete user's expired tokens var expiredTokens = BaseDBRepository.UserLoginTokenRepository.GetAll() .Where(t => t.UserId == user.Id && t.ExpiredDated < DateTimeHelper.GetDateTimeNow()).ToList(); BaseDBRepository.UserLoginTokenRepository.Delete(expiredTokens); BaseDBRepository.Commit(); // @TODO : Merge anonymous data to logged in user and delete current anonymous user & its tokens var tokenString = JwtHelper.CreateJwtToken(token.Token, token.ExpiredDated); if (!string.IsNullOrEmpty(tokenString)) { this.Response.Cookies.Delete("auth"); this.Response.Cookies.Append("auth", tokenString, new Microsoft.AspNetCore.Http.CookieOptions() { Path = "/", Expires = new DateTimeOffset(DateTimeHelper.GetDateTimeNow().AddYears(2)) }); } return(tokenString); }
public async Task <IActionResult> GoogleLogin([FromBody] GoogleUserIn userIn) { Console.WriteLine("-----------STARTING GOOGLE LOGIN"); await _userRepo.GoogleLogin(userIn.idToken, userIn.username, userIn.googleEmail); string key = _configuration.GetSection("SecretKey").ToString(); double expireTime = Double.Parse(_configuration.GetSection("ExpireTime").ToString()); string token = JwtHelper.CreateJwtToken(userIn.username, userIn.googleEmail, key, expireTime); Console.WriteLine("-----------ENDING GOOGLE LOGIN"); if (token != null) { Console.WriteLine("-----------GOOGLE LOGIN ENDED WITH OK"); return(Ok(new { token })); } Console.WriteLine("-----------GOOGLE LOGIN ENDED WITH BAD REQUEST"); return(BadRequest()); }