|
| 1 | +// code walkthrough: https://www.netlify.com/blog/2018/03/29/jamstack-architecture-on-netlify-how-identity-and-functions-work-together/#updating-user-data-with-the-identity-api |
| 2 | +// demo repo: https://github.com/biilmann/testing-slack-tutorial/tree/v3-one-message-an-hour |
| 3 | +// note: requires SLACK_WEBHOOK_URL environment variable |
| 4 | +const slackURL = process.env.SLACK_WEBHOOK_URL; |
| 5 | +const fetch = require("node-fetch"); |
| 6 | + |
| 7 | +class IdentityAPI { |
| 8 | + constructor(apiURL, token) { |
| 9 | + this.apiURL = apiURL; |
| 10 | + this.token = token; |
| 11 | + } |
| 12 | + |
| 13 | + headers(headers = {}) { |
| 14 | + return { |
| 15 | + "Content-Type": "application/json", |
| 16 | + Authorization: `Bearer ${this.token}`, |
| 17 | + ...headers |
| 18 | + }; |
| 19 | + } |
| 20 | + |
| 21 | + parseJsonResponse(response) { |
| 22 | + return response.json().then(json => { |
| 23 | + if (!response.ok) { |
| 24 | + return Promise.reject({ status: response.status, json }); |
| 25 | + } |
| 26 | + |
| 27 | + return json; |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + request(path, options = {}) { |
| 32 | + const headers = this.headers(options.headers || {}); |
| 33 | + return fetch(this.apiURL + path, { ...options, headers }).then(response => { |
| 34 | + const contentType = response.headers.get("Content-Type"); |
| 35 | + if (contentType && contentType.match(/json/)) { |
| 36 | + return this.parseJsonResponse(response); |
| 37 | + } |
| 38 | + |
| 39 | + if (!response.ok) { |
| 40 | + return response.text().then(data => { |
| 41 | + return Promise.reject({ stauts: response.status, data }); |
| 42 | + }); |
| 43 | + } |
| 44 | + return response.text().then(data => { |
| 45 | + data; |
| 46 | + }); |
| 47 | + }); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/* |
| 52 | + Fetch a user from GoTrue via id |
| 53 | +*/ |
| 54 | +function fetchUser(identity, id) { |
| 55 | + const api = new IdentityAPI(identity.url, identity.token); |
| 56 | + return api.request(`/admin/users/${id}`); |
| 57 | +} |
| 58 | + |
| 59 | +/* |
| 60 | + Update the app_metadata of a user |
| 61 | +*/ |
| 62 | +function updateUser(identity, user, app_metadata) { |
| 63 | + const api = new IdentityAPI(identity.url, identity.token); |
| 64 | + const new_app_metadata = { ...user.app_metadata, ...app_metadata }; |
| 65 | + |
| 66 | + return api.request(`/admin/users/${user.id}`, { |
| 67 | + method: "PUT", |
| 68 | + body: JSON.stringify({ app_metadata: new_app_metadata }) |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +const oneHour = 60 * 60 * 1000; |
| 73 | +export function handler(event, context, callback) { |
| 74 | + if (event.httpMethod !== "POST") { |
| 75 | + return callback(null, { |
| 76 | + statusCode: 410, |
| 77 | + body: "Unsupported Request Method" |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + const claims = context.clientContext && context.clientContext.user; |
| 82 | + if (!claims) { |
| 83 | + return callback(null, { |
| 84 | + statusCode: 401, |
| 85 | + body: "You must be signed in to call this function" |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + fetchUser(context.clientContext.identity, claims.sub).then(user => { |
| 90 | + const lastMessage = new Date( |
| 91 | + user.app_metadata.last_message_at || 0 |
| 92 | + ).getTime(); |
| 93 | + const cutOff = new Date().getTime() - oneHour; |
| 94 | + if (lastMessage > cutOff) { |
| 95 | + return callback(null, { |
| 96 | + statusCode: 401, |
| 97 | + body: "Only one message an hour allowed" |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + try { |
| 102 | + const payload = JSON.parse(event.body); |
| 103 | + |
| 104 | + fetch(slackURL, { |
| 105 | + method: "POST", |
| 106 | + body: JSON.stringify({ |
| 107 | + text: payload.text, |
| 108 | + attachments: [{ text: `From ${user.email}` }] |
| 109 | + }) |
| 110 | + }) |
| 111 | + .then(() => |
| 112 | + updateUser(context.clientContext.identity, user, { |
| 113 | + last_message_at: new Date().getTime() |
| 114 | + }) |
| 115 | + ) |
| 116 | + .then(() => { |
| 117 | + callback(null, { statusCode: 204 }); |
| 118 | + }) |
| 119 | + .catch(err => { |
| 120 | + callback(null, { |
| 121 | + statusCode: 500, |
| 122 | + body: "Internal Server Error: " + e |
| 123 | + }); |
| 124 | + }); |
| 125 | + } catch (e) { |
| 126 | + callback(null, { statusCode: 500, body: "Internal Server Error: " + e }); |
| 127 | + } |
| 128 | + }); |
| 129 | +} |
0 commit comments