Skip to content

Commit

Permalink
[BE] FEAT: anonymous login
Browse files Browse the repository at this point in the history
  • Loading branch information
enaenen committed Mar 8, 2024
1 parent 74a62f8 commit e17a5dc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package org.ftclub.cabinet.auth.service;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.concurrent.ExecutionException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.ftclub.cabinet.admin.admin.domain.Admin;
import org.ftclub.cabinet.admin.admin.service.AdminCommandService;
Expand All @@ -14,31 +20,23 @@
import org.ftclub.cabinet.user.service.UserQueryService;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.concurrent.ExecutionException;

/**
* 인증 관련 비즈니스 로직을 처리하는 서비스입니다.
*/
@Service
@RequiredArgsConstructor
public class AuthFacadeService {

private static final String REDIRECT_COOKIE_NAME = "redirect";
private final UserQueryService userQueryService;
private final UserCommandService userCommandService;
private final AdminQueryService adminQueryService;
private final AdminCommandService adminCommandService;
private final UserOauthService userOauthService;
private final AdminOauthService adminOauthService;
private final AuthPolicyService authPolicyService;

private final TokenProvider tokenProvider;
private final CookieManager cookieManager;
private static final String REDIRECT_COOKIE_NAME = "redirect";

/**
* 유저 로그인 페이지로 리다이렉트합니다.
Expand Down Expand Up @@ -95,6 +93,23 @@ public void handleUserLogin(HttpServletRequest req, HttpServletResponse res, Str
res.sendRedirect(authPolicyService.getMainHomeUrl());
}

public void handlePublicLogin(HttpServletRequest req, HttpServletResponse res, String name)
throws IOException {

User user = userQueryService.findUser(name).orElseThrow(
ExceptionStatus.NOT_FOUND_USER::asServiceException);
String token = tokenProvider.createUserToken(user, LocalDateTime.now());
Cookie cookie = cookieManager.cookieOf(TokenProvider.USER_TOKEN_NAME, token);
cookieManager.setCookieToClient(res, cookie, "/", req.getServerName());
if (cookieManager.getCookieValue(req, REDIRECT_COOKIE_NAME) != null) {
String redirect = cookieManager.getCookieValue(req, REDIRECT_COOKIE_NAME);
cookieManager.deleteCookie(res, REDIRECT_COOKIE_NAME);
res.sendRedirect(redirect);
return;
}
res.sendRedirect(authPolicyService.getMainHomeUrl());
}

/**
* 관리자 로그인 콜백으로 받은 authorization_code로 관리자 프로필 정보를 가져오고, 반환합니다.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.ftclub.cabinet.openpublic.controller;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.ftclub.cabinet.alarm.config.AlarmProperties;
import org.ftclub.cabinet.auth.service.AuthFacadeService;
import org.ftclub.cabinet.log.Logging;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Logging
@RequiredArgsConstructor
@RequestMapping("/public")
@RestController
public class LoginController {

private final AuthFacadeService authFacadeService;
private final AlarmProperties alarmProperties;

@GetMapping("/login")
public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
final String username = "anonymous";
if (!alarmProperties.getIsProduction()) {
authFacadeService.handlePublicLogin(request, response, username);
}
}
}

0 comments on commit e17a5dc

Please sign in to comment.