Skip to content

Commit 2bcbe98

Browse files
committed
chore: merge ui changes with new baseUrl function
1 parent 2b5125e commit 2bcbe98

File tree

4 files changed

+31
-66
lines changed

4 files changed

+31
-66
lines changed

src/ui/services/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface AxiosConfig {
1717
export const getUserInfo = async (): Promise<PublicUser | null> => {
1818
try {
1919
const baseUrl = await getBaseUrl();
20-
const response = await fetch(`${baseUrl}/api/auth/me`, {
20+
const response = await fetch(`${baseUrl}/api/auth/profile`, {
2121
credentials: 'include', // Sends cookies
2222
});
2323
if (!response.ok) throw new Error(`Failed to fetch user info: ${response.statusText}`);

src/ui/services/user.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -87,30 +87,4 @@ const updateUser = async (
8787
}
8888
};
8989

90-
const getUserLoggedIn = async (
91-
setIsLoading: SetStateCallback<boolean>,
92-
setIsAdmin: SetStateCallback<boolean>,
93-
setIsError: SetStateCallback<boolean>,
94-
setAuth: SetStateCallback<boolean>,
95-
): Promise<void> => {
96-
try {
97-
const baseUrl = await getBaseUrl();
98-
const response: AxiosResponse<UserData> = await axios(
99-
`${baseUrl}/api/auth/me`,
100-
getAxiosConfig(),
101-
);
102-
const data = response.data;
103-
setIsLoading(false);
104-
setIsAdmin(data.admin || false);
105-
} catch (error) {
106-
setIsLoading(false);
107-
const axiosError = error as AxiosError;
108-
if (axiosError.response?.status === 401) {
109-
setAuth(false);
110-
} else {
111-
setIsError(true);
112-
}
113-
}
114-
};
115-
116-
export { getUser, getUsers, updateUser, getUserLoggedIn };
90+
export { getUser, getUsers, updateUser };

src/ui/views/Login/Login.tsx

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,9 @@ const Login: React.FC = () => {
3434
const [isLoading, setIsLoading] = useState<boolean>(false);
3535
const [authMethods, setAuthMethods] = useState<string[]>([]);
3636
const [usernamePasswordMethod, setUsernamePasswordMethod] = useState<string>('');
37-
const [apiBaseUrl, setApiBaseUrl] = useState<string>('');
3837

3938
useEffect(() => {
40-
// Initialize API base URL
4139
getBaseUrl().then((baseUrl) => {
42-
setApiBaseUrl(baseUrl);
43-
44-
// Fetch auth config
4540
axios.get(`${baseUrl}/api/auth/config`).then((response) => {
4641
const usernamePasswordMethod = response.data.usernamePasswordMethod;
4742
const otherMethods = response.data.otherMethods;
@@ -51,7 +46,7 @@ const Login: React.FC = () => {
5146

5247
// Automatically login if only one non-username/password method is enabled
5348
if (!usernamePasswordMethod && otherMethods.length === 1) {
54-
handleAuthMethodLogin(otherMethods[0], baseUrl);
49+
handleAuthMethodLogin(otherMethods[0]);
5550
}
5651
});
5752
});
@@ -63,37 +58,40 @@ const Login: React.FC = () => {
6358
);
6459
}
6560

66-
function handleAuthMethodLogin(authMethod: string, baseUrl?: string): void {
67-
const url = baseUrl || apiBaseUrl;
68-
window.location.href = `${url}/api/auth/${authMethod}`;
61+
function handleAuthMethodLogin(authMethod: string): void {
62+
getBaseUrl().then((baseUrl) => {
63+
window.location.href = `${baseUrl}/api/auth/${authMethod}`;
64+
});
6965
}
7066

7167
function handleSubmit(event: FormEvent): void {
7268
event.preventDefault();
7369
setIsLoading(true);
7470

75-
const loginUrl = `${apiBaseUrl}/api/auth/login`;
76-
axios
77-
.post<LoginResponse>(loginUrl, { username, password }, getAxiosConfig())
78-
.then(() => {
79-
window.sessionStorage.setItem('git.proxy.login', 'success');
80-
setMessage('Success!');
81-
setSuccess(true);
82-
authContext.refreshUser().then(() => navigate(0));
83-
})
84-
.catch((error: AxiosError) => {
85-
if (error.response?.status === 307) {
71+
getBaseUrl().then((baseUrl) => {
72+
const loginUrl = `${baseUrl}/api/auth/login`;
73+
axios
74+
.post<LoginResponse>(loginUrl, { username, password }, getAxiosConfig())
75+
.then(() => {
8676
window.sessionStorage.setItem('git.proxy.login', 'success');
87-
setGitAccountError(true);
88-
} else if (error.response?.status === 403) {
89-
setMessage(processAuthError(error, false));
90-
} else {
91-
setMessage('You entered an invalid username or password...');
92-
}
93-
})
94-
.finally(() => {
95-
setIsLoading(false);
96-
});
77+
setMessage('Success!');
78+
setSuccess(true);
79+
authContext.refreshUser().then(() => navigate(0));
80+
})
81+
.catch((error: AxiosError) => {
82+
if (error.response?.status === 307) {
83+
window.sessionStorage.setItem('git.proxy.login', 'success');
84+
setGitAccountError(true);
85+
} else if (error.response?.status === 403) {
86+
setMessage(processAuthError(error, false));
87+
} else {
88+
setMessage('You entered an invalid username or password...');
89+
}
90+
})
91+
.finally(() => {
92+
setIsLoading(false);
93+
});
94+
});
9795
}
9896

9997
if (gitAccountError) {

src/ui/views/RepoList/Components/Repositories.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,7 @@ interface GridContainerLayoutProps {
3232
key: string;
3333
}
3434

35-
interface UserContextType {
36-
user: {
37-
admin: boolean;
38-
[key: string]: any;
39-
};
40-
}
41-
42-
export default function Repositories(): JSX.Element {
35+
export default function Repositories(): React.ReactElement {
4336
const useStyles = makeStyles(styles as any);
4437
const classes = useStyles();
4538
const [repos, setRepos] = useState<RepoView[]>([]);

0 commit comments

Comments
 (0)