대제목
자바와 크게 다르지않다는 생각이든다..? 정의된지않은 형식에 한계를 느꼈나? 구현에대한 설계가 명확히 이루어져야하고
오류를 잡아내는데 기존은 commonjs보다 유리한만큼 엄격하다. strict 모드를 키지않아도 훨씬 엄격하다 할수있다.
로그인 관련 로직설계와 interface 설계
// 로그인 가입 관련된 작업
// 카카오, 네이버, 구글
import { Strategy } from "./auth";
export interface AuthProps {
email: string;
password: string;
}
interface AuthenticationResponse {
success: boolean;
message?: string;
}
interface Authenticator {
// (검증에 대한 요청 처리)
authenticate(credentials: AuthProps): Promise<AuthenticationResponse>;
}
// 이메일 로그인 클래스
export class EmailAuthenticator implements Authenticator {
async authenticate(credentials: AuthProps): Promise<AuthenticationResponse> {
// 로직은없다? 요청과 응답 코드가 들어갈부분
console.log("email login");
return { success: true };
}
}
// 카카오 로그인 로그인 로직 클래스
export class kakaoAuthcenticator implements Authenticator {
async authenticate(credentials: AuthProps): Promise<AuthenticationResponse> {
// 카카오 로그인 로직이 들어갈부분
console.log("kakao login");
return { success: true, message: "kakao login12" };
}
}
// 로그인에 대한 서비스를 처리할 클래스 구조
export interface LoginService {
// 로그인 로직에 대한 함수 구조
login(type: string, credentials: AuthProps): Promise<AuthenticationResponse>;
}
// 로그인 클래스에 로그인 서비스 구조를 상속받고
export class Login implements LoginService {
constructor(private readonly strategy: Strategy) {}
async login(
type: "kakao" | "email", // typedl Strategy 에서 받아온 타입의 키값으로 바뀜 email or kakao
credentials: AuthProps
): Promise<AuthenticationResponse> {
// strategy 로그인 로직이 들어있는 객체
// 여기에서 어떤 로그인 로직으로 처리할지 type 구분해서
const result = await this.strategy[type].authenticate(credentials);
return result;
}
}
실제 로그인 구현부
import {
EmailAuthenticator,
kakaoAuthcenticator,
AuthProps,
Login,
LoginService,
} from "./Authentication";
// I 를 붙이는것은 인터페이스라고 알리는것이다.
interface IEmailsender {
sendEmail(email: string): void;
}
class Emailsender implements IEmailsender {
sendEmail(email: string): void {}
}
export interface Strategy {
email: EmailAuthenticator;
kakao: kakaoAuthcenticator;
}
class Auth {
constructor(
private readonly authporps: AuthProps,
private readonly emailsender: Emailsender,
private readonly loginservice: LoginService
) {}
//로그인 로직
public async login() {
console.log(this);
await this.loginservice.login("kakao", this.authporps);
}
// 이메일 인증 처리 구조
public register(): void {
this.emailsender.sendEmail(this.authporps.email);
}
}
// 유저의 이메일과 password 임시객체
const authPorps: AuthProps = {
email: "gusdnr205@naver.com",
password: "1234",
};
const _emailSender = new Emailsender();
// email 로그인 로직 클래스 동적할당
const _email = new EmailAuthenticator();
//kakao 로그인 로직 클래스동적할당
const _kakao = new kakaoAuthcenticator();
// 로그인 서비스 로직을 가지고있는 객체
const _startegy: Strategy = {
email: _email,
kakao: _kakao,
};
const _loginService = new Login(_startegy);
const auth = new Auth(authPorps, _emailSender, _loginService);
auth.login();
해설 설명?
코드에 대한 궁금증과 gpt답변 그리고 내생각






근데 교수님이 구현해준 이코드는 emailsender가 딱히 필요해보이지않는데?.. ?
'언어' 카테고리의 다른 글
| 20230830 타입스크립트 (0) | 2023.08.30 |
|---|