Skip to content

Commit

Permalink
Add AccountInfo state/queries
Browse files Browse the repository at this point in the history
  • Loading branch information
dauglyon committed Dec 11, 2024
1 parent baab8bd commit b9c07d4
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 133 deletions.
242 changes: 132 additions & 110 deletions src/common/api/authService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ interface AuthParams {
getMe: {
token: string;
};
setMe: {
token: string;
meUpdate: Pick<Me, 'display' | 'email'>;
};
getUsers: {
token: string;
users: string[];
Expand All @@ -45,6 +49,7 @@ interface AuthParams {

interface AuthResults {
getMe: Me;
setMe: void;
getUsers: Record<string, string>;
searchUsers: Record<string, string>;
getLoginChoice: {
Expand Down Expand Up @@ -100,21 +105,35 @@ interface AuthResults {
}

// Auth does not use JSONRpc, so we use queryFn to make custom queries
export const authApi = baseApi.injectEndpoints({
endpoints: (builder) => ({
authFromToken: builder.query<TokenResponse, string>({
query: (token) =>
authService({
url: '/api/V2/token',
method: 'GET',
headers: {
Authorization: token || '',
},
}),
}),
getMe: builder.query<AuthResults['getMe'], AuthParams['getMe']>({
query: ({ token }) => {
/* I want to do
export const authApi = baseApi
.enhanceEndpoints({ addTagTypes: ['AccountMe'] })
.injectEndpoints({
endpoints: (builder) => ({
authFromToken: builder.query<TokenResponse, string>({
query: (token) =>
authService({
url: '/api/V2/token',
method: 'GET',
headers: {
Authorization: token || '',
},
}),
}),
getMe: builder.query<AuthResults['getMe'], AuthParams['getMe']>({
query: ({ token }) => {
return authService({
headers: {
Authorization: token,
},
method: 'GET',
url: '/api/V2/me',
});
},
providesTags: ['AccountMe'],
}),
setMe: builder.mutation<AuthResults['setMe'], AuthParams['setMe']>({
query: ({ token, meUpdate }) => {
/* I want to do
const token = store.getState().auth.token;
but authSlice imports revokeToken defined here,
so this becomes a circular depenency.
Expand All @@ -123,106 +142,109 @@ export const authApi = baseApi.injectEndpoints({
type annotation and is referenced directly or indirectly in its own
initializer.
*/
return authService({
headers: {
Authorization: token,
},
method: 'GET',
url: '/api/V2/me',
});
},
}),
getUsers: builder.query<AuthResults['getUsers'], AuthParams['getUsers']>({
query: ({ token, users }) =>
authService({
headers: {
Authorization: token,
},
method: 'GET',
params: { list: users.join(',') },
url: '/api/V2/users',
}),
}),
searchUsers: builder.query<
AuthResults['searchUsers'],
AuthParams['searchUsers']
>({
query: ({ search, token }) =>
authService({
headers: {
Authorization: token,
},
method: 'GET',
url: `/api/V2/users/search/${search}`,
}),
}),
revokeToken: builder.mutation<boolean, string>({
query: (tokenId) =>
authService({
url: encode`/tokens/revoke/${tokenId}`,
method: 'DELETE',
}),
return authService({
headers: {
Authorization: token,
},
method: 'PUT',
url: '/me',
body: meUpdate,
});
},
invalidatesTags: ['AccountMe'],
}),
getUsers: builder.query<AuthResults['getUsers'], AuthParams['getUsers']>({
query: ({ token, users }) =>
authService({
headers: {
Authorization: token,
},
method: 'GET',
params: { list: users.join(',') },
url: '/api/V2/users',
}),
}),
searchUsers: builder.query<
AuthResults['searchUsers'],
AuthParams['searchUsers']
>({
query: ({ search, token }) =>
authService({
headers: {
Authorization: token,
},
method: 'GET',
url: `/api/V2/users/search/${search}`,
}),
}),
revokeToken: builder.mutation<boolean, string>({
query: (tokenId) =>
authService({
url: encode`/tokens/revoke/${tokenId}`,
method: 'DELETE',
}),
}),
getLoginChoice: builder.query<
AuthResults['getLoginChoice'],
AuthParams['getLoginChoice']
>({
query: () =>
// MUST have an in-process-login-token cookie
authService({
headers: {
accept: 'application/json',
},
method: 'GET',
url: '/login/choice',
}),
}),
postLoginPick: builder.mutation<
AuthResults['postLoginPick'],
AuthParams['postLoginPick']
>({
query: (pickedChoice) =>
authService({
url: encode`/login/pick`,
body: pickedChoice,
method: 'POST',
}),
}),
loginUsernameSuggest: builder.query<
AuthResults['loginUsernameSuggest'],
AuthParams['loginUsernameSuggest']
>({
query: (username) =>
// MUST have an in-process-login-token cookie
authService({
headers: {
accept: 'application/json',
},
method: 'GET',
url: `/login/suggestname/${encodeURIComponent(username)}`,
}),
}),
loginCreate: builder.mutation<
AuthResults['loginCreate'],
AuthParams['loginCreate']
>({
query: (params) =>
// MUST have an in-process-login-token cookie
authService({
headers: {
accept: 'application/json',
},
method: 'POST',
body: params,
url: `/login/create/`,
}),
}),
}),
getLoginChoice: builder.query<
AuthResults['getLoginChoice'],
AuthParams['getLoginChoice']
>({
query: () =>
// MUST have an in-process-login-token cookie
authService({
headers: {
accept: 'application/json',
},
method: 'GET',
url: '/login/choice',
}),
}),
postLoginPick: builder.mutation<
AuthResults['postLoginPick'],
AuthParams['postLoginPick']
>({
query: (pickedChoice) =>
authService({
url: encode`/login/pick`,
body: pickedChoice,
method: 'POST',
}),
}),
loginUsernameSuggest: builder.query<
AuthResults['loginUsernameSuggest'],
AuthParams['loginUsernameSuggest']
>({
query: (username) =>
// MUST have an in-process-login-token cookie
authService({
headers: {
accept: 'application/json',
},
method: 'GET',
url: `/login/suggestname/${encodeURIComponent(username)}`,
}),
}),
loginCreate: builder.mutation<
AuthResults['loginCreate'],
AuthParams['loginCreate']
>({
query: (params) =>
// MUST have an in-process-login-token cookie
authService({
headers: {
accept: 'application/json',
},
method: 'POST',
body: params,
url: `/login/create/`,
}),
}),
}),
});
});

export const {
authFromToken,
getMe,
setMe,
getUsers,
searchUsers,
revokeToken,
Expand Down
3 changes: 3 additions & 0 deletions src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

export const isInsideIframe: (w: Window) => boolean = (w) =>
Boolean(w) && Boolean(w.top) && w !== w.top;

export const emailRegex =
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
Loading

0 comments on commit b9c07d4

Please sign in to comment.