forked from AcaMate/AcaMate_API
[✨] 회원 탈퇴 API 생성
This commit is contained in:
parent
19fb34bc32
commit
6963c5eadb
|
@ -31,7 +31,8 @@ public class UserController : ControllerBase
|
|||
private readonly JwtTokenService _jwtTokenService;
|
||||
private readonly IRepositoryService _repositoryService;
|
||||
|
||||
public UserController(AppDbContext dbContext, ILogger<UserController> logger, JwtTokenService jwtTokenService, IRepositoryService repositoryService)
|
||||
public UserController(AppDbContext dbContext, ILogger<UserController> logger, JwtTokenService jwtTokenService,
|
||||
IRepositoryService repositoryService)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_logger = logger;
|
||||
|
@ -43,58 +44,61 @@ public class UserController : ControllerBase
|
|||
[CustomOperation("회원 정보 조회", "회원 정보 조회 (자기자신)", "사용자")]
|
||||
public async Task<IActionResult> GetUserData(string token, string refresh)
|
||||
{
|
||||
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(refresh)) return BadRequest(APIResponse.InvalidInputError());
|
||||
if(!ModelState.IsValid) return BadRequest(APIResponse.InvalidInputError());
|
||||
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(refresh))
|
||||
return BadRequest(APIResponse.InvalidInputError());
|
||||
if (!ModelState.IsValid) return BadRequest(APIResponse.InvalidInputError());
|
||||
string summary = String.Empty;
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
var validateToken = await _repositoryService.ValidateToken(token, refresh);
|
||||
summary = _repositoryService.ReadSummary(typeof(PushController), "GetUserData");
|
||||
|
||||
summary = _repositoryService.ReadSummary(typeof(UserController), "GetUserData");
|
||||
|
||||
var user = await _dbContext.User
|
||||
.Where(u => u.uid == validateToken.uid)
|
||||
.Select(u => new User
|
||||
{
|
||||
uid = u.uid,
|
||||
name = u.name,
|
||||
auto_login_yn = u.auto_login_yn,
|
||||
birth = u.birth,
|
||||
device_id = u.device_id,
|
||||
login_date = u.login_date,
|
||||
type = u.type
|
||||
})
|
||||
.FirstOrDefaultAsync();
|
||||
.Where(u => u.uid == validateToken.uid)
|
||||
.Select(u => new User
|
||||
{
|
||||
uid = u.uid,
|
||||
name = u.name,
|
||||
auto_login_yn = u.auto_login_yn,
|
||||
birth = u.birth,
|
||||
device_id = u.device_id,
|
||||
login_date = u.login_date,
|
||||
type = u.type
|
||||
})
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
return Ok(APIResponse.Send("000", $"[{summary}], 정상", user));
|
||||
}
|
||||
catch (TokenException tokenEx)
|
||||
{
|
||||
_logger.LogInformation($"[{summary}] : {tokenEx}");
|
||||
return Ok(APIResponse.Send("001", $"[{summary}], 토큰에 문제가 있음",Empty));
|
||||
return Ok(APIResponse.Send("001", $"[{summary}], 토큰에 문제가 있음", Empty));
|
||||
}
|
||||
catch (RefreshRevokeException refreshEx)
|
||||
{
|
||||
_logger.LogInformation($"[{summary}] : {refreshEx}");
|
||||
return Ok(APIResponse.Send("001", $"[{summary}], 폐기된 리프레시 토큰",Empty));
|
||||
return Ok(APIResponse.Send("001", $"[{summary}], 폐기된 리프레시 토큰", Empty));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, APIResponse.UnknownError(ex.Message));
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("academy")]
|
||||
[CustomOperation("학원 리스트 확인", "사용자가 등록된 학원 리스트 확인", "사용자")]
|
||||
public async Task<IActionResult> ReadAcademyInfo(string token, string refresh)
|
||||
{
|
||||
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(refresh)) return BadRequest(APIResponse.InvalidInputError());
|
||||
if(!ModelState.IsValid) return BadRequest(APIResponse.InvalidInputError());
|
||||
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(refresh))
|
||||
return BadRequest(APIResponse.InvalidInputError());
|
||||
if (!ModelState.IsValid) return BadRequest(APIResponse.InvalidInputError());
|
||||
string summary = String.Empty;
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
var validateToken = await _repositoryService.ValidateToken(token, refresh);
|
||||
summary = _repositoryService.ReadSummary(typeof(PushController), "ReadAcademyInfo");
|
||||
summary = _repositoryService.ReadSummary(typeof(UserController), "ReadAcademyInfo");
|
||||
|
||||
var academies = await (from ua in _dbContext.UserAcademy
|
||||
join a in _dbContext.Academy on ua.bid equals a.bid
|
||||
|
@ -104,19 +108,19 @@ public class UserController : ControllerBase
|
|||
bid = a.bid,
|
||||
name = a.business_name
|
||||
}).ToListAsync();
|
||||
|
||||
|
||||
return Ok(APIResponse.Send("000",$"[{summary}], 정상.",academies));
|
||||
|
||||
|
||||
return Ok(APIResponse.Send("000", $"[{summary}], 정상.", academies));
|
||||
}
|
||||
catch (TokenException tokenEx)
|
||||
{
|
||||
_logger.LogInformation($"[{summary}] : {tokenEx}");
|
||||
return Ok(APIResponse.Send("001", $"[{summary}], 토큰에 문제가 있음",Empty));
|
||||
return Ok(APIResponse.Send("001", $"[{summary}], 토큰에 문제가 있음", Empty));
|
||||
}
|
||||
catch (RefreshRevokeException refreshEx)
|
||||
{
|
||||
_logger.LogInformation($"[{summary}] : {refreshEx}");
|
||||
return Ok(APIResponse.Send("001", $"[{summary}], 폐기된 리프레시 토큰",Empty));
|
||||
return Ok(APIResponse.Send("001", $"[{summary}], 폐기된 리프레시 토큰", Empty));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -124,37 +128,38 @@ public class UserController : ControllerBase
|
|||
return StatusCode(500, APIResponse.UnknownError(ex.Message));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
[HttpGet("login")]
|
||||
[CustomOperation("SNS 로그인", "로그인 후 회원이 있는지 확인", "사용자")]
|
||||
public async Task<IActionResult> Login(string acctype, string sns_id)
|
||||
{
|
||||
// API 동작 파라미터 입력 값 확인
|
||||
if (string.IsNullOrEmpty(acctype) && string.IsNullOrEmpty(sns_id)) return BadRequest(APIResponse.InvalidInputError());
|
||||
if(!ModelState.IsValid) return BadRequest(APIResponse.InvalidInputError());
|
||||
if (string.IsNullOrEmpty(acctype) && string.IsNullOrEmpty(sns_id))
|
||||
return BadRequest(APIResponse.InvalidInputError());
|
||||
if (!ModelState.IsValid) return BadRequest(APIResponse.InvalidInputError());
|
||||
string summary = String.Empty;
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
summary = _repositoryService.ReadSummary(typeof(PushController), "Login");
|
||||
summary = _repositoryService.ReadSummary(typeof(UserController), "Login");
|
||||
|
||||
var login = await _dbContext.Login
|
||||
.FirstOrDefaultAsync(l => l.sns_type == acctype && l.sns_id == sns_id);
|
||||
|
||||
|
||||
if (login != null)
|
||||
{
|
||||
// 로그인 정보가 존재 하는 상황
|
||||
var user = await _dbContext.User
|
||||
.FirstOrDefaultAsync(u => u.uid == login.uid);
|
||||
|
||||
|
||||
// 회원 정보 없음
|
||||
if (user == null) return Ok(APIResponse.Send("002", $"[{summary}], 회원 정보 오류",Empty));
|
||||
|
||||
if (user == null) return Ok(APIResponse.Send("002", $"[{summary}], 회원 정보 오류", Empty));
|
||||
|
||||
// 정상적으로 User 테이블에도 있는것이 확인 됨
|
||||
user.login_date = DateTime.Now;
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
|
||||
// 토큰 생성은 로그인이 이제 되고 나서 한다.
|
||||
var accessToken = _jwtTokenService.GenerateJwtToken(login.uid);
|
||||
var refreshToken = _jwtTokenService.GenerateRefreshToken(login.uid);
|
||||
|
@ -170,7 +175,7 @@ public class UserController : ControllerBase
|
|||
log = $"[{summary}] : 정상"
|
||||
};
|
||||
await _repositoryService.SaveData<LogUser>(logUser);
|
||||
return Ok(APIResponse.Send("000",$"[{summary}], 정상",
|
||||
return Ok(APIResponse.Send("000", $"[{summary}], 정상",
|
||||
new { token = accessToken, refresh = refreshToken.refresh_token }));
|
||||
}
|
||||
else
|
||||
|
@ -183,13 +188,13 @@ public class UserController : ControllerBase
|
|||
log = $"[{summary}] : 실패"
|
||||
};
|
||||
await _repositoryService.SaveData<LogUser>(logUser);
|
||||
|
||||
|
||||
return Ok(APIResponse.InternalSeverError($"[{summary}], 로그인 저장 실패"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return Ok(APIResponse.Send("001", $"[{summary}], 로그인 정보 없음",Empty));
|
||||
|
||||
return Ok(APIResponse.Send("001", $"[{summary}], 로그인 정보 없음", Empty));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -197,20 +202,20 @@ public class UserController : ControllerBase
|
|||
return StatusCode(500, APIResponse.UnknownError(ex.Message));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[HttpPost("register")]
|
||||
[CustomOperation("회원 가입", "사용자 회원 가입", "사용자")]
|
||||
public async Task<IActionResult> UserRegister([FromBody] UserAll request)
|
||||
{
|
||||
if(!ModelState.IsValid) return BadRequest(APIResponse.InvalidInputError());
|
||||
if (!ModelState.IsValid) return BadRequest(APIResponse.InvalidInputError());
|
||||
string summary = String.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
summary = _repositoryService.ReadSummary(typeof(PushController), "UserRegister");
|
||||
summary = _repositoryService.ReadSummary(typeof(UserController), "UserRegister");
|
||||
var localPartEmail = request.email.Substring(0, request.email.IndexOf('@'));
|
||||
var uid = $"AM{localPartEmail}{DateTime.Now:yyyyMMdd}";
|
||||
|
||||
|
||||
var user = new User
|
||||
{
|
||||
uid = uid,
|
||||
|
@ -248,7 +253,7 @@ public class UserController : ControllerBase
|
|||
phone = request.phone,
|
||||
address = request.address
|
||||
};
|
||||
|
||||
|
||||
var logUser = new LogUser
|
||||
{
|
||||
uid = login.uid,
|
||||
|
@ -263,18 +268,18 @@ public class UserController : ControllerBase
|
|||
var saveContact = await _repositoryService.SaveData<Contact>(contact);
|
||||
if (saveUser && saveLogin && savePermission && saveContact)
|
||||
{
|
||||
|
||||
|
||||
var token = _jwtTokenService.GenerateJwtToken(uid);
|
||||
var refreshToken = _jwtTokenService.GenerateRefreshToken(uid);
|
||||
|
||||
if (await _repositoryService.SaveData<RefreshToken>(refreshToken))
|
||||
{
|
||||
logUser.log = $"[{summary}] : 정상";
|
||||
|
||||
|
||||
if (await _repositoryService.SaveData<LogUser>(logUser))
|
||||
_logger.LogError($"[{summary}] : 로그 저장 성공");
|
||||
|
||||
return Ok(APIResponse.Send("000",$"[{summary}], 정상",new
|
||||
|
||||
return Ok(APIResponse.Send("000", $"[{summary}], 정상", new
|
||||
{
|
||||
accessToken = token,
|
||||
refreshToken = refreshToken.refresh_token
|
||||
|
@ -285,10 +290,10 @@ public class UserController : ControllerBase
|
|||
_logger.LogError($"[{summary}] : 토큰 저장 실패");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
logUser.log = $"[{summary}] : 동작 실패";
|
||||
await _repositoryService.SaveData<LogUser>(logUser);
|
||||
|
||||
|
||||
return Ok(APIResponse.InternalSeverError());
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
@ -303,25 +308,27 @@ public class UserController : ControllerBase
|
|||
[CustomOperation("로그아웃", "사용자 로그아웃", "사용자")]
|
||||
public async Task<IActionResult> Logout(string token, string refresh)
|
||||
{
|
||||
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(refresh)) return BadRequest(APIResponse.InvalidInputError());
|
||||
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(refresh))
|
||||
return BadRequest(APIResponse.InvalidInputError());
|
||||
if (!ModelState.IsValid) return BadRequest(APIResponse.InvalidInputError());
|
||||
string summary = String.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
summary = _repositoryService.ReadSummary(typeof(PushController), "UserRegister");
|
||||
|
||||
summary = _repositoryService.ReadSummary(typeof(UserController), "UserRegister");
|
||||
|
||||
// 여기서 애초에 토큰 관련 에러가 2개가 나오게 만들어져 있음
|
||||
var validateToken = await _repositoryService.ValidateToken(token, refresh);
|
||||
|
||||
|
||||
var refreshToken = await _dbContext.RefreshTokens.FirstOrDefaultAsync(r => r.uid == validateToken.uid);
|
||||
|
||||
|
||||
if (refreshToken != null)
|
||||
{
|
||||
refreshToken.revoke_Date = DateTime.Now;
|
||||
await _repositoryService.SaveData<RefreshToken>(refreshToken);
|
||||
return Ok(APIResponse.Send("000", $"[{summary}], 로그아웃 정상", Empty));
|
||||
}
|
||||
|
||||
// 리프레시 토큰이 없다?? 그럼 이거 무조건 문제지 (이유를 알 수 없는)
|
||||
return Ok(APIResponse.UnknownError());
|
||||
}
|
||||
|
@ -341,7 +348,7 @@ public class UserController : ControllerBase
|
|||
|
||||
// [HttpGet("set")]
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
Loading…
Reference in New Issue
Block a user