Getting rid of the netlify widgit and rolling our own, gives us the flexibility to also add the username into the signup process as well as allow the user to opt into the newsletter at the same time. Auth is still netlify, via their "gotrue", we're just handling the more of it.
85 lines
3.2 KiB
JavaScript
85 lines
3.2 KiB
JavaScript
import { createUserInsecure } from 'src/services/users/users.js'
|
|
import { db } from 'src/lib/db'
|
|
import { enforceAlphaNumeric, generateUniqueString } from 'src/services/helpers'
|
|
|
|
export const handler = async (req, _context) => {
|
|
const body = JSON.parse(req.body)
|
|
console.log(body)
|
|
console.log(_context)
|
|
// DUMP FROM THE LOGS ABOVE
|
|
/*
|
|
5:09:30 AM: 2020-10-19T18:09:30.011Z 9da27e24-b6ec-404e-8e7d-25b5d323b67a INFO {
|
|
event: 'signup',
|
|
instance_id: '403b7d63-17f9-48f1-a85f-3d6b41c7dad1',
|
|
user: {
|
|
id: '641222ee-3e61-4253-8c11-9f764779bcc5',
|
|
aud: '',
|
|
role: '',
|
|
email: 'k.hutten@protonmail.ch',
|
|
confirmation_sent_at: '2020-10-19T18:09:01Z',
|
|
app_metadata: { provider: 'email' },
|
|
user_metadata: { full_name: 'sick_dog', userName: 'hi bob' },
|
|
created_at: '2020-10-19T18:09:01Z',
|
|
updated_at: '2020-10-19T18:09:01Z'
|
|
}
|
|
}
|
|
5:09:30 AM: 2020-10-19T18:09:30.011Z 9da27e24-b6ec-404e-8e7d-25b5d323b67a INFO {
|
|
callbackWaitsForEmptyEventLoop: [Getter/Setter],
|
|
succeed: [Function],
|
|
fail: [Function],
|
|
done: [Function],
|
|
functionVersion: '$LATEST',
|
|
functionName: 'ba7eb4948d1313283ebb91472c689d38444f07ae2f4278da925d3ce7f1d94e3c',
|
|
memoryLimitInMB: '1024',
|
|
logGroupName: '/aws/lambda/ba7eb4948d1313283ebb91472c689d38444f07ae2f4278da925d3ce7f1d94e3c',
|
|
logStreamName: '2020/10/19/[$LATEST]af6ff2c067da44268b4a0c9d1e4ca1ea',
|
|
clientContext: {
|
|
custom: {
|
|
netlify: 'eyJpZGVudGl0eSI6eyJ1cmwiOiJodHRwczovL2FuZ3J5LWRpamtzdHJhLTAzMWExMC5uZXRsaWZ5LmFwcC8ubmV0bGlmeS9pZGVudGl0eSIsInRva2VuIjoiZXlKaGJHY2lPaUpJVXpJMU5pSXNJblI1Y0NJNklrcFhWQ0o5LmV5SmxlSEFpT2pFMk1ETXhNekV3TWprc0luTjFZaUk2SWpBaWZRLk54Q0hmb0I2aDRpc0V6NnpJREhWbThLTU5hcEZrb3g0dTFXS2dTemhzUncifSwic2l0ZV91cmwiOiJodHRwczovL2FuZ3J5LWRpamtzdHJhLTAzMWExMC5uZXRsaWZ5LmFwcCJ9'
|
|
},
|
|
identity: {
|
|
url: 'https://angry-dijkstra-031a10.netlify.app/.netlify/identity',
|
|
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MDMxMzEwMjksInN1YiI6IjAifQ.NxCHfoB6h4isEz6zIDHVm8KMNapFkox4u1WKgSzhsRw'
|
|
}
|
|
},
|
|
identity: undefined,
|
|
invokedFunctionArn: 'arn:aws:lambda:us-east-1:012533533302:function:ba7eb4948d1313283ebb91472c689d38444f07ae2f4278da925d3ce7f1d94e3c',
|
|
awsRequestId: '9da27e24-b6ec-404e-8e7d-25b5d323b67a',
|
|
getRemainingTimeInMillis: [Function: getRemainingTimeInMillis]
|
|
}
|
|
5:09:30 AM: Duration: 5.78 ms Memory Usage: 69 MB Init Duration: 199.35 ms
|
|
*/
|
|
|
|
const eventType = body.event
|
|
const user = body.user
|
|
const email = user.email
|
|
|
|
let roles = []
|
|
|
|
if (eventType === 'signup') {
|
|
roles.push('user')
|
|
const isUniqueCallback = async (seed) =>
|
|
db.user.findOne({
|
|
where: { userName: seed },
|
|
})
|
|
const userNameSeed = enforceAlphaNumeric(user?.user_metadata?.userName)
|
|
const userName = await generateUniqueString(userNameSeed, isUniqueCallback) // TODO maybe come up with a better default userName?
|
|
const input = {
|
|
email,
|
|
userName,
|
|
name: user?.user_metadata?.full_name,
|
|
id: user.id,
|
|
}
|
|
await createUserInsecure({ input })
|
|
|
|
return {
|
|
statusCode: 200,
|
|
body: JSON.stringify({ app_metadata: { roles: roles } }),
|
|
}
|
|
} else {
|
|
return {
|
|
statusCode: 200,
|
|
}
|
|
}
|
|
}
|