-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: user management view #9
Conversation
typeWolffo
commented
Jul 22, 2024
9547281
to
b09d11c
Compare
return useMutation({ | ||
mutationFn: async (options: LoginUserOptions) => { | ||
const response = await ApiClient.auth.authControllerLogin(options.data); | ||
|
||
if (!response) throw new Error("Invalid username or password"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@k1eu any ideas on how to move to onError
in this case?
the problem is in return response.data
, where the response is undefined, even if the ‘real’ response has an error message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should actually go to onError if you throw or the ApiCLient throws here 🤔
onSuccess: (data) => { | ||
setCurrentUser(data.data); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add a /current-user endpoint and treat useCurrentUser() - as react query hook for that with longer revalidate time, then in the
const { setCurrentUser, currentUser } = useAuthStore(); | ||
if (!currentUser) { | ||
throw new Error("User is not logged in"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd remove that, user shouldn't be able to get into that action ui wise, and if SOMEHOW he gets there - he should be blocked on BE roles
const { currentUser } = useAuthStore(); | ||
if (!currentUser) { | ||
throw new Error("User is not logged in"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd remove this, if user is not logged in backend will return 401
|
||
type LoginUserOptions = { | ||
data: LoginBody; | ||
}; | ||
|
||
export function useLoginUser() { | ||
const { setLoggedIn } = useAuthStore(); | ||
const setLoggedIn = useAuthStore.getState().setLoggedIn; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change ? Didn't you want to add selector here?
return useQuery(currentUserQueryOptions); | ||
} | ||
|
||
export function useSafeCurrentUser() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useCurrentUserSuspense let's make it clear for what it is
@@ -97,4 +99,16 @@ export class AuthController { | |||
|
|||
return null; | |||
} | |||
|
|||
@Get("me") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/current-user
ApiClient.instance.interceptors.response.use( | ||
(response) => response, | ||
async (error) => { | ||
const isLoggedIn = useAuthStore.getState().isLoggedIn; | ||
const originalRequest = error.config; | ||
|
||
if ( | ||
some(bypassRefreshEndpoints, (endpoint) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think even without it login won't do the refresh token since user is logged out and we check it right? :D
) { | ||
return Promise.reject(error); | ||
} | ||
|
||
if (error.response?.status === 401 && !originalRequest._retry) { | ||
originalRequest._retry = true; | ||
if (!isLoggedIn) return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that can be moved up from this if so it exits to normal flow quicker
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall cool, could you add also Query Client cache purge on logout?
@@ -51,6 +51,15 @@ export type LogoutResponse = null; | |||
|
|||
export type RefreshTokensResponse = null; | |||
|
|||
export interface MeResponse { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please regenerate this file so it's cucrent user response
export function useCurrentUserSuspense() { | ||
const { data, ...rest } = useSuspenseQuery(currentUserQueryOptions); | ||
|
||
return { data: data?.data, ...rest }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In suspense query the data shouldn't be optional I guess