From d8e113597056f0ccc1e748a1b7c538ff0b164cdf Mon Sep 17 00:00:00 2001 From: Joel Gustafson Date: Fri, 19 Nov 2021 15:35:42 -0500 Subject: [PATCH] twitter auth flow --- pages/api/users.ts | 25 ++++++++++++++++++------- pages/backup.tsx | 5 ++++- pages/login.tsx | 4 ++-- prisma/schema.prisma | 3 ++- utils/verification.ts | 5 +++++ 5 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 utils/verification.ts diff --git a/pages/api/users.ts b/pages/api/users.ts index cf639b7..87a6a5c 100644 --- a/pages/api/users.ts +++ b/pages/api/users.ts @@ -2,6 +2,7 @@ import * as t from "io-ts" import { makeHandler, ServerError } from "next-rest/server" import { prisma } from "utils/prisma" +import { getTextFromPublicKey, zkChatTwitterHandle } from "utils/verification" const postRequestHeaders = t.type({ "content-type": t.literal("application/json"), @@ -9,7 +10,6 @@ const postRequestHeaders = t.type({ const postRequestBody = t.type({ publicKey: t.string, - twitterHandle: t.string, }) type PostRequestHeaders = t.TypeOf @@ -40,6 +40,11 @@ const twitterApiResponse = t.type({ oldest_id: t.string, result_count: t.number, }), + includes: t.type({ + users: t.array( + t.type({ id: t.string, name: t.string, username: t.string }) + ), + }), data: t.array( t.type({ id: t.string, @@ -52,11 +57,11 @@ export default makeHandler("/api/users", { POST: { headers: postRequestHeaders.is, body: postRequestBody.is, - exec: async ({ body: { publicKey, twitterHandle } }) => { - const query = encodeURIComponent(`from:${twitterHandle} "${publicKey}"`) + exec: async ({ body: { publicKey } }) => { + const query = encodeURIComponent(`@${zkChatTwitterHandle} "${publicKey}"`) const res = await fetch( - `https://api.twitter.com/2/tweets/search/recent?query=${query}`, + `https://api.twitter.com/2/tweets/search/recent?query=${query}&expansions=author_id&user.fields=username`, { headers: { Authorization: `Bearer ${process.env.TWITTER_BEARER_TOKEN}`, @@ -79,13 +84,19 @@ export default makeHandler("/api/users", { const [{ id, text }] = data.data - // Change this if we wrap the public key with some text or anything - if (text !== publicKey) { + if (text !== getTextFromPublicKey(publicKey)) { throw new ServerError(500, "Invalid tweet syntax") } + const { username } = data.includes.users.find((user) => user.id === id)! + await prisma.user.create({ - data: { publicKey, twitterHandle, verificationTweetId: id }, + data: { + publicKey, + twitterId: id, + twitterHandle: username, + verificationTweetId: id, + }, }) return { headers: {}, body: undefined } diff --git a/pages/backup.tsx b/pages/backup.tsx index 1eccd7c..228f338 100644 --- a/pages/backup.tsx +++ b/pages/backup.tsx @@ -35,7 +35,10 @@ export default function BackupPage(props: {}) {
-
This is your ZK CHAT login token. Keep it secret and save it somewhere safe:
+
+ This is your ZK CHAT login token. Keep it secret and save it somewhere + safe: +