From e398e6c373d7e5d4803545fd235a969aded41006 Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Thu, 24 Nov 2022 17:59:37 +0300 Subject: [PATCH] getSession --- codegen.yml | 2 +- src/context/session.tsx | 10 +++++----- src/graphql/mutation/my-session.ts | 4 ++-- src/graphql/privateGraphQLClient.ts | 1 + src/graphql/types.gen.ts | 5 +---- src/pages/api/upload.ts | 2 +- src/utils/apiClient.ts | 6 +++--- 7 files changed, 14 insertions(+), 16 deletions(-) diff --git a/codegen.yml b/codegen.yml index 2219d008..44d1e541 100644 --- a/codegen.yml +++ b/codegen.yml @@ -1,5 +1,5 @@ overwrite: true -schema: 'https://testapi.discours.io' +schema: 'http://localhost:8080' generates: src/graphql/introspec.gen.ts: plugins: diff --git a/src/context/session.tsx b/src/context/session.tsx index 47a51e76..966d8d6f 100644 --- a/src/context/session.tsx +++ b/src/context/session.tsx @@ -8,7 +8,7 @@ type SessionContextType = { session: InitializedResource isAuthenticated: Accessor actions: { - refreshSession: () => AuthResult | Promise + getSession: () => AuthResult | Promise signIn: ({ email, password }: { email: string; password: string }) => Promise signOut: () => Promise confirmEmail: (token: string) => Promise @@ -17,7 +17,7 @@ type SessionContextType = { const SessionContext = createContext() -const refreshSession = async (): Promise => { +const getSession = async (): Promise => { try { const authResult = await apiClient.getSession() if (!authResult) { @@ -37,7 +37,7 @@ export function useSession() { } export const SessionProvider = (props: { children: JSX.Element }) => { - const [session, { refetch: refetchRefreshSession, mutate }] = createResource(refreshSession, { + const [session, { refetch: refetchSession, mutate }] = createResource(getSession, { ssrLoadFrom: 'initial', initialValue: null }) @@ -65,7 +65,7 @@ export const SessionProvider = (props: { children: JSX.Element }) => { } const actions = { - refreshSession: refetchRefreshSession, + getSession: refetchSession, signIn, signOut, confirmEmail @@ -74,7 +74,7 @@ export const SessionProvider = (props: { children: JSX.Element }) => { const value: SessionContextType = { session, isAuthenticated, actions } onMount(() => { - refetchRefreshSession() + refetchSession() }) return {props.children} diff --git a/src/graphql/mutation/my-session.ts b/src/graphql/mutation/my-session.ts index aba14e7d..0a884374 100644 --- a/src/graphql/mutation/my-session.ts +++ b/src/graphql/mutation/my-session.ts @@ -1,8 +1,8 @@ import { gql } from '@urql/core' export default gql` - mutation RefreshSessionMutation { - refreshSession { + mutation GetSessionMutation { + getSession { error token user { diff --git a/src/graphql/privateGraphQLClient.ts b/src/graphql/privateGraphQLClient.ts index c6863898..1c4761ca 100644 --- a/src/graphql/privateGraphQLClient.ts +++ b/src/graphql/privateGraphQLClient.ts @@ -32,6 +32,7 @@ const options: ClientOptions = { // меняем через setToken, например при получении значения с сервера // скорее всего придумаем что-нибудь получше со временем const token = localStorage.getItem(TOKEN_LOCAL_STORAGE_KEY) + if (token === null) alert('token is null') const headers = { Authorization: token } return { headers } }, diff --git a/src/graphql/types.gen.ts b/src/graphql/types.gen.ts index 90e62888..5b255416 100644 --- a/src/graphql/types.gen.ts +++ b/src/graphql/types.gen.ts @@ -176,11 +176,11 @@ export type Mutation = { deleteShout: Result destroyTopic: Result follow: Result + getSession: AuthResult inviteAuthor: Result inviteChat: Result markAsRead: Result rateUser: Result - refreshSession: AuthResult registerUser: AuthResult removeAuthor: Result sendLink: Result @@ -661,10 +661,7 @@ export type TopicInput = { export type TopicStat = { authors: Scalars['Int'] - commented?: Maybe followers: Scalars['Int'] - rating?: Maybe - reacted: Scalars['Int'] shouts: Scalars['Int'] } diff --git a/src/pages/api/upload.ts b/src/pages/api/upload.ts index e8d3b571..3113894b 100644 --- a/src/pages/api/upload.ts +++ b/src/pages/api/upload.ts @@ -5,7 +5,7 @@ import { createPresignedPost } from '@aws-sdk/s3-presigned-post' export default async function handler(req, res) { const s3Client = new S3Client({ - region: process.env.S3_REGION || 'eu-east-1', + region: process.env.S3_REGION || 'eu-west-1', credentials: { accessKeyId: process.env.S3_ACCESS_KEY, secretAccessKey: process.env.S3_SECRET_KEY diff --git a/src/utils/apiClient.ts b/src/utils/apiClient.ts index 152ce280..bacc1d4f 100644 --- a/src/utils/apiClient.ts +++ b/src/utils/apiClient.ts @@ -184,11 +184,11 @@ export const apiClient = { throw new ApiError('unknown', response.error.message) } - if (response.data?.refreshSession?.error) { - throw new ApiError('unknown', response.data.refreshSession.error) + if (response.data?.getSession?.error) { + throw new ApiError('unknown', response.data.getSession.error) } - return response.data.refreshSession + return response.data.getSession }, getAllTopics: async () => { const response = await publicGraphQLClient.query(topicsAll, {}).toPromise()