-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpage.tsx
More file actions
210 lines (195 loc) · 6.16 KB
/
page.tsx
File metadata and controls
210 lines (195 loc) · 6.16 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
'use client';
import { getOauthAccessToken } from '@/api/login/oauth';
import { login } from '@/api/login/user';
import Loader from '@/components/common/Loader';
import { signupState } from '@/recoil/signupStore';
import { Provider } from '@/types/login';
import {
clearLetterUrl,
setOnboarding,
setRecentLogin,
setTokens
} from '@/utils/storage';
import axios from 'axios';
import { useRouter } from 'next/navigation';
import { useEffect, useState } from 'react';
import { useRecoilState } from 'recoil';
import styled from 'styled-components';
const Auth = () => {
const [registerToken, setRegisterToken] = useRecoilState(signupState);
const router = useRouter();
const [absoluteUrl, setAbsoluteUrl] = useState('');
const [storeUrl, setstoreUrl] = useState('');
const [type, setType] = useState('');
const [oauthAccessToken, setOauthAccessToken] = useState('');
useEffect(() => {
if (typeof window !== 'undefined') {
//oauth 타입을 state에 저장
const params = new URL(window.location.href).searchParams;
const typeParam = params.get('type');
setType(typeParam);
const url = `${window.location.protocol}//${window.location.host}/login/auth?type=${typeParam}`;
const letterId = localStorage.getItem('letter_url');
setAbsoluteUrl(url);
if (letterId) setstoreUrl(letterId);
}
}, []);
useEffect(() => {
if (!absoluteUrl || !type) return;
const getToken = async () => {
const AUTHORIZATION_CODE = new URL(window.location.href).searchParams.get(
'code'
);
const STATE = new URL(window.location.href).searchParams.get('state');
const TYPE = new URL(window.location.href).searchParams.get('type');
if (!AUTHORIZATION_CODE || !TYPE) {
console.error('Authorization Code or Type is missing');
return;
}
//type에 따라 다른 토큰 url 지정
switch (TYPE) {
case 'kakao':
try {
const response = await axios.post(
'https://kauth.kakao.com/oauth/token',
new URLSearchParams({
grant_type: 'authorization_code',
client_id: process.env.NEXT_PUBLIC_KAKAO_REST_API_KEY,
redirect_uri: absoluteUrl,
code: AUTHORIZATION_CODE
}),
{
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}
);
setOauthAccessToken(response.data.access_token);
} catch (error) {
console.error(error);
clearLetterUrl();
return;
}
break;
case 'google':
try {
const body = new URLSearchParams({
grant_type: 'authorization_code',
client_id: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID!,
client_secret: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_SECRET!,
redirect_uri: absoluteUrl,
code: AUTHORIZATION_CODE
});
const response = await axios.post(
'https://oauth2.googleapis.com/token',
body.toString(),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
);
setOauthAccessToken(response.data.access_token);
} catch (error) {
console.error('Unsupported OAuth type:', type);
clearLetterUrl();
return;
}
break;
case 'naver':
try {
const response = await getOauthAccessToken(
'NAVER' as Provider,
AUTHORIZATION_CODE,
STATE
);
setOauthAccessToken(response);
} catch (error) {
console.error('Unsupported OAuth type:', type);
clearLetterUrl();
return;
}
break;
default:
console.error('Unknown OAuth type:', TYPE);
return;
}
};
getToken();
}, [absoluteUrl, type]);
useEffect(() => {
try {
if (oauthAccessToken) {
login(type?.toUpperCase() as Provider, oauthAccessToken)
.then((res) => {
console.log('accessToken', res.data.accessToken);
setTokens(res.data.accessToken, res.data.refreshToken);
/* 온보딩 여부 저장 */
setOnboarding(res.data.isProcessedOnboarding);
/* 최근 로그인 정보 저장 */
setRecentLogin(type);
if (storeUrl) {
router.push(`/verify/letter?url=${storeUrl}`);
clearLetterUrl();
} else {
router.push('/planet');
}
})
.catch((error) => {
if (error.response && error.response.status === 401) {
console.log('registerToken', error.response.data.registerToken);
setRegisterToken(error.response.data.registerToken);
if (storeUrl) {
router.push(`/signup/step1?url=${storeUrl}`);
clearLetterUrl();
} else {
router.push('/signup/step1');
}
}
});
}
} catch (error) {
console.log('oauth token 에러');
console.error(error);
clearLetterUrl();
return;
}
}, [oauthAccessToken]);
return (
<Container>
<LoaderContainer>
<Loader />
<Guidetext>
로그인 중입니다
<br />
잠시만 기다려주세요...
</Guidetext>
</LoaderContainer>
</Container>
);
};
export default Auth;
const Container = styled.div`
display: flex;
box-sizing: border-box;
flex-direction: column;
height: 100%;
padding: 25px;
background: ${(props) => props.theme.colors.bg};
`;
const LoaderContainer = styled.div`
width: 100%;
height: 100%;
min-height: 600px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
`;
const Guidetext = styled.div`
width: 100%;
display: flex;
text-align: center;
justify-content: center;
${(props) => props.theme.fonts.regular16};
color: ${(props) => props.theme.colors.gray300};
padding-top: 10px;
`;