video-flow-b/lib/auth.ts
2025-06-30 17:16:15 +08:00

70 lines
1.9 KiB
TypeScript

// Mock Google OAuth configuration
const GOOGLE_CLIENT_ID = 'your-google-client-id';
const GOOGLE_REDIRECT_URI = typeof window !== 'undefined'
? `${window.location.origin}/api/auth/google/callback`
: '';
/**
* Initiates Google OAuth authentication flow
*/
export const signInWithGoogle = () => {
const params = new URLSearchParams({
client_id: GOOGLE_CLIENT_ID,
redirect_uri: GOOGLE_REDIRECT_URI,
response_type: 'code',
scope: 'email profile',
prompt: 'select_account',
});
// In a real implementation, you would have proper error handling and secure state management
window.location.href = `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`;
};
/**
* Gets the current user from session storage (mock implementation)
*/
export const getCurrentUser = () => {
if (typeof window === 'undefined') return null;
const userJson = sessionStorage.getItem('currentUser');
if (!userJson) return null;
try {
return JSON.parse(userJson);
} catch (error) {
console.error('Failed to parse user data from session', error);
return null;
}
};
/**
* Handles user login (mock implementation)
*/
export const loginUser = async (email: string, password: string) => {
// This is a mock implementation
// In a real app, you would make an API call to your backend
return new Promise((resolve) => {
setTimeout(() => {
// Mock user data
const user = {
id: '123',
name: 'Test User',
email,
avatar: 'https://i.pravatar.cc/150?u=' + email,
};
// Store in session storage (just for demo purposes)
sessionStorage.setItem('currentUser', JSON.stringify(user));
resolve(user);
}, 1000);
});
};
/**
* Handles user logout
*/
export const logoutUser = () => {
if (typeof window === 'undefined') return;
sessionStorage.removeItem('currentUser');
window.location.href = '/login';
};