Skip to content
This repository has been archived by the owner on May 7, 2023. It is now read-only.

Commit

Permalink
Add refresh token senario on auth (#516)
Browse files Browse the repository at this point in the history
* Add datatype to schema

* Add refresh token senario on auth

* Add [ErrorBoundary] and `getIdToken` in `fetchGraphql`

* Impl `getIdToken` api
  • Loading branch information
hyochan authored Dec 25, 2021
1 parent 436fd86 commit 347950a
Show file tree
Hide file tree
Showing 17 changed files with 508 additions and 42 deletions.
1 change: 1 addition & 0 deletions client/assets/langs/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"TAKE_A_PICTURE": "Take a picture",
"TERMS_FOR_AGREEMENT": "Terms for agreement",
"THUMBNAIL_ERROR": "No Video Preview",
"RELOAD": "Reload",
"TRY_AGAIN": "Try again",
"UNBAN_USER": "Unban User",
"UNBAN_USER_TEXT": "Do you wan to unban current user? If then, you will start to see and receive messages from the user.",
Expand Down
1 change: 1 addition & 0 deletions client/assets/langs/ko.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"TAKE_A_PICTURE": "촬영하기",
"TERMS_FOR_AGREEMENT": "이용약관",
"THUMBNAIL_ERROR": "미리보기 실패",
"RELOAD": "새로고침",
"TRY_AGAIN": "다시 시도",
"UNBAN_USER": "차단 해제",
"UNBAN_USER_TEXT": "선택한 유저의 차단을 해지하시겠습니까? 앞으로 해당 유저의 모든 메시지를 보거나 받을 수 있습니다.",
Expand Down
1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"react": "17.0.2",
"react-native": "0.64.3",
"react-native-appearance": "~0.3.4",
"react-native-error-boundary": "^1.1.12",
"react-native-gesture-handler": "~1.10.2",
"react-native-get-random-values": "~1.7.1",
"react-native-modalbox": "^2.0.2",
Expand Down
7 changes: 7 additions & 0 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Notifications from 'expo-notifications';
import * as SplashScreen from 'expo-splash-screen';

import FallbackComponent, {handleError} from './utils/error';
import React, {FC, ReactElement, ReactNode, useEffect} from 'react';
import {dark, light} from './theme';

Expand All @@ -12,6 +13,7 @@ import AsyncStorage from '@react-native-async-storage/async-storage';
import {AuthProvider} from './providers/AuthProvider';
import ComponentWrapper from './utils/ComponentWrapper';
import {DeviceProvider} from './providers/DeviceProvider';
import ErrorBoundary from 'react-native-error-boundary';
import Icons from './utils/Icons';
import {RelayEnvironmentProvider} from 'react-relay';
import RootNavigator from './components/navigations/RootStackNavigator';
Expand Down Expand Up @@ -99,6 +101,11 @@ const WrappedApp = new ComponentWrapper(RootNavigator)
.wrap(ActionSheetProviderWithChildren, {})
.wrap(AuthProvider, {})
.wrap(RelayEnvironmentProvider, {environment})
// @ts-ignore
.wrap(ErrorBoundary, {
onError: handleError,
FallbackComponent,
})
.wrap(DeviceProvider, {})
.wrap(SnackbarProvider, {})
.wrap(HackatalkThemeProvider, {})
Expand Down
45 changes: 45 additions & 0 deletions client/src/apis/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as Config from '../../config';

import AsyncStorage from '@react-native-async-storage/async-storage';

const {ROOT_URL} = Config;

type Result = {
token?: string;
message?: string;
};

export const getIdToken = async (
body?: Record<string, unknown>,
signal?: AbortController['signal'],
): Promise<string> => {
const token = (await AsyncStorage.getItem('token')) || '';

if (!token) {
return '';
}

const fetchOption = {
signal,
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: token,
},
body: JSON.stringify(body),
};

try {
const res: Response = await fetch(`${ROOT_URL}/get_id_token`, fetchOption);
const json: Result = await res.json();

if (json.token && json.token !== token) {
await AsyncStorage.setItem('token', token);
}

return json.token || '';
} catch (err: any) {
throw new Error(err);
}
};
4 changes: 2 additions & 2 deletions client/src/apis/sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export const sample = async (
const fetchOption = {
// signal,
method: 'POST',
headers: new Headers({
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
}),
},
body: JSON.stringify(body),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ const ChannelModalContent: FC<ModalContentProps> = ({
const {channelId} = leaveChannelModalState;

const mutationConfig = {
variables: {
channelId,
},
variables: {channelId},
updater: (store: RecordSourceSelectorProxy<{}>) => {
deleteChannelAndUpdate(store, channelId);
},
Expand Down
6 changes: 4 additions & 2 deletions client/src/relay/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Config from '../../config';

import AsyncStorage from '@react-native-async-storage/async-storage';
import {FetchFunction} from 'relay-runtime';
import {getIdToken} from '../apis/auth';

const {GRAPHQL_URL} = Config;

Expand All @@ -17,10 +17,12 @@ const fetchGraphQL: FetchFunction = async (
cacheConfig,
uploadables,
) => {
const token = await getIdToken();

const config: RequestProps = {
method: 'POST',
headers: {
Authorization: (await AsyncStorage.getItem('token')) || '',
Authorization: token,
Accept: 'application/json',
},
body: '',
Expand Down
58 changes: 58 additions & 0 deletions client/src/utils/error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {Button, Typography} from 'dooboo-ui';
import {DevSettings, View} from 'react-native';
import React, {ReactElement, useEffect} from 'react';

import AsyncStorage from '@react-native-async-storage/async-storage';
import {getString} from '../../STRINGS';
import styled from '@emotion/native';

const Container = styled.SafeAreaView`
margin: 0 24px;
flex: 1;
justify-content: center;
`;

export const handleError = (error: any, stack?: string): void => {
if (__DEV__) {
// eslint-disable-next-line no-console
console.error(stack, error);
}
};

interface Props {
error: Error;
resetError: Function;
}

export default function FallbackComponent({resetError}: Props): ReactElement {
useEffect(() => {
AsyncStorage.removeItem('token');
AsyncStorage.removeItem('push_token');

resetError();
}, [resetError]);

return (
<Container>
<>
<Typography.Heading1>{getString('ERROR_OCCURED')}</Typography.Heading1>
</>
<View style={{height: 32}} />
<Button
size="large"
onPress={() => resetError()}
text={getString('TRY_AGAIN')}
/>
<View style={{height: 12}} />
{__DEV__ ? (
<Button
style={{marginBottom: 32}}
size="large"
onPress={() => DevSettings.reload()}
text={getString('RELOAD')}
/>
) : null}
</Container>
);
}
5 changes: 5 additions & 0 deletions client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13034,6 +13034,11 @@ react-native-codegen@^0.0.6:
jscodeshift "^0.11.0"
nullthrows "^1.1.1"

react-native-error-boundary@^1.1.12:
version "1.1.12"
resolved "https://registry.yarnpkg.com/react-native-error-boundary/-/react-native-error-boundary-1.1.12.tgz#80773d8f52bf7354f9776b022a57081a7ecafb8c"
integrity sha512-YVM7DOY6TbCrtYc87CqABGntnxQRMWSkzcF6+tB9QBUs4sWOWIfeeN4yJGF2eYJAGVnaym5ys9GqcYDu8mjE2g==

react-native-gesture-handler@~1.10.2:
version "1.10.3"
resolved "https://registry.yarnpkg.com/react-native-gesture-handler/-/react-native-gesture-handler-1.10.3.tgz#942bbf2963bbf49fa79593600ee9d7b5dab3cfc0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
Warnings:
- You are about to alter the column `name` on the `Channel` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(255)`.
- You are about to alter the column `lastMessageId` on the `Channel` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(255)`.
- You are about to alter the column `token` on the `Notification` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(1024)`.
- You are about to alter the column `device` on the `Notification` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(255)`.
- You are about to alter the column `os` on the `Notification` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(255)`.
- You are about to alter the column `userId` on the `Notification` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(255)`.
- You are about to alter the column `socialId` on the `Profile` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(255)`.
- You are about to alter the column `value` on the `Reaction` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(4)`.
- You are about to alter the column `userId` on the `Reaction` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(255)`.
- You are about to alter the column `messageId` on the `Reaction` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(255)`.
- You are about to alter the column `senderId` on the `Reply` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(255)`.
- You are about to alter the column `messageId` on the `Reply` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(255)`.
- You are about to alter the column `password` on the `User` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(255)`.
- You are about to alter the column `name` on the `User` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(255)`.
- You are about to alter the column `nickname` on the `User` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(255)`.
- You are about to alter the column `thumbURL` on the `User` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(1024)`.
- You are about to alter the column `photoURL` on the `User` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(1024)`.
- You are about to alter the column `phone` on the `User` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(255)`.
*/
-- DropForeignKey
ALTER TABLE "BlockedUser" DROP CONSTRAINT "BlockedUser_blockedUserId_fkey";

-- DropForeignKey
ALTER TABLE "BlockedUser" DROP CONSTRAINT "BlockedUser_userId_fkey";

-- DropForeignKey
ALTER TABLE "Friend" DROP CONSTRAINT "Friend_friendId_fkey";

-- DropForeignKey
ALTER TABLE "Friend" DROP CONSTRAINT "Friend_userId_fkey";

-- DropForeignKey
ALTER TABLE "Membership" DROP CONSTRAINT "Membership_channelId_fkey";

-- DropForeignKey
ALTER TABLE "Membership" DROP CONSTRAINT "Membership_userId_fkey";

-- DropForeignKey
ALTER TABLE "Message" DROP CONSTRAINT "Message_channelId_fkey";

-- DropForeignKey
ALTER TABLE "Message" DROP CONSTRAINT "Message_senderId_fkey";

-- DropForeignKey
ALTER TABLE "Notification" DROP CONSTRAINT "Notification_userId_fkey";

-- DropForeignKey
ALTER TABLE "Profile" DROP CONSTRAINT "Profile_userId_fkey";

-- DropForeignKey
ALTER TABLE "Reaction" DROP CONSTRAINT "Reaction_messageId_fkey";

-- DropForeignKey
ALTER TABLE "Reaction" DROP CONSTRAINT "Reaction_replyId_fkey";

-- DropForeignKey
ALTER TABLE "Reaction" DROP CONSTRAINT "Reaction_userId_fkey";

-- DropForeignKey
ALTER TABLE "Reply" DROP CONSTRAINT "Reply_messageId_fkey";

-- DropForeignKey
ALTER TABLE "Reply" DROP CONSTRAINT "Reply_senderId_fkey";

-- DropForeignKey
ALTER TABLE "Report" DROP CONSTRAINT "Report_reportedUserId_fkey";

-- DropForeignKey
ALTER TABLE "Report" DROP CONSTRAINT "Report_userId_fkey";

-- AlterTable
ALTER TABLE "Channel" ALTER COLUMN "name" SET DATA TYPE VARCHAR(255),
ALTER COLUMN "lastMessageId" SET DATA TYPE VARCHAR(255);

-- AlterTable
ALTER TABLE "Notification" ALTER COLUMN "token" SET DATA TYPE VARCHAR(1024),
ALTER COLUMN "device" SET DATA TYPE VARCHAR(255),
ALTER COLUMN "os" SET DATA TYPE VARCHAR(255),
ALTER COLUMN "userId" SET DATA TYPE VARCHAR(255);

-- AlterTable
ALTER TABLE "Profile" ADD COLUMN "refreshToken" VARCHAR(1024),
ALTER COLUMN "socialId" SET DATA TYPE VARCHAR(255);

-- AlterTable
ALTER TABLE "Reaction" ALTER COLUMN "value" SET DATA TYPE VARCHAR(4),
ALTER COLUMN "userId" SET DATA TYPE VARCHAR(255),
ALTER COLUMN "messageId" SET DATA TYPE VARCHAR(255);

-- AlterTable
ALTER TABLE "Reply" ALTER COLUMN "senderId" SET DATA TYPE VARCHAR(255),
ALTER COLUMN "messageId" SET DATA TYPE VARCHAR(255);

-- AlterTable
ALTER TABLE "User" ALTER COLUMN "password" SET DATA TYPE VARCHAR(255),
ALTER COLUMN "name" SET DATA TYPE VARCHAR(255),
ALTER COLUMN "nickname" SET DATA TYPE VARCHAR(255),
ALTER COLUMN "thumbURL" SET DATA TYPE VARCHAR(1024),
ALTER COLUMN "photoURL" SET DATA TYPE VARCHAR(1024),
ALTER COLUMN "phone" SET DATA TYPE VARCHAR(255);

-- AddForeignKey
ALTER TABLE "Notification" ADD CONSTRAINT "Notification_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Profile" ADD CONSTRAINT "Profile_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Friend" ADD CONSTRAINT "Friend_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Friend" ADD CONSTRAINT "Friend_friendId_fkey" FOREIGN KEY ("friendId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BlockedUser" ADD CONSTRAINT "BlockedUser_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BlockedUser" ADD CONSTRAINT "BlockedUser_blockedUserId_fkey" FOREIGN KEY ("blockedUserId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Report" ADD CONSTRAINT "Report_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Report" ADD CONSTRAINT "Report_reportedUserId_fkey" FOREIGN KEY ("reportedUserId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Membership" ADD CONSTRAINT "Membership_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Membership" ADD CONSTRAINT "Membership_channelId_fkey" FOREIGN KEY ("channelId") REFERENCES "Channel"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Message" ADD CONSTRAINT "Message_channelId_fkey" FOREIGN KEY ("channelId") REFERENCES "Channel"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Message" ADD CONSTRAINT "Message_senderId_fkey" FOREIGN KEY ("senderId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Reply" ADD CONSTRAINT "Reply_senderId_fkey" FOREIGN KEY ("senderId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Reply" ADD CONSTRAINT "Reply_messageId_fkey" FOREIGN KEY ("messageId") REFERENCES "Message"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Reaction" ADD CONSTRAINT "Reaction_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Reaction" ADD CONSTRAINT "Reaction_messageId_fkey" FOREIGN KEY ("messageId") REFERENCES "Message"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Reaction" ADD CONSTRAINT "Reaction_replyId_fkey" FOREIGN KEY ("replyId") REFERENCES "Reply"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- RenameIndex
ALTER INDEX "Membership.updatedAt_index" RENAME TO "Membership_updatedAt_idx";

-- RenameIndex
ALTER INDEX "Notification.token_unique" RENAME TO "Notification_token_key";

-- RenameIndex
ALTER INDEX "Profile.userId_unique" RENAME TO "Profile_userId_key";

-- RenameIndex
ALTER INDEX "User.email_unique" RENAME TO "User_email_key";
Loading

0 comments on commit 347950a

Please sign in to comment.