All files / lib/hooks useSocialLogin.ts

29.62% Statements 8/27
0% Branches 0/8
25% Functions 2/8
29.62% Lines 8/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93                                              1x   1x                     1x                             1x                             1x             1x                 1x 1x                  
import React from 'react';
import { naverOauthClientId } from '@lib/config';
import { useRouter } from 'next/navigation';
import {
  removeLocalStorage,
  setLocalStorage,
  setLocalStorageObject
} from '@lib/utils/localStorage';
import { OauthData } from '@lib/models/oauthData';
 
interface UseSocialLoginProps {
  type: 'signin' | 'signup';
  redirectUrl: string;
  anonymousToken?: string;
  isDevelopment: boolean;
}
 
export default function useSocialLogin({
  type,
  redirectUrl,
  anonymousToken,
  isDevelopment
}: UseSocialLoginProps) {
  const router = useRouter();
 
  const beforeSocialLogin = React.useCallback(() => {
    setLocalStorage('redirectUrl', redirectUrl);
    if (anonymousToken) {
      setLocalStorage('anonymousToken', anonymousToken);
    }
    setLocalStorage('oauthType', type);
    if (isDevelopment) {
      setLocalStorage('tset', 'htua');
    }
  }, [redirectUrl, anonymousToken, type]);
 
  const handleClickNaver = React.useCallback(() => {
    beforeSocialLogin();
    // eslint-disable-next-line new-cap
    const naverIdLogin = new window.naver_id_login(
      naverOauthClientId,
      `${window.location.origin}/oauth/Naver`
    );
    const state = naverIdLogin.getUniqState();
    router.push(
      `https://nid.naver.com/oauth2.0/authorize?response_type=code&client_id=ualUr3Eo8TSpwhJ5PYjk&redirect_uri=${encodeURI(
        `${window.location.origin}/oauth/Naver`
      )}&state=${state}&version=js-2.0.0`
    );
  }, [type]);
 
  const handleClickApple = React.useCallback(() => {
    window.AppleID.auth.signIn().then((data: any) => {
      const oauthData = OauthData.parse({
        provider: 'Apple',
        token: data.authorization.id_token,
        id: JSON.parse(atob(data.authorization.id_token.split('.')[1])).sub,
        email: data.user ? data.user.email : undefined,
        name: data.user ? `${data.user.name.firstName} ${data.user.name.lastName}` : undefined
      });
      removeLocalStorage('tset'); // 애플 로그인은 redirect하지 않으므로 로컬스토리지 세팅이 필요없음
      setLocalStorageObject('oauthData', oauthData);
      window.location.reload();
    });
  }, [type]);
 
  const handleClickKakao = React.useCallback(() => {
    beforeSocialLogin();
    window.Kakao.Auth.authorize({
      redirectUri: `${window.location.origin}/oauth/Kakao`
    });
  }, [type]);
 
  const handleClickFacebook = React.useCallback(() => {
    beforeSocialLogin();
    router.push(
      `https://www.facebook.com/v16.0/dialog/oauth?client_id=1577067202416214&redirect_uri=${encodeURI(
        `${window.location.origin}/oauth/Facebook`
      )}`
    );
  }, []);
 
  return React.useMemo(
    () => ({
      handleClickNaver,
      handleClickApple,
      handleClickKakao,
      handleClickFacebook
    }),
    [handleClickNaver, handleClickApple, handleClickKakao, handleClickFacebook]
  );
}