Skip to content

Instantly share code, notes, and snippets.

@gahabeen
Last active January 21, 2021 05:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gahabeen/1f3271cdd428efd5c294dd185fe43cd6 to your computer and use it in GitHub Desktop.
Save gahabeen/1f3271cdd428efd5c294dd185fe43cd6 to your computer and use it in GitHub Desktop.
8base / Auth0 - Passwordless Login/Signup
# 8base / Auth0 - Passwordless Login/Signup
Follow the conversation over there: [https://community.8base.com/t/using-auth0-passwordless-email-connection/529/6](https://community.8base.com/t/using-auth0-passwordless-email-connection/529/6)
import gql from 'graphql-tag'
export const USER_SIGN_UP_WITH_TOKEN = gql`
mutation($authProfileId: ID!, $email: String!) {
userSignUpWithToken(authProfileId: $authProfileId, user: { email: $email }) {
id
}
}
`
export const FIND_USER_BY_EMAIL = gql`
query users($email: String) {
usersList(filter: { email: { equals: $email } }) {
count
}
}
`
import { passwordless } from '../../utils/auth0.js'
import { USER_SIGN_UP_WITH_TOKEN, FIND_USER_BY_EMAIL } from './query'
async function userPasswordlessLogin(event, ctx) {
const { email, code } = event.data
let success = true
let auth = {}
try {
const { id_token } = await passwordless.signIn(email, code).then(({ data }) => data)
auth = { idToken: id_token }
let userExists = await ctx.api
.gqlRequest(FIND_USER_BY_EMAIL, { email }, { checkPermissions: false })
.then(({ usersList }) => usersList.count > 0)
.catch(() => false)
// If user doesn't exists yet
if (!userExists) {
await ctx.api
.gqlRequest(
USER_SIGN_UP_WITH_TOKEN,
{
authProfileId: process.env.AUTH_PROFILE_ID,
email,
},
{
headers: {
authorization: `Bearer ${id_token}`,
},
}
)
.then((item) => {
success = true
})
.catch((err) => {
success = false
console.error(err)
})
}
} catch (error) {
console.error(error)
return {
data: {
success: false,
},
errors: [error],
}
}
return {
data: {
success,
auth,
},
}
}
export default userPasswordlessLogin
import { passwordless } from '../../utils/auth0.js'
/* Send a password reset email to the user */
async function userPasswordlessStart(event) {
const { email, type = 'code' } = event.data
try {
await passwordless.sendEmail(email, type)
} catch (error) {
console.error(error)
return {
data: {
success: false,
},
errors: [error],
}
}
return {
data: {
success: true,
},
}
}
export default userPasswordlessStart
import axios from 'axios'
const clientId = process.env.AUTH0_CLIENT_ID
const clientSecret = process.env.AUTH0_CLIENT_SECRET
export const passwordless = {
sendEmail(email, send = 'code') {
return axios.post('https://formbase.eu.auth0.com/passwordless/start', {
client_id: clientId,
client_secret: clientSecret,
email,
send,
connection: 'email',
})
},
signIn(email, code) {
return axios.post('https://formbase.eu.auth0.com/oauth/token', {
grant_type: 'http://auth0.com/oauth/grant-type/passwordless/otp',
client_id: clientId,
client_secret: clientSecret,
username: email,
otp: code,
realm: 'email',
})
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment