Akshay
03/28/2018, 10:32 AMmax
03/28/2018, 12:50 PMmoment
and bcrypt
max
03/28/2018, 12:50 PMmax
03/28/2018, 12:51 PMimport * as moment from 'moment'
syntaxAkshay
03/28/2018, 12:58 PMAkshay
03/28/2018, 1:02 PMmax
03/28/2018, 1:04 PMimport { fromEvent, FunctionEvent } from 'graphcool-lib'
import { GraphQLClient } from 'graphql-request'
import * as moment from 'moment'
interface EventData {
id: string
}
interface User {
id: string
}
interface AccountActivationCode {
id: string
createdAt: Date
user: User
}
interface ActivatedUser {
id: string
accountActivated: boolean
}
export default async (event: FunctionEvent<EventData>) => {
console.log(event)
try {
const accountActivationCodeId = event.data.id;
const api = fromEvent(event).api('simple/v1')
// use the ID to get the AccountActivationCode node
const accountActivationCode: AccountActivationCode = await getAccountActivationCode(api, accountActivationCodeId)
.then(r => r.AccountActivationCode)
// check if it exists
if (!accountActivationCode || !accountActivationCode.id) {
return { error: 'User not activate not activated 1' }
}
// check the time stamp - 12 hours to verify an email
const now = moment();
const createdAt = moment(accountActivationCode.createdAt);
if ( moment(now).isBefore(createdAt.subtract(12,'hours')) ) {
return { error: 'User not activate not activated 2' }
}
// everything checks out then set accountActivated on user to true and return true
const activatedUser: ActivatedUser = await activateUserAccount(api, accountActivationCode.user.id)
// check if user exists and was updated
if (!activatedUser || !activatedUser.id) {
return { error: 'User not activate not activated 3' }
}
return { data: { result: true } }
} catch (e) {
console.log(e)
return { error: 'An unexpected error occured during email verification.' }
}
}
async function getAccountActivationCode(api: GraphQLClient, id: string): Promise<{ AccountActivationCode }> {
const query = `
query getAccountActivationCode($id: ID!) {
AccountActivationCode(id: $id) {
id
createdAt
user {
id
}
}
}
`
const variables = { id }
return api.request<{AccountActivationCode}>(query, variables)
}
async function activateUserAccount(api: GraphQLClient, id: string): Promise<ActivatedUser> {
const mutation = `
mutation updateUser($id: ID!, $accountActivated: Boolean!) {
updateUser(id: $id, accountActivated: $accountActivated) {
id
}
}
`
const variables = { id, accountActivated: true }
return api.request<{updateUser: ActivatedUser}>(mutation, variables)
.then(r => r.updateUser)
}
max
03/28/2018, 1:06 PMmax
03/28/2018, 1:07 PMmoment
in your package.json file under dependencies.max
03/28/2018, 1:07 PMmax
03/28/2018, 1:08 PM"dependencies": {
"@types/aws-sdk": "^2.7.0",
"@types/bcryptjs": "^2.4.1",
"@types/form-data": "^2.2.0",
"@types/validator": "^6.3.0",
"Base64": "^1.0.1",
"aws-sdk": "^2.173.0",
"bcryptjs": "^2.4.3",
"form-data": "^2.3.1",
"graphcool-lib": "^0.1.4",
"graphql-request": "^1.4.0",
"isomorphic-fetch": "^2.2.1",
"moment": "^2.19.1",
"uuid": "^3.1.0",
"validator": "^9.0.0"
},
Akshay
03/28/2018, 1:09 PMmax
03/28/2018, 1:10 PMmax
03/28/2018, 1:10 PMAkshay
03/28/2018, 1:12 PMmax
03/28/2018, 1:16 PM