-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSocialAuth.ts
More file actions
53 lines (43 loc) · 1.7 KB
/
SocialAuth.ts
File metadata and controls
53 lines (43 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { OAuth2Provider, OAuth2Config } from './OAuth2Provider';
export enum SocialProvider {
GOOGLE = 'google',
GITHUB = 'github',
LINKEDIN = 'linkedin'
}
export class SocialAuth {
private providers: Map<SocialProvider, OAuth2Provider> = new Map();
constructor() {
this.initializeProviders();
}
private initializeProviders() {
// Configuration would normally come from environment variables
const googleConfig: OAuth2Config = {
clientId: process.env.GOOGLE_CLIENT_ID || '',
clientSecret: process.env.GOOGLE_CLIENT_SECRET || '',
redirectUri: `${process.env.APP_URL}/auth/google/callback`,
authEndpoint: 'https://accounts.google.com/o/oauth2/v2/auth',
tokenEndpoint: 'https://oauth2.googleapis.com/token',
userInfoEndpoint: 'https://www.googleapis.com/oauth2/v3/userinfo',
scope: 'openid email profile'
};
this.providers.set(SocialProvider.GOOGLE, new OAuth2Provider(googleConfig));
// Similarly for GitHub and LinkedIn...
}
getRedirectUrl(provider: SocialProvider, state: string): string {
const authProvider = this.providers.get(provider);
if (!authProvider) throw new Error(`Provider ${provider} not configured`);
return authProvider.getAuthUrl(state);
}
async handleCallback(provider: SocialProvider, code: string): Promise<any> {
const authProvider = this.providers.get(provider);
if (!authProvider) throw new Error(`Provider ${provider} not configured`);
const profile = await authProvider.authenticate(code);
return {
provider,
providerId: profile.id,
email: profile.email,
displayName: profile.name,
avatar: profile.raw.picture || profile.raw.avatar_url
};
}
}