From 8a462252af3da6147edcc254b3d1a34205f01d4f Mon Sep 17 00:00:00 2001 From: keyan Date: Fri, 21 May 2021 14:34:40 -0500 Subject: [PATCH] force unique user name --- components/fund-error.js | 2 +- lib/url.js | 2 +- pages/api/auth/[...nextauth].js | 23 +++++++++++++++--- .../20210521192356_name_trigger/migration.sql | 24 +++++++++++++++++++ prisma/schema.prisma | 2 +- 5 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 prisma/migrations/20210521192356_name_trigger/migration.sql diff --git a/components/fund-error.js b/components/fund-error.js index 3ffa4026..fcafe630 100644 --- a/components/fund-error.js +++ b/components/fund-error.js @@ -35,7 +35,7 @@ export function FundErrorModal () { onHide={() => setError(false)} > -

you're out of sats!

+

you are out of sats

diff --git a/lib/url.js b/lib/url.js index b43f4eae..434ac50d 100644 --- a/lib/url.js +++ b/lib/url.js @@ -1,5 +1,5 @@ export function ensureProtocol (value) { - if (!/^[a-z0-9]+:(\/\/)?/.test(value)) { + if (!/^([a-z0-9]+:\/\/|mailto:)/.test(value)) { value = 'http://' + value } return value diff --git a/pages/api/auth/[...nextauth].js b/pages/api/auth/[...nextauth].js index 1037ba4d..18c642ff 100644 --- a/pages/api/auth/[...nextauth].js +++ b/pages/api/auth/[...nextauth].js @@ -9,15 +9,32 @@ const options = { providers: [ Providers.GitHub({ clientId: process.env.GITHUB_ID, - clientSecret: process.env.GITHUB_SECRET + clientSecret: process.env.GITHUB_SECRET, + profile: profile => { + return { + ...profile, + name: profile.login + } + } }), Providers.Twitter({ clientId: process.env.TWITTER_ID, - clientSecret: process.env.TWITTER_SECRET + clientSecret: process.env.TWITTER_SECRET, + profile: profile => { + console.log(profile) + return { + ...profile, + name: profile.screen_name + } + } }), Providers.Email({ server: process.env.EMAIL_SERVER, - from: process.env.EMAIL_FROM + from: process.env.EMAIL_FROM, + profile: profile => { + console.log(profile) + return profile + } }) ], adapter: Adapters.Prisma.Adapter({ prisma }), diff --git a/prisma/migrations/20210521192356_name_trigger/migration.sql b/prisma/migrations/20210521192356_name_trigger/migration.sql new file mode 100644 index 00000000..e0b40651 --- /dev/null +++ b/prisma/migrations/20210521192356_name_trigger/migration.sql @@ -0,0 +1,24 @@ +-- AlterEnum +ALTER TYPE "WithdrawlStatus" ADD VALUE 'UNKNOWN_FAILURE'; + +CREATE OR REPLACE FUNCTION assign_name() RETURNS TRIGGER AS $$ + DECLARE + BEGIN + -- if doesn't have a name, SPLIT email on @ and assign to name + IF NEW.name IS NULL THEN + NEW.name = SPLIT_PART(NEW.email, '@', 1); + END IF; + -- replace unsupported characters (non alphanum + _) in name with _ + NEW.name = REGEXP_REPLACE(NEW.name, '\W|_', '_', 'gi'); + -- while name exists append random number + WHILE EXISTS (SELECT 1 FROM users WHERE name = NEW.name) LOOP + NEW.name = NEW.name || floor(random() * 10 + 1)::int; + END LOOP; + + RETURN NEW; + END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER name_tgr + BEFORE INSERT ON "users" + FOR EACH ROW EXECUTE PROCEDURE assign_name(); \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 8196312a..c544c80d 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -14,7 +14,7 @@ model User { id Int @id @default(autoincrement()) createdAt DateTime @default(now()) @map(name: "created_at") updatedAt DateTime @updatedAt @map(name: "updated_at") - name String? @unique + name String @unique email String? @unique emailVerified DateTime? @map(name: "email_verified") image String?