forked from AcaMate/AcaMate_API
[✨] 카카오 로그인
1. 카카오 로그인 인증 snsID 받아오기 2. 리다이렉트 동작 수정 3. 세션을 통한 토큰 저장 3.1. 세션서비스 생성 4. 회원가입 화면으로 이동
This commit is contained in:
parent
65962c01c2
commit
3ebb7137c0
|
@ -118,12 +118,17 @@ builder.Services.AddHostedService<PushBackgroundService>();
|
|||
// PUSH 설정부 끝
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// 세션 설정
|
||||
// IN-MEMORY 캐시
|
||||
builder.Services.AddDistributedMemoryCache();
|
||||
builder.Services.AddSession();
|
||||
|
||||
// ==== SCOPED 으로 등록 할 서비스 ==== //
|
||||
// 여기다가 API 있는 컨트롤러들 AddScoped 하면 되는건가?
|
||||
builder.Services.AddScoped<JwtTokenService>();
|
||||
builder.Services.AddScoped<ILogRepository, LogRepository>();
|
||||
builder.Services.AddScoped<IRepositoryService, RepositoryService>();
|
||||
builder.Services.AddScoped<ISessionService, SessionService>();
|
||||
builder.Services.AddScoped<IHeaderConfig, HeaderConfigRepository>();
|
||||
|
||||
builder.Services.AddScoped<IUserService, UserService>();
|
||||
|
@ -215,6 +220,7 @@ app.UseStaticFiles(new StaticFileOptions
|
|||
});
|
||||
|
||||
app.UseRouting();
|
||||
app.UseSession();
|
||||
app.UseCors("CorsPolicy");
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
|
|
@ -25,8 +25,9 @@ namespace Back.Program.Controllers.V1
|
|||
private readonly JwtTokenService _jwtTokenService;
|
||||
private readonly IAppService _appService;
|
||||
private readonly IAppRepository _appRepository;
|
||||
private readonly ISessionService _sessionService;
|
||||
|
||||
public AppController(AppDbContext dbContext, ILogger<AppController> logger, IRepositoryService repositoryService, JwtTokenService jwtTokenService,IAppService appService, IAppRepository appRepository)
|
||||
public AppController(AppDbContext dbContext, ILogger<AppController> logger, IRepositoryService repositoryService, JwtTokenService jwtTokenService,IAppService appService, IAppRepository appRepository, ISessionService sessionService)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_logger = logger;
|
||||
|
@ -34,6 +35,7 @@ namespace Back.Program.Controllers.V1
|
|||
_jwtTokenService = jwtTokenService;
|
||||
_appService = appService;
|
||||
_appRepository = appRepository;
|
||||
_sessionService = sessionService;
|
||||
}
|
||||
|
||||
|
||||
|
@ -70,9 +72,14 @@ namespace Back.Program.Controllers.V1
|
|||
var result = await _appService.RetryAccess(summary, refresh);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
[HttpGet("session")]
|
||||
[CustomOperation("세션 정보 읽어오기", "세션 정보를 읽어오는 동작 수행", "시스템")]
|
||||
public async Task<IActionResult> GetSessionData(string key)
|
||||
{
|
||||
var value = _sessionService.GetString(key);
|
||||
return Ok(new {key = value});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using Back.Program.Common.Model;
|
||||
using Back.Program.Controllers.V1;
|
||||
using Back.Program.Services.V1;
|
||||
using Back.Program.Services.V1.Interfaces;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Back.Program.Models.APIResponses;
|
||||
|
||||
namespace Back.Program.Controllers;
|
||||
|
||||
|
@ -16,14 +19,16 @@ public class OutController: ControllerBase
|
|||
private readonly IRepositoryService _repositoryService;
|
||||
private readonly IUserService _userService;
|
||||
private readonly IKakaoService _kakaoService;
|
||||
private readonly ISessionService _sessionService;
|
||||
|
||||
public OutController(ILogger<OutController> logger,
|
||||
IRepositoryService repositoryService, IUserService userService, IKakaoService kakaoService)
|
||||
IRepositoryService repositoryService, IUserService userService, IKakaoService kakaoService, ISessionService sessionService)
|
||||
{
|
||||
_logger = logger;
|
||||
_repositoryService = repositoryService;
|
||||
_userService = userService;
|
||||
_kakaoService = kakaoService;
|
||||
_sessionService = sessionService;
|
||||
}
|
||||
[HttpGet("kakao/auth")]
|
||||
[CustomOperation("카카오 로그인", "카카오 로그인 동작", "사용자")]
|
||||
|
@ -49,9 +54,35 @@ public class OutController: ControllerBase
|
|||
if (json.RootElement.TryGetProperty("id", out var idElement))
|
||||
{
|
||||
var snsId = idElement.ToString();
|
||||
Console.WriteLine($"ID = {snsId}");
|
||||
Console.WriteLine($"ID = {snsId}");
|
||||
var loginResult = await _userService.Login("SNS Login", "ST01", snsId);
|
||||
Console.WriteLine($"login = {loginResult.JsonToString()}");
|
||||
if (loginResult.status.code == "000")
|
||||
{
|
||||
|
||||
var data = loginResult.data as LoginAPIResponse ?? new LoginAPIResponse();
|
||||
if (data != null)
|
||||
{
|
||||
string token = data.token;
|
||||
string refresh = data.refresh;
|
||||
if (await _sessionService.SetString("token", token) &&
|
||||
await _sessionService.SetString("refresh", refresh))
|
||||
{
|
||||
return Redirect("/about");
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (loginResult.status.code == "001")
|
||||
{
|
||||
if (await _sessionService.SetString("snsId", snsId))
|
||||
{
|
||||
return Redirect("/auth/register");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest(new { error = "로그인 실패", message = loginResult.status.message });
|
||||
}
|
||||
// return Ok(new { id="cc" });
|
||||
}
|
||||
}
|
||||
|
|
7
Program/Models/APIResponses/API_User.cs
Normal file
7
Program/Models/APIResponses/API_User.cs
Normal file
|
@ -0,0 +1,7 @@
|
|||
namespace Back.Program.Models.APIResponses;
|
||||
|
||||
public class LoginAPIResponse
|
||||
{
|
||||
public string token { get; set; } = string.Empty;
|
||||
public string refresh { get; set; } = string.Empty;
|
||||
}
|
12
Program/Services/V1/Interfaces/IRepositoryService.cs
Normal file
12
Program/Services/V1/Interfaces/IRepositoryService.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
using System.Linq.Expressions;
|
||||
|
||||
namespace Back.Program.Services.V1.Interfaces;
|
||||
|
||||
public interface IRepositoryService
|
||||
{
|
||||
// Task<ValidateToken> ValidateToken(string token, string refresh);
|
||||
Task<bool> SaveData<T>(T entity, Expression<Func<T, object>> key = null) where T : class;
|
||||
Task<bool> DeleteData<T>(T entity, Expression<Func<T, object>> key = null) where T : class;
|
||||
String ReadSummary(Type type, String name);
|
||||
Task SendFrontData<T>(T data, string url);
|
||||
}
|
8
Program/Services/V1/Interfaces/ISessionService.cs
Normal file
8
Program/Services/V1/Interfaces/ISessionService.cs
Normal file
|
@ -0,0 +1,8 @@
|
|||
namespace Back.Program.Services.V1.Interfaces;
|
||||
|
||||
public interface ISessionService
|
||||
{
|
||||
Task<bool> SetString(string key, string value);
|
||||
Task<(bool result, string data)> GetString(string key);
|
||||
Task<bool> Remove(string key);
|
||||
}
|
|
@ -6,19 +6,11 @@ using Back.Program.Common.Auth;
|
|||
using Back.Program.Common.Data;
|
||||
using Back.Program.Common.Model;
|
||||
using Back.Program.Models.Entities;
|
||||
using Back.Program.Services.V1.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Back.Program.Services.V1
|
||||
{
|
||||
public interface IRepositoryService
|
||||
{
|
||||
// Task<ValidateToken> ValidateToken(string token, string refresh);
|
||||
Task<bool> SaveData<T>(T entity, Expression<Func<T, object>> key = null) where T : class;
|
||||
Task<bool> DeleteData<T>(T entity, Expression<Func<T, object>> key = null) where T : class;
|
||||
String ReadSummary(Type type, String name);
|
||||
Task SendFrontData<T>(T data, string url);
|
||||
}
|
||||
|
||||
public class RepositoryService: IRepositoryService
|
||||
{
|
||||
private readonly AppDbContext _dbContext;
|
||||
|
|
50
Program/Services/V1/SessionService.cs
Normal file
50
Program/Services/V1/SessionService.cs
Normal file
|
@ -0,0 +1,50 @@
|
|||
using Back.Program.Services.V1.Interfaces;
|
||||
|
||||
namespace Back.Program.Services.V1;
|
||||
|
||||
public class SessionService: ISessionService
|
||||
{
|
||||
private readonly IHttpContextAccessor _http;
|
||||
public SessionService(IHttpContextAccessor http)
|
||||
{
|
||||
_http = http;
|
||||
}
|
||||
public Task<bool> SetString(string key, string value)
|
||||
{
|
||||
try
|
||||
{
|
||||
_http.HttpContext.Session.SetString(key, value);
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
}
|
||||
public Task<(bool result, string data)> GetString(string key)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = _http.HttpContext.Session.GetString(key);
|
||||
return Task.FromResult((true, value ?? string.Empty));
|
||||
}
|
||||
catch
|
||||
{
|
||||
return Task.FromResult((false, ""));
|
||||
}
|
||||
}
|
||||
public Task<bool> Remove(string key)
|
||||
{
|
||||
try
|
||||
{
|
||||
_http.HttpContext.Session.Remove(key);
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user