-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth-manager.js
More file actions
151 lines (130 loc) · 4.16 KB
/
auth-manager.js
File metadata and controls
151 lines (130 loc) · 4.16 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Auth0 Configuration for Production
class Auth0Manager {
constructor() {
this.domain = 'dev-j244xylfomnfbzn8.us.auth0.com';
this.clientId = '5VZQZVh4RCyJBqZmrzDAFT12YHjXIvUy';
this.redirectUri = this.getRedirectUri();
this.auth0Client = null;
this.isInitialized = false;
}
getRedirectUri() {
// Handle different environments
if (typeof window !== 'undefined') {
return window.location.origin;
}
return 'https://your-vercel-domain.vercel.app';
}
async initialize() {
if (this.isInitialized) return this.auth0Client;
try {
// Load Auth0 SDK if not already loaded
if (typeof auth0 === 'undefined') {
await this.loadAuth0SDK();
}
this.auth0Client = await auth0.createAuth0Client({
domain: this.domain,
clientId: this.clientId,
authorizationParams: {
redirect_uri: this.redirectUri
},
cacheLocation: 'localstorage',
useRefreshTokens: true
});
this.isInitialized = true;
console.log('✅ Auth0 initialized successfully');
return this.auth0Client;
} catch (error) {
console.error('❌ Auth0 initialization failed:', error);
throw error;
}
}
async loadAuth0SDK() {
return new Promise((resolve, reject) => {
if (typeof auth0 !== 'undefined') {
resolve();
return;
}
const script = document.createElement('script');
script.src = 'https://cdn.auth0.com/js/auth0-spa-js/2.0/auth0-spa-js.production.js';
script.async = true;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
async handleRedirectCallback() {
if (!this.auth0Client) {
throw new Error('Auth0 client not initialized');
}
try {
await this.auth0Client.handleRedirectCallback();
// Clean up the URL
window.history.replaceState({}, document.title, window.location.pathname);
return true;
} catch (error) {
console.error('Redirect callback error:', error);
throw error;
}
}
async login() {
if (!this.auth0Client) {
await this.initialize();
}
try {
await this.auth0Client.loginWithRedirect({
authorizationParams: {
redirect_uri: this.redirectUri
}
});
} catch (error) {
console.error('Login error:', error);
throw error;
}
}
async logout() {
if (!this.auth0Client) return;
try {
await this.auth0Client.logout({
logoutParams: {
returnTo: this.redirectUri
}
});
} catch (error) {
console.error('Logout error:', error);
throw error;
}
}
async isAuthenticated() {
if (!this.auth0Client) return false;
try {
return await this.auth0Client.isAuthenticated();
} catch (error) {
console.error('Authentication check error:', error);
return false;
}
}
async getUser() {
if (!this.auth0Client) return null;
try {
return await this.auth0Client.getUser();
} catch (error) {
console.error('Get user error:', error);
return null;
}
}
async getAccessToken() {
if (!this.auth0Client) return null;
try {
return await this.auth0Client.getTokenSilently();
} catch (error) {
console.error('Get access token error:', error);
return null;
}
}
}
// Create global instance
const authManager = new Auth0Manager();
// Export for modules
if (typeof module !== 'undefined' && module.exports) {
module.exports = { Auth0Manager, authManager };
}