Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
"caption": "Sign in with OAuth",
"doc": "Signs in a user using an OAuth provider (Google, GitHub, etc.)",
"fields": {
"additional_parameters": {
"caption": "Additional Parameters",
"doc": "Optional. Additional OAuth parameters as a JSON string (e.g., {\"prompt\": \"consent\"})",
"editor": "DynamicValue",
"long_text": true,
"name": "additional_parameters",
"optional": true,
"rank": 3,
"value": "text"
},
"provider": {
"caption": "Provider",
"doc": "The OAuth provider to use (e.g., 'google', 'github', 'facebook', 'azure', 'bitbucket', 'discord', 'gitlab', 'notion', 'slack', 'spotify', 'twitch', 'twitter', 'workos')",
Expand All @@ -26,16 +36,6 @@
"optional": true,
"rank": 2,
"value": "text"
},
"additional_parameters": {
"caption": "Additional Parameters",
"doc": "Optional. Additional OAuth parameters as a JSON string (e.g., {\"prompt\": \"consent\"})",
"editor": "DynamicValue",
"long_text": true,
"name": "additional_parameters",
"optional": true,
"rank": 3,
"value": "text"
}
}
}
}
36 changes: 34 additions & 2 deletions elements/AAC-850m6/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,39 @@ function(instance, context) {

// Add to the existing initialization code, after the Supabase client is initialized
if (instance.data.supabase) {
// Centralized auth state change handler
// First, check for OAuth callback state
const hasCallbackState = window.location.hash || window.location.search;
if (hasCallbackState) {
console.log('Detected OAuth callback state, attempting to handle...');
instance.data.supabase.auth.getSession()
.then(({ data: { session }, error }) => {
if (error) {
return instance.data.handleError("Failed to get session after OAuth", error);
}

if (session) {
console.log('Successfully retrieved session after OAuth callback');
instance.data.updateUserStates(session.user, session, instance.data.element_level_properties);
instance.triggerEvent('user_is_logged_in');
} else {
// If no session, try to exchange the OAuth token
instance.data.supabase.auth.getUser()
.then(({ data: { user }, error }) => {
if (error) {
return instance.data.handleError("Failed to get user after OAuth", error);
}

if (user) {
console.log('Successfully retrieved user after OAuth callback');
instance.data.updateUserStates(user, null, instance.data.element_level_properties);
instance.triggerEvent('user_is_logged_in');
}
});
}
});
}

// Then set up the auth state change listener
instance.data.supabase.auth.onAuthStateChange((event, session) => {
console.log('Auth state changed:', event);

Expand All @@ -211,7 +243,7 @@ function(instance, context) {
break;

case 'USER_UPDATED':
// Handle user data updates (e.g., password change, email update)
// Handle user data updates
instance.data.updateUserStates(session.user, session, instance.data.element_level_properties);
instance.triggerEvent('user_updated');
break;
Expand Down