Skip to content

Commit

Permalink
Merge pull request #367 from depromeet/feat/login
Browse files Browse the repository at this point in the history
fix: μ½”λ“œ μˆ˜μ •
  • Loading branch information
summermong authored Sep 2, 2024
2 parents 509213f + a288b86 commit 5992b8b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 39 deletions.
39 changes: 20 additions & 19 deletions app/apple/test/index.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
'use client';

import { useSetAtom } from 'jotai';
import { useRouter } from 'next/navigation';
import { useRouter, useSearchParams } from 'next/navigation';
import { useEffect } from 'react';

import { LoginLoading, LoginScreen } from '@/features/login';
import { AuthInfoAtom } from '@/store/auth';

const Page = () => {
const router = useRouter();
const searchParams = useSearchParams();
const setAuth = useSetAtom(AuthInfoAtom);

useEffect(() => {
const url = new URL(window.location.href);
const userId = Number(url.searchParams.get('userId'));
const nickname = url.searchParams.get('nickname') as string;
const isSignUpComplete = Boolean(url.searchParams.get('isSignUpComplete'));

setAuth({
isLogined: true,
nickname: nickname,
userId: userId,
});

if (isSignUpComplete) {
router.push('/');
} else {
router.push('/join/nickname');
const userId = searchParams.get('userId');
const nickname = searchParams.get('nickname');
const profileImageUrl = searchParams.get('profileImageUrl');
const isSignUpComplete = searchParams.get('isSignUpComplete');

if (userId && nickname && profileImageUrl && isSignUpComplete !== null) {
setAuth({
isLogined: true,
nickname,
userId: Number(userId),
});

if (isSignUpComplete) {
router.push('/');
} else {
router.push('/join/nickname');
}
}
}, [router, setAuth]);
}, [router, searchParams, setAuth]);

return (
<>
Expand Down
59 changes: 39 additions & 20 deletions app/kakao/oauth/index.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,58 @@
'use client';

import { error } from 'console';
import { useSetAtom } from 'jotai';
import { useRouter, useSearchParams } from 'next/navigation';
import { useEffect } from 'react';

import { LoginLoading, LoginScreen } from '@/features/login';
import { AuthInfoAtom } from '@/store/auth';
import { AuthResponse } from '@/types/authType';

const Page = () => {
const router = useRouter();
const searchParams = useSearchParams();
const setAuth = useSetAtom(AuthInfoAtom);

useEffect(() => {
const userId = searchParams.get('userId');
const nickname = searchParams.get('nickname');
const profileImageUrl = searchParams.get('profileImageUrl');
const isSignUpComplete = searchParams.get('isSignUpComplete');

if (userId && nickname && profileImageUrl && isSignUpComplete !== null) {
setAuth({
isLogined: true,
nickname,
userId: Number(userId),
});

if (isSignUpComplete) {
router.push('/');
} else {
router.push('/join/nickname');
const KAKAO_CODE = searchParams.get('code');

const postCode = async () => {
if (!KAKAO_CODE) {
console.error('μ½”λ“œκ°€ μœ νš¨ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.');
return;
}
try {
const response = await fetch(`/api/kakao/oauth?code=${KAKAO_CODE}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ code: KAKAO_CODE }),
});

if (response.status === 200) {
const data = (await response.json()) as AuthResponse;

setAuth({
isLogined: true,
nickname: data.data.data.nickname,
userId: data.data.data.userId,
});

if (data.data.data.isSignUpComplete) {
router.push('/');
} else {
router.push('/join/nickname');
}
}
} catch (error) {
console.error('Error:', error);
}
} else {
console.error(error);
}
};

postCode().catch((error) => {
console.error('Error:', error);
});
}, [router, searchParams, setAuth]);

return (
Expand Down

0 comments on commit 5992b8b

Please sign in to comment.