Replies: 5 comments 3 replies
-
Hey!
You can see my pet project nukeapp with But It's okay to move logic with access token to I think you can use any of these approaches.
You can see PR noveogroup-amorgunov/nukeapp#37 (not merged yet), where
Auth store can be considered an infrastructure service (not domain specific feature) that is needed to work with the
You can use |
Beta Was this translation helpful? Give feedback.
-
Hey, thanks for your response. Speaking of attaching token to be used inside of shared api, i also have a refresh functionality , which uses auth service directly, and store actions. And i also refactored auth to @/entities/session Do u have ideas how i can handle it? Here is an example: const {
tokens: { refreshToken }
} = getSessionStore()
const tokens = await sessionService.getTokens({ refreshToken })
sessionActions.setTokens(tokens)
return axiosInstance(originalRequest) |
Beta Was this translation helpful? Give feedback.
-
here is full code (with incorrect imports) import axios, { AxiosError } from 'axios'
import {
getSessionStore,
sessionActions,
sessionService
} from '@/entities/session'
import { env } from '../config'
import { router } from '../lib'
export const axiosInstance = axios.create({
baseURL: env.VITE_API_BASE_URL
})
axiosInstance.interceptors.request.use(config => {
const {
tokens: { accessToken }
} = getSessionStore()
if (config?.headers && accessToken) {
config.headers.Authorization = `Bearer ${accessToken}`
}
return config
})
axiosInstance.interceptors.response.use(
r => r,
async error => {
const originalRequest = error.config
if (
error?.response?.status === 401 &&
!originalRequest._retry &&
!error.config.skipAuthRefresh
) {
originalRequest._retry = true
try {
const {
tokens: { refreshToken }
} = getSessionStore()
const tokens = await sessionService.getTokens({ refreshToken })
sessionActions.setTokens(tokens)
return axiosInstance(originalRequest)
} catch (e) {
if (
e instanceof AxiosError &&
e.response?.data?.message === 'ERR_JWT_EXPIRED'
) {
sessionActions.logout()
return router.navigate({ to: '/' })
}
throw e
}
}
throw error
}
) async getTokens(data: RefreshTokenDto) {
const refreshTokenDto = parse(RefreshTokenDtoSchema, data)
const response = await axiosInstance.post(
sessionApiEndpoints.tokens,
refreshTokenDto
)
const parsedData = parse(TokensDtoSchema, response.data)
return parsedData
}, i'm using stan-js for store management, and i'm not really sure if they have this type of functionality you showed above. I''l take a look, but i want getTokens function to be a part of sessionService. |
Beta Was this translation helpful? Give feedback.
-
Maybe i can do something like this in the root? const sessionApi = {
setTokens: (tokens) => sessionActions.setTokens(tokens),
logout: () => sessionActions.logout(),
getTokens: (params) => sessionService.getTokens(params)
}
setSessionApi(sessionApi) The same way as attaching token |
Beta Was this translation helpful? Give feedback.
-
Hey, I've done something like this: // shared/api/apiMemoryStorage
type Tokens = {
accessToken: string
refreshToken: string
}
type ApiMemoryStorage = {
accessToken: string
refreshToken: string
refreshTokens: (data: Pick<Tokens, 'refreshToken'>) => Promise<Tokens>
updateTokens: (data: Tokens) => void
logout: () => void
}
let __internalMemoryStorage: () => ApiMemoryStorage
export const attachInternalApiMemoryStorage = (data: ApiMemoryStorage) => {
__internalMemoryStorage = () => data
}
export const getApiAccessToken = () => {
const { accessToken } = __internalMemoryStorage()
return accessToken
}
export const getApiRefreshToken = () => {
const { refreshToken } = __internalMemoryStorage()
return refreshToken
}
export const getRefreshedTokens = (data: Pick<Tokens, 'refreshToken'>) => {
const { refreshTokens } = __internalMemoryStorage()
return refreshTokens(data)
}
export const setTokens = (data: Tokens) => {
const { updateTokens } = __internalMemoryStorage()
return updateTokens(data)
}
export const logUserOut = () => {
const { logout } = __internalMemoryStorage()
return logout()
} And i'm giving the needed data in the app entry: const { tokens, setTokens, logout } = useSessionStore()
attachInternalApiMemoryStorage({
accessToken: tokens.accessToken,
refreshToken: tokens.refreshToken,
refreshTokens: sessionService.getTokens,
updateTokens: setTokens,
logout
}) Is it fine? What do you think? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Can auth be an entity?
I have auth service -
It's located in shared/api folder, because my axios instance depends on auth service to retrieve the token. So it's impossible to move auth into entities and then import into shared. However, i also have auth store, and it's located to shared, but is it a domain specific feature? I struggle to handle this correctly due to complexity of my project.
My store also uses userService to get current user, so i need to move user api to shared folder again, and same goes with other entities, so my shared folder holds domain specific apis, constants, etc. I'm lost in trying to find the right way to handle that.
And also if i moves user and auth to entites, store needs AuthDto types, so i would need to make cross imports just for one type?
Beta Was this translation helpful? Give feedback.
All reactions