Skip to content

Commit f7be0f5

Browse files
committed
Working on Auth Implementation
1 parent 7ac5501 commit f7be0f5

4 files changed

Lines changed: 21 additions & 72 deletions

File tree

.env.webui

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/api/auth.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,22 @@ import type { AuthUser } from '../types/User.ts';
33

44
export const authApi = {
55
// Get current user
6-
getMe: () => apiRequest<AuthUser>('/auth/me'),
6+
getMe: () => apiRequest<AuthUser>('/api/auth/me'),
77

8-
// Initiate Google OAth
9-
loginWithGoogle: () => {
10-
window.location.href = '/auth/google';
11-
},
12-
13-
// Initiate Microsoft OAuth
14-
loginWithMicrosoft: () => {
15-
window.location.href = '/auth/microsoft';
8+
// Initiate Auth
9+
login: (provider: string) => {
10+
window.location.href = `/api/auth/${provider}`;
1611
},
1712

1813
// Logout
1914
logout: () =>
20-
apiRequest<void>('/auth/logout', {
15+
apiRequest<void>('/api/auth/logout', {
2116
method: 'POST',
2217
}),
2318

2419
// Refresh token
2520
refresh: () =>
26-
apiRequest<void>('/auth/refresh', {
21+
apiRequest<void>('/api/auth/refresh', {
2722
method: 'POST',
2823
}),
2924
};

src/contexts/AuthContext.tsx

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable react-refresh/only-export-components*/
2-
import React, { createContext, useContext, useState, type ReactNode } from 'react';
2+
import React, { createContext, useContext, useState, useEffect, type ReactNode } from 'react';
3+
import { authApi } from '../api/auth';
34

45
type User = {
56
uid?: string;
@@ -23,7 +24,6 @@ type AuthProviderProps = {
2324
};
2425

2526
const AuthContext = createContext<AuthContextType | undefined>(undefined);
26-
const AUTH_API_BASE = '/api/v1/auth';
2727

2828
export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
2929
const [user, setUser] = useState<User | null>(null);
@@ -33,77 +33,36 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
3333
const fetchMe = async (): Promise<void> => {
3434
try {
3535
setLoading(true);
36+
setError(null);
3637

37-
const response = await fetch(`${AUTH_API_BASE}/me`, {
38-
headers: { Accept: 'application/json' },
39-
credentials: 'include',
40-
});
41-
42-
if (response.ok) {
43-
const data: User = await response.json();
44-
setUser(data);
45-
} else {
46-
setUser(null);
47-
}
38+
const data = await authApi.getMe();
39+
setUser(data);
4840
} catch (err: unknown) {
49-
if (err instanceof Error) {
50-
console.error('Failed to fetch user:', err);
51-
setError(err.message);
52-
} else {
53-
console.error('Unknown error:', err);
54-
setError('Unknown error');
55-
}
56-
5741
setUser(null);
42+
setError(err instanceof Error ? err.message : 'Unknown error');
5843
} finally {
5944
setLoading(false);
6045
}
6146
};
6247

63-
const login = (provider: 'google' | 'microsoft'): void => {
64-
const url = provider === 'google' ? `${AUTH_API_BASE}/google` : `${AUTH_API_BASE}/microsoft`;
65-
66-
window.open(url, '_blank');
67-
68-
const pollInterval = setInterval(async () => {
69-
try {
70-
const response = await fetch(`${AUTH_API_BASE}/me`, {
71-
headers: { Accept: 'application/json' },
72-
credentials: 'include',
73-
});
74-
75-
if (response.ok) {
76-
const data: User = await response.json();
77-
setUser(data);
78-
clearInterval(pollInterval);
79-
}
80-
} catch (err: unknown) {
81-
if (err instanceof Error) {
82-
console.error('Polling failed:', err.message);
83-
}
84-
}
85-
}, 3000);
48+
useEffect(() => {
49+
fetchMe();
50+
}, []);
8651

87-
setTimeout(() => {
88-
clearInterval(pollInterval);
89-
}, 120000);
52+
const login = (provider: 'google' | 'microsoft'): void => {
53+
authApi.login(provider);
9054
};
55+
9156
const logout = async (): Promise<void> => {
9257
try {
93-
await fetch(`${AUTH_API_BASE}/logout`, {
94-
method: 'POST',
95-
credentials: 'include',
96-
});
97-
58+
await authApi.logout();
9859
setUser(null);
99-
10060
window.location.href = '/app/';
10161
} catch (err: unknown) {
102-
if (err instanceof Error) {
103-
console.error('Logout failed:', err.message);
104-
}
62+
console.error('Logout failed:', err);
10563
}
10664
};
65+
10766
const value: AuthContextType = {
10867
user,
10968
loading,

src/types/Organization.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export type UpdateOrganization = {
1616
};
1717

1818
export type OrganizationUser = {
19-
// TODO: API returns same as base User
2019
date_joined: string;
2120
email: string;
2221
first_name: string;

0 commit comments

Comments
 (0)