-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle existing user trying to use OTP
- Loading branch information
Showing
5 changed files
with
330 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { TRPCError } from '@trpc/server' | ||
import { supabaseAdmin } from 'app/utils/supabase/admin' | ||
import debug from 'debug' | ||
import { z } from 'zod' | ||
import { createTRPCRouter, publicProcedure } from '../../trpc' | ||
import { AuthStatus } from './types' | ||
|
||
const log = debug('api:auth') | ||
|
||
export const authRouter = createTRPCRouter({ | ||
signInWithOtp: publicProcedure | ||
.input( | ||
z.object({ | ||
phone: z.string().trim(), | ||
countrycode: z.string(), | ||
captchaToken: z.string().optional(), | ||
bypassOnboardedCheck: z.boolean().optional().default(false), | ||
}) | ||
) | ||
.mutation(async ({ input }) => { | ||
const { phone, countrycode, captchaToken, bypassOnboardedCheck } = input | ||
|
||
if (!phone) { | ||
throw new TRPCError({ | ||
code: 'BAD_REQUEST', | ||
message: 'Phone number is required', | ||
}) | ||
} | ||
|
||
if (!countrycode) { | ||
throw new TRPCError({ | ||
code: 'BAD_REQUEST', | ||
message: 'Country Code is required', | ||
}) | ||
} | ||
|
||
if (!!process.env.NEXT_PUBLIC_TURNSTILE_SITE_KEY && !captchaToken) { | ||
throw new TRPCError({ | ||
code: 'BAD_REQUEST', | ||
message: 'Captcha token is required', | ||
}) | ||
} | ||
|
||
if (!bypassOnboardedCheck) { | ||
log('checking if phone is already used', { phone }) | ||
|
||
const { data } = await supabaseAdmin | ||
.rpc('profile_lookup', { lookup_type: 'phone', identifier: `${countrycode}${phone}` }) | ||
.maybeSingle() | ||
|
||
if (data) { | ||
log('phone is already used', { phone }) | ||
|
||
return { | ||
status: AuthStatus.PhoneAlreadyUsed, | ||
} | ||
} | ||
} | ||
|
||
const { error } = await supabaseAdmin.auth | ||
.signInWithOtp({ phone: `${countrycode}${phone}`, options: { captchaToken } }) | ||
.then(async (r) => { | ||
// TODO: potentially add a fake numbers list for app store reviewers | ||
if (__DEV__ || process.env.CI) { | ||
log('fake_otp_credentials', { phone: `${countrycode}${phone}` }) | ||
return await supabaseAdmin.rpc('fake_otp_credentials', { | ||
phone: `${countrycode}${phone}`, | ||
}) | ||
} | ||
const errMessage = r.error?.message.toLowerCase() | ||
log('signInWithOtp', { errMessage, phone }) | ||
return r | ||
}) | ||
|
||
if (error) { | ||
throw new TRPCError({ | ||
code: 'INTERNAL_SERVER_ERROR', | ||
message: error.message, | ||
}) | ||
} | ||
|
||
log('successfully signed up with otp', { phone }) | ||
|
||
return { | ||
status: AuthStatus.SignedIn, | ||
} | ||
}), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum AuthStatus { | ||
SignedIn = 'SignedIn', | ||
PhoneAlreadyUsed = 'PhoneAlreadyUsed', | ||
} |
Oops, something went wrong.