session api upgrade
This commit is contained in:
parent
7224b87797
commit
ae1203f5b9
|
@ -8,7 +8,7 @@ type SessionContextType = {
|
||||||
session: InitializedResource<AuthResult>
|
session: InitializedResource<AuthResult>
|
||||||
isAuthenticated: Accessor<boolean>
|
isAuthenticated: Accessor<boolean>
|
||||||
actions: {
|
actions: {
|
||||||
refreshSession: () => AuthResult | Promise<AuthResult>
|
getSession: () => AuthResult | Promise<AuthResult>
|
||||||
signIn: ({ email, password }: { email: string; password: string }) => Promise<void>
|
signIn: ({ email, password }: { email: string; password: string }) => Promise<void>
|
||||||
signOut: () => Promise<void>
|
signOut: () => Promise<void>
|
||||||
confirmEmail: (token: string) => Promise<void>
|
confirmEmail: (token: string) => Promise<void>
|
||||||
|
@ -17,7 +17,7 @@ type SessionContextType = {
|
||||||
|
|
||||||
const SessionContext = createContext<SessionContextType>()
|
const SessionContext = createContext<SessionContextType>()
|
||||||
|
|
||||||
const refreshSession = async (): Promise<AuthResult> => {
|
const getSession = async (): Promise<AuthResult> => {
|
||||||
try {
|
try {
|
||||||
const authResult = await apiClient.getSession()
|
const authResult = await apiClient.getSession()
|
||||||
if (!authResult) {
|
if (!authResult) {
|
||||||
|
@ -37,7 +37,7 @@ export function useSession() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SessionProvider = (props: { children: JSX.Element }) => {
|
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',
|
ssrLoadFrom: 'initial',
|
||||||
initialValue: null
|
initialValue: null
|
||||||
})
|
})
|
||||||
|
@ -65,7 +65,7 @@ export const SessionProvider = (props: { children: JSX.Element }) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const actions = {
|
const actions = {
|
||||||
refreshSession: refetchRefreshSession,
|
getSession: refetchSession,
|
||||||
signIn,
|
signIn,
|
||||||
signOut,
|
signOut,
|
||||||
confirmEmail
|
confirmEmail
|
||||||
|
@ -74,7 +74,7 @@ export const SessionProvider = (props: { children: JSX.Element }) => {
|
||||||
const value: SessionContextType = { session, isAuthenticated, actions }
|
const value: SessionContextType = { session, isAuthenticated, actions }
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
refetchRefreshSession()
|
refetchSession()
|
||||||
})
|
})
|
||||||
|
|
||||||
return <SessionContext.Provider value={value}>{props.children}</SessionContext.Provider>
|
return <SessionContext.Provider value={value}>{props.children}</SessionContext.Provider>
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { gql } from '@urql/core'
|
||||||
|
|
||||||
export default gql`
|
export default gql`
|
||||||
mutation RefreshSessionMutation {
|
mutation RefreshSessionMutation {
|
||||||
refreshSession {
|
getSession {
|
||||||
error
|
error
|
||||||
token
|
token
|
||||||
user {
|
user {
|
||||||
|
|
|
@ -186,7 +186,7 @@ export type Mutation = {
|
||||||
inviteChat: Result
|
inviteChat: Result
|
||||||
markAsRead: Result
|
markAsRead: Result
|
||||||
rateUser: Result
|
rateUser: Result
|
||||||
refreshSession: AuthResult
|
getSession: AuthResult
|
||||||
registerUser: AuthResult
|
registerUser: AuthResult
|
||||||
removeAuthor: Result
|
removeAuthor: Result
|
||||||
sendLink: Result
|
sendLink: Result
|
||||||
|
|
|
@ -179,11 +179,11 @@ export const apiClient = {
|
||||||
throw new ApiError('unknown', response.error.message)
|
throw new ApiError('unknown', response.error.message)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.data?.refreshSession?.error) {
|
if (response.data?.getSession?.error) {
|
||||||
throw new ApiError('unknown', response.data.refreshSession.error)
|
throw new ApiError('unknown', response.data.getSession.error)
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.data.refreshSession
|
return response.data.getSession
|
||||||
},
|
},
|
||||||
getAllTopics: async () => {
|
getAllTopics: async () => {
|
||||||
const response = await publicGraphQLClient.query(topicsAll, {}).toPromise()
|
const response = await publicGraphQLClient.query(topicsAll, {}).toPromise()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user