session api upgrade

This commit is contained in:
tonyrewin 2022-11-25 19:24:36 +03:00
parent 7224b87797
commit ae1203f5b9
4 changed files with 10 additions and 10 deletions

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

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

View File

@ -186,7 +186,7 @@ export type Mutation = {
inviteChat: Result
markAsRead: Result
rateUser: Result
refreshSession: AuthResult
getSession: AuthResult
registerUser: AuthResult
removeAuthor: Result
sendLink: Result

View File

@ -179,11 +179,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()