Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add create auth user migration function #3979

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions functions/scripts/runtimeConfig/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ export const runtimeConfigTest: configVars = {
prerender: {
api_key: 'fake_prerender_key',
},
migration: {
api_key: 'fake_migration_key',
},
}
31 changes: 31 additions & 0 deletions functions/src/Utils/migration.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { CONFIG } from '../config/config'

export function migrationEnabled() {
switch (CONFIG.deployment.site_url) {
case 'https://dev.onearmy.world':
case 'https://dev.community.projectkamp.com':
case 'https://dev.community.fixing.fashion':
return true
default:
return false
}
}

export function migrationProject() {
switch (CONFIG.deployment.site_url) {
case 'https://dev.onearmy.world':
return 'dev_pp'
case 'https://community.preciousplastic.com':
return 'prod_pp'
case 'https://dev.community.projectkamp.com':
return 'dev_pk'
case 'https://community.projectkamp.com':
return 'prod_pk'
case 'https://dev.community.fixing.fashion':
return 'dev_ff'
case 'https://community.fixing.fashion':
return 'prod_ff'
default:
return null
}
}
3 changes: 3 additions & 0 deletions functions/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ if (Object.keys(c).length === 0) {
slack_webhook: 'http://simulated-webhook-receiver:30102/slack',
},
service: null,
migration: {
api_key: 'fake_key',
},
} as any
} else {
c = {
Expand Down
2 changes: 2 additions & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ exports.aggregations = require('./aggregations')

exports.database = require('./database')

exports.supabaseCreateAuthUser = require('./supabaseMigration')

exports.userUpdates = UserUpdates.handleUserUpdates

exports.discussionUpdates = DiscussionUpdates.handleDiscussionUpdate
Expand Down
42 changes: 42 additions & 0 deletions functions/src/supabaseMigration/createAuthUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Import Firebase Functions and Admin SDK
import * as functions from 'firebase-functions'
import axios from 'axios'
import { migrationEnabled, migrationProject } from '../Utils/migration.utils'
import { CONFIG } from '../config/config'

const MIGRATION_API = 'https://supabase-migration-api.fly.dev/migration'
const MIGRATION_API_KEY = CONFIG.migration.api_key

async function sendToSupabase(userDoc) {
try {
const response = await axios.post(`${MIGRATION_API}/auth`, userDoc, {
headers: {
'x-api-key': MIGRATION_API_KEY,
},
})

console.log('User synced with Supabase')
// console.log('User synced with Supabase:', response.data)
// return response.data
} catch (error) {
console.error(
'Error syncing user with Supabase:',
error.response?.data || error.message,
)
throw new Error('Failed to sync user with Supabase')
}
}

// Firebase function triggered on user creation
exports.default = functions.auth.user().onCreate((user) => {
if (!migrationEnabled) return
const userDoc = {
uid: user.uid,
project: migrationProject,
email: user.email,
doc: user,
}
console.log('Sending new user to supabase')
// console.log('Sending new user to supabase:', JSON.stringify(userDoc))
return sendToSupabase(userDoc)
})
1 change: 1 addition & 0 deletions functions/src/supabaseMigration/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.createAuthUser = require('./createAuthUser')
3 changes: 3 additions & 0 deletions shared/models/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ export interface configVars {
prerender: {
api_key: string
}
migration?: {
api_key: string
}
}
Loading