getSession

This commit is contained in:
tonyrewin 2022-11-24 17:59:37 +03:00
parent 6bb9e2a674
commit e398e6c373
7 changed files with 14 additions and 16 deletions

View File

@ -1,5 +1,5 @@
overwrite: true
schema: 'https://testapi.discours.io'
schema: 'http://localhost:8080'
generates:
src/graphql/introspec.gen.ts:
plugins:

View File

@ -8,7 +8,7 @@ type SessionContextType = {
session: InitializedResource<AuthResult>
isAuthenticated: Accessor<boolean>
actions: {
refreshSession: () => AuthResult | Promise<AuthResult>
getSession: () => AuthResult | Promise<AuthResult>
signIn: ({ email, password }: { email: string; password: string }) => Promise<void>
signOut: () => Promise<void>
confirmEmail: (token: string) => Promise<void>
@ -17,7 +17,7 @@ type SessionContextType = {
const SessionContext = createContext<SessionContextType>()
const refreshSession = async (): Promise<AuthResult> => {
const getSession = async (): Promise<AuthResult> => {
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<AuthResult>(refreshSession, {
const [session, { refetch: refetchSession, mutate }] = createResource<AuthResult>(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 <SessionContext.Provider value={value}>{props.children}</SessionContext.Provider>

View File

@ -1,8 +1,8 @@
import { gql } from '@urql/core'
export default gql`
mutation RefreshSessionMutation {
refreshSession {
mutation GetSessionMutation {
getSession {
error
token
user {

View File

@ -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 }
},

View File

@ -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<Scalars['Int']>
followers: Scalars['Int']
rating?: Maybe<Scalars['Int']>
reacted: Scalars['Int']
shouts: Scalars['Int']
}

View File

@ -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

View File

@ -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()