forked from AcaMate/AcaMate_Web
[✨] 쿠키 동작
1. 쿠키 추가, 불러오기 2. API 읽어오기 3. Header 값 쿠키 저장 하기
This commit is contained in:
parent
9167e2a9d6
commit
899a563aac
33
App.razor.cs
33
App.razor.cs
|
@ -3,35 +3,40 @@ using System.Net.Http.Json;
|
|||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.JSInterop;
|
||||
|
||||
using Front.Program.Models;
|
||||
using Front.Program.Services;
|
||||
|
||||
namespace Front;
|
||||
|
||||
public partial class App : ComponentBase
|
||||
{
|
||||
[Inject] HttpClient Http { get; set; } = default!;
|
||||
[Inject] IJSRuntime JS { get; set; } = default!;
|
||||
// [Inject] private HttpClient Http { get; set; } = default;
|
||||
[Inject] private APIService API { get; set; } = default!;
|
||||
[Inject] private CookieService Cookie { get; set; } = default!;
|
||||
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if (firstRender)
|
||||
{
|
||||
// Web_AM_Connect_Key 쿠키를 가져오는 JavaScript 함수를 호출합니다.
|
||||
var header = await JS.InvokeAsync<string>("blazorGetCookie", "Web_AM_Connect_Key");
|
||||
var cookie = await Cookie.GetCookieAsync("Web_AM_Connect_Key");
|
||||
|
||||
// 값 없으면 API 호출
|
||||
if (string.IsNullOrEmpty(header))
|
||||
if (string.IsNullOrEmpty(cookie))
|
||||
{
|
||||
var response = await Http.GetFromJsonAsync<ApiResult>("/api/v1/in/app?type=W&specific=Web_Connect&project=AcaMate");
|
||||
Console.WriteLine($"COOKIE: {response.header}");
|
||||
if (!string.IsNullOrEmpty(response?.header))
|
||||
var response = await API.GetJsonAsync<APIHeader, AppHeader>(
|
||||
"/api/v1/in/app",
|
||||
new AppHeader()
|
||||
{
|
||||
type = "W",
|
||||
specific = "Web_Connect",
|
||||
project = "AcaMate"
|
||||
});
|
||||
if (!string.IsNullOrEmpty(response.data.header))
|
||||
{
|
||||
Console.WriteLine($"NO?");
|
||||
await JS.InvokeVoidAsync("blazorSetCookie", "Web_AM_Connect_Key", response.header, 1);
|
||||
await Cookie.SetCookieAsync("Web_AM_Connect_Key", response.data.header);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public class ApiResult
|
||||
{
|
||||
public string header { get; set; }
|
||||
}
|
||||
}
|
|
@ -25,5 +25,6 @@ builder.Services.AddScoped(sp => //new HttpClient
|
|||
|
||||
// SCOPED 으로 등록된 서비스는 DI 컨테이너에 등록된 서비스의 인스턴스를 사용합니다.
|
||||
builder.Services.AddScoped<APIService>();
|
||||
builder.Services.AddScoped<CookieService>();
|
||||
|
||||
await builder.Build().RunAsync();
|
||||
|
|
32
Program/Models/APIResponse.cs
Normal file
32
Program/Models/APIResponse.cs
Normal file
|
@ -0,0 +1,32 @@
|
|||
using System.Text.Json;
|
||||
|
||||
namespace Front.Program.Models
|
||||
{
|
||||
public class APIResponseStatus<T>
|
||||
{
|
||||
public Status status { get; set; }
|
||||
public T? data { get; set; }
|
||||
|
||||
public string JsonToString()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
}
|
||||
}
|
||||
|
||||
public class Status
|
||||
{
|
||||
public string code { get; set; }
|
||||
public string message { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public class AppHeader
|
||||
{
|
||||
public string type { get; set; }
|
||||
public string specific { get; set; }
|
||||
public string project { get; set; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
6
Program/Models/AppModel.cs
Normal file
6
Program/Models/AppModel.cs
Normal file
|
@ -0,0 +1,6 @@
|
|||
namespace Front.Program.Models;
|
||||
|
||||
public class APIHeader
|
||||
{
|
||||
public string header { get; set; } = string.Empty;
|
||||
}
|
|
@ -1,12 +1,31 @@
|
|||
using System.Net.Http.Json;
|
||||
using Front.Program.Models;
|
||||
|
||||
namespace Front.Program.Services;
|
||||
|
||||
public class APIService
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
|
||||
|
||||
public APIService(HttpClient http)
|
||||
{
|
||||
_http = http;
|
||||
}
|
||||
|
||||
|
||||
private string ChangeToString<T>(T data)
|
||||
{
|
||||
if (data == null) return string.Empty;
|
||||
var properties = typeof(T).GetProperties();
|
||||
var value = properties.Select(p => $"{p.Name}={p.GetValue(data)}");
|
||||
return string.Join("&", value);
|
||||
}
|
||||
|
||||
public async Task<APIResponseStatus<TResponse>?> GetJsonAsync<TResponse,TRequest>(string url, TRequest value)
|
||||
{
|
||||
string parameter = ChangeToString(value);
|
||||
var response = await _http.GetFromJsonAsync<APIResponseStatus<TResponse>>($"{url}?{parameter}");
|
||||
return response;
|
||||
}
|
||||
}
|
24
Program/Services/CookieService.cs
Normal file
24
Program/Services/CookieService.cs
Normal file
|
@ -0,0 +1,24 @@
|
|||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.JSInterop;
|
||||
|
||||
namespace Front.Program.Services;
|
||||
|
||||
public class CookieService
|
||||
{
|
||||
private readonly IJSRuntime _js;
|
||||
|
||||
public CookieService(IJSRuntime js)
|
||||
{
|
||||
_js = js;
|
||||
}
|
||||
|
||||
public async Task SetCookieAsync(string key, string value)
|
||||
{
|
||||
await _js.InvokeVoidAsync("setCookie", key, value, 1);
|
||||
}
|
||||
|
||||
public async Task<string?> GetCookieAsync(string key)
|
||||
{
|
||||
return await _js.InvokeAsync<string>("getCookie", key);
|
||||
}
|
||||
}
|
|
@ -34,6 +34,7 @@
|
|||
|
||||
<!-- Blazor WASM 로딩 스크립트 -->
|
||||
<script src="_framework/blazor.webassembly.js" autostart="false"></script>
|
||||
|
||||
<script>
|
||||
const MIN_LOADING_MS = 2700;
|
||||
const loadingStart = performance.now();
|
||||
|
@ -49,14 +50,13 @@
|
|||
console.error("❌ Blazor 로딩 실패", err);
|
||||
});
|
||||
</script>
|
||||
|
||||
<script>
|
||||
window.blazorGetCookie = function (name) {
|
||||
alert("blazorGetCookie 호출됨");
|
||||
window.getCookie = function (name) {
|
||||
const v = document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)');
|
||||
return v ? v.pop() : '';
|
||||
};
|
||||
window.blazorSetCookie = function (name, value, days) {
|
||||
alert("blazorSetCookie 호출됨");
|
||||
window.setCookie = function (name, value, days) {
|
||||
let expires = "";
|
||||
if (days) {
|
||||
const date = new Date();
|
||||
|
@ -64,7 +64,6 @@
|
|||
expires = "; expires=" + date.toUTCString();
|
||||
}
|
||||
document.cookie = name + "=" + (value || "") + expires + "; path=/";
|
||||
console.log("쿠키 생성됨:", document.cookie);
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
|
|
Loading…
Reference in New Issue
Block a user