-
Notifications
You must be signed in to change notification settings - Fork 1
[Feat] 웹뷰 자동 로그인 구현 #195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
The head ref may contain hidden characters: "194-feat-rn-webview-\uBE0C\uB9BF\uC9C0-\uD1B5\uC2E0-\uAD6C\uD604"
Merged
[Feat] 웹뷰 자동 로그인 구현 #195
Changes from all commits
Commits
Show all changes
62 commits
Select commit
Hold shift + click to select a range
c90736d
Chore: turbo.json globalEnv 추가
miraclee1226 eb053ae
Feat: WebView 환경 감지하여 API URL 분기 처리
miraclee1226 bd1c2bf
Feat: postMessage 처리 추가하여 WebView 로그인/로그아웃 상태 동기화
miraclee1226 22ff9da
Feat: 개발용 WebView 설정 및 message handler 연결
miraclee1226 2327638
Feat: Webview 환경에서 페이지 전환 로직 분기 처리
miraclee1226 e5fe6bf
Chore: 줄바꿈 (prettier)
miraclee1226 2f0b978
Chore: @react-native-async-storage/async-storage install
miraclee1226 77a8806
Chore: 의존성 업데이트트
miraclee1226 0e82561
Feat: window.d.ts 추가
miraclee1226 c2c1666
Chore: 모바일 앱 개발을 위한 환경 변수 업데이트
miraclee1226 c0c220f
Refactor: messageHandler를 webviewLoginBridge로 이름 변경
miraclee1226 b33fd22
Feat: 웹뷰에서 자동로그인을 위한 토큰 전송 구현
miraclee1226 0279355
Feat: 웹뷰 자동로그인을 위한 웹 측 리스너 구현
miraclee1226 1aad308
Feat: 웹에서 앱으로 로그인/로그아웃 메시지 전송 구현
miraclee1226 9456b8a
Feat: 웹뷰 환경에서 자동로그인을 위한 토큰 검증 로직 추가
miraclee1226 9ebdc3f
Chore: prettier 적용용
miraclee1226 f9740e9
Chore: 의존성 업데이트
miraclee1226 050107d
Refactor: window.d.ts -> global.d.ts로 변경
miraclee1226 3508d40
Refactor: 로그인 API 로직 개선 및 토큰 검증 로직직 추가
miraclee1226 341d3de
Refactor: useIsReactNativeWebview-> useWebview로 훅 이름 변경
miraclee1226 2cc7189
Chore: globalEnv 수정
miraclee1226 470a4dd
Chore: 의존성 업데이트
miraclee1226 8c266b9
Chore: global.d.ts platform 제거거
miraclee1226 054f0aa
Refactor: 웹뷰 이벤트 핸들러 로직 분리
miraclee1226 4f123f4
Refactor: 웹뷰 메시지 처리 로직 개선
miraclee1226 5269de5
Refactor: 웹뷰 메세지 타입 상수화
miraclee1226 32347cc
Remove: useWebview.ts 삭제
miraclee1226 fbf04fc
Chore: Merge branch 'develop' of https://github.com/codeit-internship…
miraclee1226 031691c
Chore: globalEnv 설정 추가
miraclee1226 0d78266
Chore: 파일명 대소문자 변경
miraclee1226 ef843a9
Chore: React import 구문 추가
miraclee1226 583ebb5
Refactor: getBaseUrl -> getNativeApiUrl로 변경
miraclee1226 3a5dbe9
Refactor: parseMessageEvent -> parseMessage로 변경
miraclee1226 d45af81
Fix: token 유효성 검사 로직 제거
miraclee1226 f00ba88
Refactor: WebView API URL 체크 로직을 getWebApiUrl 유틸 함수로 분리
miraclee1226 9734517
Chore: 불필요한 className 제거
miraclee1226 7cf18cd
Style: MembersSettingModal의 gap 간격 제거
miraclee1226 4fbc13e
Refactor: WebView 메시지 핸들러에 accessToken 체크 로직 추가 및 불필요한 코드 제거
miraclee1226 b1866a9
Chore: AuthGuard에서 웹뷰 리다이렉트 로직 제거
miraclee1226 5d45e13
Feat: WebView 메시지에 따른 분기처리
miraclee1226 4fd40cf
Chore: 의존성 업데이트트
miraclee1226 656a9ba
Chore: react-device-detect 라이브러리 제거
miraclee1226 c7df4cb
Refactor: JSON.stringify를 stringifyJson 유틸함수로 대체
miraclee1226 4925109
Refactor: WebView 감지 로직 개선
miraclee1226 9e33165
Refactor: react-device-detect 의존성 제거 후 내부 유틸함수로 대체
miraclee1226 feb13bf
Refactor: WebView 이벤트 핸들링 구조 개선
miraclee1226 a57e38b
Refactor: 프로젝트 전반적인 네이밍 Native -> WebView로 통일
miraclee1226 4305856
Refactor: AsyncStorage 핸들링 구조 개선
miraclee1226 5343fce
Refactor: WebView 메시지 브릿지 구조 개선
miraclee1226 26f0889
Chore: detectWebView.ts 삭제
miraclee1226 d05d646
Refactor: 메시지 핸들러 파라미터 수정 및 불필요한 WebView 속성 제거
miraclee1226 57fab4a
Chore: import 문 줄바꿈
miraclee1226 f60439c
Refactor: WebView 디바이스 감지 로직 간소화
miraclee1226 a1ea33f
Refactor: WebView 디바이스 감지 로직을 별도 유틸리티로 분리하여 hook 간소화
miraclee1226 f11ecfd
Refactor: WebView 메시지 브릿지를 이벤트 리스너로 변경
miraclee1226 41ff8e4
Chore: sendMessageToWebView import 경로를 절대 경로로 변경
miraclee1226 7ac1522
Refactor: switch 문의 case 블록 스코프 처리
miraclee1226 ac573c1
Chore: import React 문 제거
miraclee1226 9857a2b
Chore: Merge branch 'develop' of https://github.com/codeit-internship…
miraclee1226 7a114d1
Chore: ReactNativeWebView 객체 접근 방식을 옵셔널 체이닝으로 변경
miraclee1226 9f8524f
Fix: 불필요한 return 제거
miraclee1226 d46b87c
Merge branch 'develop' into 194-feat-rn-webview-브릿지-통신-구현
miraclee1226 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,33 @@ | ||
| import { useRef } from "react"; | ||
| import { WebView, WebViewMessageEvent } from "react-native-webview"; | ||
|
|
||
| import { useHandleNavigationActions } from "@/hooks/useHandleNavigationActions"; | ||
| import { getBaseUrl } from "@/utils/getBaseUrl"; | ||
| import { handleAuthStorage } from "@/store/authStorage"; | ||
| import { getWebViewApiUrl } from "@/utils/getWebViewApiUrl"; | ||
| import { parseMessage } from "@/utils/parseMessage"; | ||
| import { webViewLoadHandler } from "@/utils/webViewLoadHandler"; | ||
|
|
||
| export default function HomeScreen() { | ||
| // TODO : login상태에 따른 분기 처리 설정 | ||
| const webviewRef = useRef<WebView>(null); | ||
|
|
||
| const baseUrl = getBaseUrl(); | ||
| const baseUrl = getWebViewApiUrl(); | ||
| const handleNavigationActions = useHandleNavigationActions(); | ||
| const { event, handler } = webViewLoadHandler(webviewRef); | ||
|
|
||
| const requestOnMessage = (e: WebViewMessageEvent) => { | ||
| handleNavigationActions(e); | ||
| const handleMessage = (e: WebViewMessageEvent) => { | ||
| const { type, data } = parseMessage(e); | ||
|
|
||
| handleNavigationActions({ type, data }); | ||
| handleAuthStorage({ type, data }); | ||
| }; | ||
|
|
||
| return <WebView className="flex-1" source={{ uri: `${baseUrl}` }} onMessage={requestOnMessage} />; | ||
| return ( | ||
| <WebView | ||
| ref={webviewRef} | ||
| source={{ uri: `${baseUrl}` }} | ||
| onMessage={handleMessage} | ||
| {...{ [event]: handler }} | ||
| className="flex-1" | ||
| /> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import AsyncStorage from "@react-native-async-storage/async-storage"; | ||
| import { WEBVIEW_MESSAGE_TYPES } from "@repo/constants"; | ||
| import { IUser } from "@repo/types/userType"; | ||
| import { LoginData, Message } from "@repo/ui/src/types/WebViewMessageTypes"; | ||
| import { stringifyJson } from "@repo/ui/src/utils/stringifyJson"; | ||
|
|
||
| export const getAuthData = async () => { | ||
| const [accessToken, userStr] = await Promise.all([AsyncStorage.getItem("accessToken"), AsyncStorage.getItem("user")]); | ||
|
|
||
| return { | ||
| accessToken, | ||
| user: userStr ? JSON.parse(userStr) : null, | ||
| }; | ||
| }; | ||
miraclee1226 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export const setAuthData = async ({ accessToken, user }: LoginData) => { | ||
| await Promise.all([ | ||
| AsyncStorage.setItem("accessToken", accessToken), | ||
| AsyncStorage.setItem("user", stringifyJson(user)), | ||
| ]); | ||
| }; | ||
|
|
||
| export const clearAuthData = async () => { | ||
| await Promise.all([AsyncStorage.removeItem("accessToken"), AsyncStorage.removeItem("user")]); | ||
| }; | ||
|
|
||
| export const handleAuthStorage = async ({ type, data }: Message<LoginData>) => { | ||
| switch (type) { | ||
| case WEBVIEW_MESSAGE_TYPES.SIGN_IN_SUCCESS: { | ||
| const { user, accessToken } = data; | ||
| await setAuthData({ accessToken, user }); | ||
| break; | ||
| } | ||
| case WEBVIEW_MESSAGE_TYPES.SIGN_OUT_SUCCESS: | ||
| await clearAuthData(); | ||
| break; | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
apps/mobile/utils/getBaseUrl.ts → apps/mobile/utils/getWebViewApiUrl.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import { Platform } from "react-native"; | ||
|
|
||
| export const getBaseUrl = () => { | ||
| export const getWebViewApiUrl = () => { | ||
| if (Platform.OS === "ios") return process.env.EXPO_PUBLIC_API_URL_IOS; | ||
| if (Platform.OS === "android") return process.env.EXPO_PUBLIC_API_URL_ANDROID; | ||
| }; |
2 changes: 1 addition & 1 deletion
2
apps/mobile/utils/parseMessageEvent.ts → apps/mobile/utils/parseMessage.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { WebViewMessageEvent } from "react-native-webview"; | ||
|
|
||
| export const parseMessageEvent = (e: WebViewMessageEvent) => { | ||
| export const parseMessage = (e: WebViewMessageEvent) => { | ||
| return JSON.parse(e.nativeEvent.data); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { stringifyJson } from "@repo/ui/src/utils/stringifyJson"; | ||
| import { Message } from "@ui/src/types/WebViewMessageTypes"; | ||
| import { RefObject } from "react"; | ||
| import WebView from "react-native-webview"; | ||
|
|
||
| export interface SendMessage<T> extends Message<T> { | ||
| webViewRef: RefObject<WebView<object>>; | ||
| } | ||
|
|
||
| export const sendMessageToWeb = <T>({ webViewRef, type, data }: SendMessage<T>) => { | ||
| if (webViewRef.current) { | ||
| webViewRef.current.postMessage(stringifyJson({ type, data })); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { WEBVIEW_MESSAGE_TYPES } from "@repo/constants"; | ||
| import { RefObject } from "react"; | ||
| import { Platform } from "react-native"; | ||
| import WebView from "react-native-webview"; | ||
| import { WebViewProgressEvent } from "react-native-webview/lib/WebViewTypes"; | ||
|
|
||
| import { getAuthData } from "@/store/authStorage"; | ||
|
|
||
| import { sendMessageToWeb } from "./sendMessageToWeb"; | ||
|
|
||
| interface WebViewLoadHandler { | ||
| event: "onLoadProgress" | "onLoad"; | ||
| handler: (e: WebViewProgressEvent) => void; | ||
| } | ||
|
|
||
| const sendAuthData = async (webViewRef: RefObject<WebView<object>>) => { | ||
| const authData = await getAuthData(); | ||
|
|
||
| sendMessageToWeb({ | ||
| webViewRef, | ||
| type: WEBVIEW_MESSAGE_TYPES.AUTO_LOGIN, | ||
| data: authData, | ||
| }); | ||
| }; | ||
miraclee1226 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export const webViewLoadHandler = (webViewRef: RefObject<WebView<object>>): WebViewLoadHandler => { | ||
| if (Platform.OS === "ios") { | ||
| return { | ||
| event: "onLoadProgress", | ||
| handler: (e: WebViewProgressEvent) => { | ||
| const progress = e.nativeEvent.progress; | ||
|
|
||
| if (progress === 1) { | ||
| sendAuthData(webViewRef); | ||
| } | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| event: "onLoad", | ||
| handler: () => sendAuthData(webViewRef), | ||
| }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
miraclee1226 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { detectWebView } from "@/lib/bridge/detectWebView"; | ||
|
|
||
| export const getWebApiUrl = (): string | undefined => { | ||
| const { isAndroid, isIOS } = detectWebView(); | ||
|
|
||
| if (isAndroid) return process.env.NEXT_PUBLIC_ANDROID_API_URL; | ||
| if (isIOS) return process.env.NEXT_PUBLIC_IOS_API_URL; | ||
|
|
||
| return process.env.NEXT_PUBLIC_API_URL; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.